SQL 상용 어구 DQL 언어

SQL 상용 어구 DQL 언어
  • select

  • 표 조회
    다 표 조회
    수직 합병
    하위 조회
    연결
    select
    단일 테이블 조회
    select * from students;  --  students      
    select stuid,name,age from students;  -- students    stuid,name,age       
    select name as   ,age   ,stuid from students; --         ,as       
    select * from students where stuid = 20;  --  students  stuid 20    
    select * from students where stuid > 20;  --  students  stuid  20    
    select * from students where stuid <> 20;  --  students  stuid   20    
    select * from students where stuid != 20;  --  students  stuid   20    
    select * from students where age>10 and gender='f';  --  students      10        
    select * from students where age >= 20 and age <= 30;  --  students        20     30  
    select * from students where age between 20 and 30;  --        
    select * from students where name like 's%';  --  students  name  s        
    select * from students where name like '%yu%';  --  students  name    yu      
    select distinct age  from students ;  --  students  age  ,         
    select * from students where classid is null; --  students  classid        
    select * from students where classid is not null; --  students  classid         
    select count(stuid) from students;  --  students  stuid         
    select max(age) from students;   --  students         
    select min(age) from students;   --  students         
    select avg(age) from students;   --  students         
    select gender,avg(age) as     from students group by gender;  --  students      ,       
    select classid,avg(age) as     from students group by classid having classid >3;  --  students  classid  3 , classid           
    select classid,gender,avg(age)  from students group by classid,gender;  --  students   classid gender         。       
    select * from students order by age desc;  --  students , age       ,     
    select * from students order by -classid desc;  --  students , classid      ,  null     ,     
    select classid,sum(age) from students group by classid order by -classid desc;  --  students ,   classid             ,      
    select classid,sum(age) from students group by classid order by -classid desc limit 3;  --  students ,   classid             ,      ,      
    select classid,sum(age) from students group by classid order by -classid desc limit 23;  --  students ,   classid             ,      ,           
    select * from students where classid in (1,3,5);  --  students classid 1,3,5  
    

    다 중 표 조회
    수직 합병
    select stuid,name,age,gender from students union select * from teachers;  --     stuid,name,age,gender                   
    select * from teachers union all  select * from teachers;  --        ,   ,  all        
    select * from students cross join teachers;  --                    ,                ,      
    select * from students inner join teachers on students.teacherid=teachers.tid;  --        ID       id     ,     
    select s.stuid,s.name,s.age,t.tid,t.name,t.age from students as s inner join teachers as t on s.teacherid=t.tid; --         id,     ,    id,     ,          ,       ,       ,        ID       ID     
    select s.stuid,s.name,s.age,t.tid,t.name,t.age from students as s left outer join teachers as t on s.teacherid=t.tid;  --                             ,      id,     ,    id,     ,         null,      
    select s.stuid,s.name,s.age,t.tid,t.name,t.age from students as s right outer join teachers as t on s.teacherid=t.tid;  --                             ,      id,     ,    id,     ,         null,      
    select s.stuid,s.name student_name,s.age student_age,t.tid,t.name teacher_name,t.age teacher_age from students as s inner join teachers as t on s.teacherid=t.tid and s.age > 30; --         id,     ,    id,     ,          ,       ,       ,        ID       ID     ,         30   
    select s.stuid,s.name,s.age,t.tid,t.name,t.age from students as s left outer join teachers as t on s.teacherid=t.tid where t.tid is null;  --       ,     tid      
    select s.stuid,s.name,s.age,t.tid,t.name,t.age from students as s right outer join teachers as t on s.teacherid=t.tid where s.teacherid is  null;  --       ,     teacher      
    select s.stuid,s.name,s.age,t.tid,t.name,t.age from students as s full outer join teachers as t on s.teacherid=t.tid;  --             ,  mysql   
    select s.stuid,s.name,s.age,t.tid,t.name,t.age from students as s left outer join teachers as t on s.teacherid=t.tid union select s.stuid,s.name,s.age,t.tid,t.name,t.age from students as s right outer join teachers as t on s.teacherid=t.tid ;  --mysql          ,     ,             
    select * from (select s.stuid,s.name s_name,s.teacherid,t.tid,t.name t_name from students s left outer join teachers t on s.teacherid=t.tid union select s.stuid,s.name,s.teacherid,t.tid,t.name from students as s right outer join teachers as t on s.teacherid=t.tid) as a where a.teacherid is null or a.tid is null;  --                         
    select st.name,co.course,sc.score from students as st inner join scores as sc on st.stuid=sc.stuid inner join courses as co on sc.courseid=co.CourseID;  --      ID       ID       ,             id          ,             ,                ,      
    

    합 쳐 진 표 데이터 형식 과 필드 의 수량 이 같 습 니 다.
    하위 조회
    select * from students where age >(select avg(age) from students);  --                  select       
    

    연결
    seletct e.name,l.name from emp as e left join emp as l on e.leaderid=l.id;  --           ,      ID   ID                 
    

    좋은 웹페이지 즐겨찾기