SQL 클 리 어

머리말
웹 개발 자로 서 데이터베이스 와 접촉 하지 않 는 것 은 거의 불가능 합 니 다!이 를 통 해 알 수 있 듯 이 SQL 문 구 를 파악 하 는 것 이 웹 개발 자 에 게 얼마나 중요 한 지 알 수 있다.다음은 제 가 컴퓨터 를 정리 할 때 찾 은 자료 입 니 다. 괜 찮 으 면 꺼 내 서 여러분 과 공유 하 겠 습 니 다.쓸데없는 소리 하지 마, 어 때? 보면 알 아.주제 에 들 어가 면 본 고의 주요 내용 은 다음 과 같다.
  • I. 문제 배경   
  • II. SQL 조회 54 문?

  • 1. 문제 배경
    본 논문 에서 SQL 문 구 는 모두 아래 의 몇 장의 표를 바탕 으로 하 는데 이것 도 비교적 전형 적 인 데이터 베이스 교육 에 사용 되 는 데이터 베이스 예 이다.
    (1) / * 직원 인사 표 직원 * /
    emp_no
    char(5)
    Not null
    primary key
    직원 번호
    emp_name
    char(10)
    Not null
     
    직원 이름
    sex
    char(1)
    Not null
     
    성별.
    dept
    char(4)
    Not null
     
    소속 부서
    title
    char(6)
    Not null
     
    직명
    date_hired
    datetime
    Not null
     
    부임 일
    birthday
    datetime
    Null
     
    생일.
    salary
    int
    Not null
     
    급료
    addr
    char(50)
    null
     
    주소.
    Mod­_date
    datetime
    Default(getdate())
    조작 자
    (2) / * 고객 표 customer * /
    cust_id
    char(5)
    Not null
    primary key
    고객 번호
    cust_name
    char(20)
    Not null,
     
    클 라 이언 트 이름
    addr
    char(40)
    Not null,
     
    고객 주소
    tel_no
    char(10)
    Not null,
     
    고객 전화
    zip
    char(6)
    null
     
    우편번호
    (3) / * 판매 주 표 sales * /
    order_no
    int
    Not null
    primary key
    주문 번호
    cust_id
    char(5)
    Not null,
     
    고객 번호
    sale_id
    char(5)
    Not null,
     
    업무원 번호
    tot_amt
    numeric(9,2)
    Not null,
     
    주문 금액
    order_date
    datetime
    Not null,
     
    주문 날짜
    ship_date
    datetime
    Not null,
     
    출하 일자
    invoice_no
    char(10)
    Not null
     
    인보이스 번호
    (4) / * 판매 명세서 saleitem */
    order_no
    int
    Not null,
    primary key
    주문 번호
    prod_id
    char(5)
    Not null,
    제품 번호
    qty
    int
    Not null
     
    판매 수량
    unit_price
    numeric(7,2)
    Not null
     
    단가
    order_date
    datetime
    null
     
    주문 날짜
    (5) / * 제품 명 표 product * /
    prod_id
    char(5)
    Not null
    primary key
    제품 번호
    prod_name
    char(20)
    Not null
     
    제품 명
    2. 각종 조회 의 SQL 구문
    문제 1. 직원 의 번호, 이름, 부서 와 생년월일 을 찾 습 니 다. 생년월일 이 비어 있 으 면 날짜 가 불분명 하고 부서 별로 정렬 하여 출력 합 니 다. 날짜 형식 은 yyy - mm - dd 입 니 다.
    문제 2. 같은 부서 에 있 는 직원 의 성명, 성별, 부서 와 직명 찾기
    
      
      
      
      
    1. select emp_no ,emp_name ,dept ,  
    2.        isnull(convert(char(10),birthday,120),' ') birthday  
    3. from employee  
    4. order by dept 

    문제 3. 부서 별로 총결산 하여 각 부서 의 총 임금 을 통계 한다.
    
      
      
      
      
    1. select dept,sum(salary)  
    2. from employee  
    3. group by dept 

    문제 4. 상품 명 이 14 인치 디 스 플레이 상품 의 판매 현황 을 찾 아 해당 상품 의 번호, 판매 수량, 단가 와 금액 을 표시 합 니 다.
    
      
      
      
      
    1. select a.prod_id,qty,unit_price,unit_price*qty totprice  
    2. from sale_item a,product b  
    3. where a.prod_id=b.prod_id and prod_name='14 ' 

     문제 5. 판매 명세서 에서 제품 번호 에 따라 집계 하고 각 제품 의 판매 수량 과 금액 을 통계 한다.
    
      
      
      
      
    1. select prod_id,sum(qty) totqty,sum(qty*unit_price) totprice  
    2. from sale_item  
    3. group by prod_id 

    문제 6. convert 함 수 를 사용 하여 고객 번호 에 따라 모든 고객 의 1996 년 주문 총 금액 을 통계 한다.
    
      
      
      
      
    1. select cust_id,sum(tot_amt) totprice  
    2. from sales  
    3. where convert(char(4),order_date,120)='1996' 
    4. group by cust_id 

     문제 7. 판매 기록 이 있 는 고객 번호, 명칭 과 주문 총액 찾기
    
      
      
      
      
    1. select a.cust_id,cust_name,sum(tot_amt) totprice  
    2. from customer a,sales b  
    3. where a.cust_id=b.cust_id  
    4. group by a.cust_id,cust_name 

     문제 8. 1997 년 에 판매 기록 이 있 는 고객 번호, 명칭 과 주문 총액 을 찾 습 니 다.
    
      
      
      
      
    1. select a.cust_id,cust_name,sum(tot_amt) totprice  
    2. from customer a,sales b  
    3. where a.cust_id=b.cust_id and convert(char(4),order_date,120)='1997' 
    4. group by a.cust_id,cust_name 

    질문 9. 판매 최대 의 판매 기록 찾기
    
      
      
      
      
    1. select order_no,cust_id,sale_id,tot_amt  
    2. from sales  
    3. where tot_amt=  
    4.    (select max(tot_amt)  
    5.     from sales) 

    질문 10. 최소 3 번 의 영업 사원 명단 과 판매 날 짜 를 찾 습 니 다.
    
      
      
      
      
    1. select emp_name,order_date  
    2. from employee a,sales b   
    3. where emp_no=sale_id and a.emp_no in 
    4.   (select sale_id  
    5.    from sales  
    6.    group by sale_id  
    7.    having count(*)>=3)  
    8. order by emp_name 

    문제 11. 존재 하 는 양사 로 주문 기록 이 없 는 고객 이름 찾기
    
      
      
      
      
    1. select cust_name  
    2. from customer a  
    3. where not exists  
    4.    (select *  
    5.     from sales b  
    6.     where a.cust_id=b.cust_id) 

    문제 12. 왼쪽 연결 을 사용 하여 모든 고객 의 고객 번호, 명칭, 주문 날짜, 주문 금액 을 찾 습 니 다.주문 날 짜 는 시간 을 표시 하지 마 십시오. 날짜 형식 은 yyy - mm - dd 입 니 다.고객 번호 에 따라 정렬 하고 같은 고객 은 주문 내림차 순 으로 정렬 하여 출력 한다.
    
      
      
      
      
    1. select a.cust_id,cust_name,convert(char(10),order_date,120),tot_amt  
    2. from customer a left outer join sales b on a.cust_id=b.cust_id  
    3. order by a.cust_id,tot_amt desc 

    문제 13. 16M D 램 의 판매 현황 을 찾 아 해당 판매원 의 성명, 성별, 판매 날짜, 판매 수량 과 금액 을 표시 해 야 한다. 그 중에서 성별 은 남자, 여자 로 표시 한다.
    
      
      
      
      
    1. select emp_name  ,  = case a.sex  when 'm' then ' ' 
    2.                                        when 'f' then ' '   
    3.                                        else ' ' 
    4.                                        end,  
    5.          = isnull(convert(char(10),c.order_date,120),' '),  
    6.         qty  , qty*unit_price as   
    7. from employee a, sales b, sale_item c,product d  
    8. where d.prod_name='16M DRAM' and d.pro_id=c.prod_id and 
    9.       a.emp_no=b.sale_id and b.order_no=c.order_no 

    문제 14. 모든 사람의 판매 기록 을 찾 아 판매원 의 번호, 성명, 성별, 제품 명칭, 수량, 단가, 금액 과 판매 날 짜 를 표시 하도록 요구한다.
    
      
      
      
      
    1. select emp_no  ,emp_name  ,  = case a.sex when 'm' then ' ' 
    2.                                        when 'f' then ' '   
    3.                                        else ' ' 
    4.                                        end,  
    5.       prod_name  , = isnull(convert(char(10),c.order_date,120),' '),  
    6.       qty  , qty*unit_price as   
    7. from employee a left outer join sales b on a.emp_no=b.sale_id , sale_item c,product d  
    8. where d.pro_id=c.prod_id and b.order_no=c.order_no 

    문제 15. 판매 금액 이 가장 큰 고객 이름과 총 대금 찾기
    
      
      
      
      
    1. select cust_name,d.cust_sum  
    2. from   customer a,  
    3.        (select cust_id,cust_sum  
    4.         from (select cust_id, sum(tot_amt) as cust_sum  
    5.               from sales  
    6.               group by cust_id ) b  
    7.         where b.cust_sum =   
    8.                ( select max(cust_sum)  
    9.                  from (select cust_id, sum(tot_amt) as cust_sum  
    10.                        from sales  
    11.                        group by cust_id ) c )  
    12.         ) d  
    13. where a.cust_id=d.cust_id 

    문제 16. 매출 총액 이 1000 위안 이하 인 판매원 번호, 이름과 매출 액 찾기
    
      
      
      
      
    1. select emp_no,emp_name,d.sale_sum  
    2. from   employee a,  
    3.        (select sale_id,sale_sum  
    4.         from (select sale_id, sum(tot_amt) as sale_sum  
    5.               from sales  
    6.               group by sale_id ) b  
    7.         where b.sale_sum <1000                 
    8.         ) d  
    9. where a.emp_no=d.sale_id 

    문제 17. 최소 3 가지 상품 을 판매 한 고객 번호, 고객 이름, 상품 번호, 상품 명, 수량 과 금액 찾기
    
      
      
      
      
    1. select a.cust_id,cust_name,b.prod_id,prod_name,d.qty,d.qty*d.unit_price  
    2. from customer a, product b, sales c, sale_item d  
    3. where a.cust_id=c.cust_id and d.prod_id=b.prod_id and   
    4.       c.order_no=d.order_no and a.cust_id in (  
    5.       select cust_id  
    6.       from  (select cust_id,count(distinct prod_id) prodid  
    7.              from (select cust_id,prod_id  
    8.                    from sales e,sale_item f  
    9.                    where e.order_no=f.order_no) g  
    10.              group by cust_id  
    11.              having count(distinct prod_id)>=3) h ) 

    문제 18. 적어도 세계 기술 개발 회사 와 같은 고객 번호, 명칭 과 상품 번호, 상품 명칭, 수량 과 금액 찾기
    
      
      
      
      
    1. select a.cust_id,cust_name,d.prod_id,prod_name,qty,qty*unit_price  
    2. from customer a, product b, sales c, sale_item d  
    3. where a.cust_id=c.cust_id and d.prod_id=b.prod_id and   
    4.       c.order_no=d.order_no  and not exists  
    5.   (select f.*  
    6.    from customer x ,sales e, sale_item f  
    7.    where cust_name=' ' and x.cust_id=e.cust_id and 
    8.          e.order_no=f.order_no and not exists  
    9.            ( select g.*  
    10.              from sale_item g, sales  h  
    11.              where g.prod_id = f.prod_id and g.order_no=h.order_no and 
    12.                    h.cust_id=a.cust_id)  
    13.     ) 

    문제 19. 표 에 있 는 모든 유 씨 직원 의 번호, 부서, 월급 을 찾 습 니 다.
    
      
      
      
      
    1. select emp_no,emp_name,dept,salary  
    2. from employee  
    3. where emp_name like ' %' 

    문제 20. 모든 주문서 금액 이 20000 이상 인 모든 고객 번 호 를 찾 습 니 다.
    
      
      
      
      
    1. select cust_id  
    2. from sales  
    3. where tot_amt>20000 

    문제 21. 통계표 에서 직원 들 의 월급 이 40000 - 60000 사이 인 사람
    
      
      
      
      
    1. select count(*)as   
    2. from employee  
    3. where salary between 40000 and 60000 

    문제 22. 조회 표 에 있 는 같은 부서 의 직원 들 의 평균 임금 이지 만 '주소' 만 조회 하 는 것 은 '상하 이 시' 의 직원 이다.
    
      
      
      
      
    1. select avg(salary) avg_sal,dept   
    2. from employee   
    3. where addr like ' %' 
    4. group by dept 

    문제 23. 표 의 주소 가 '상하 이 시' 인 직원 의 주 소 를 '베 이 징 시' 로 바 꿉 니 다.
    
      
      
      
      
    1. update employee    
    2. set addr like ' ' 
    3. where addr like ' ' 

    문제 24. 업무부 또는 회계부 여 직원 의 기본 정보 찾기
    
      
      
      
      
    1. select emp_no,emp_name,dept  
    2. from employee   
    3. where sex='F'and dept in (' ',' '

    문제 25. 각 제품 의 판매 금액 의 총 계 를 나타 내 고 판매 금액 에 따라 큰 것 에서 작은 것 으로 수출 한다.
    
      
      
      
      
    1. select prod_id ,sum(qty*unit_price)  
    2. from sale_item   
    3. group by prod_id  
    4. order by sum(qty*unit_price) desc 

    문제 26. 번호 가 'C0001' 과 'C0004' 에 있 는 고객 번호, 고객 이름, 고객 주 소 를 선택한다.
    
      
      
      
      
    1. select CUST_ID,cust_name,addr  
    2. from customer   
    3. where cust_id between 'C0001' AND 'C0004' 

    문제
    
      
      
      
      
    1. select count(distinct prod_id) as ' ' 
    2. from sale_item 

    문제 28 、 업무부 직원 급여 3% 인상
    
      
      
      
      
    1. update employee  
    2. set salary=salary*1.03  
    3. where dept=' ' 

    문제 29. employee 표 에서 월급 이 가장 낮은 직원 정 보 를 찾 아 냈 다.
    
      
      
      
      
    1. select *  
    2. from employee  
    3. where salary=  
    4.        (select min(salary )  
    5.         from employee ) 

    질문 30. join 을 사용 하여 고객 의 이름 이 '고객 병' 이 구 매 한 화물 의 '고객 이름', '주문서 금액', '주문 날짜', '전화번호' 를 조회 합 니 다.
    
      
      
      
      
    1. select a.cust_id,b.tot_amt,b.order_date,a.tel_no  
    2. from customer a join sales b  
    3. on a.cust_id=b.cust_id and cust_name like ' ' 

    문제 31. sales 표 에서 주문 금액 이 'E0013 업무원 이 1996 / 10 / 15 일 에 받 은 모든 주문 금액' 보다 큰 것 을 찾 아 냈 다.
    
      
      
      
      
    1. select *  
    2. from sales  
    3. where tot_amt>all 
    4.        (select tot_amt   
    5.         from sales   
    6.         where sale_id='E0013'and order_date='1996/10/15')  
    7. order by tot_amt 

    문제 32. 'P0001' 제품 의 평균 판매 단 가 를 계산한다.
    
      
      
      
      
    1. select avg(unit_price)  
    2. from sale_item  
    3. where prod_id='P0001' 

    문제 33. 회사 여 직원 이 받 은 주문 서 를 찾 아 라.
    
      
      
      
      
    1. select sale_id,tot_amt  
    2. from sales  
    3. where sale_id in   
    4. (select sale_id from employee  
    5. where sex='F'

    문제 34. 같은 날 입사 한 직원 찾기
    
      
      
      
      
    1. select a.emp_no,a.emp_name,a.date_hired  
    2. from employee a  
    3. join employee b  
    4. on (a.emp_no!=b.emp_no and a.date_hired=b.date_hired)  
    5. order by a.date_hired 

    문 제 는 35. 현재 실적 이 232000 위안 을 넘 는 직원 번호 와 이름 을 찾 아 라.
    
      
      
      
      
    1. select emp_no,emp_name  
    2. from employee   
    3. where emp_no in 
    4. (select sale_id  
    5. from sales   
    6. group by sale_id  
    7. having sum(tot_amt)<232000) 

    문제 36. employee 표 에서 모든 여 직원 의 평균 임금 과 주소 가 '상하 이 시' 에 있 는 모든 여 직원 의 평균 임금 을 조회 했다.
    
      
      
      
      
    1. select avg(salary)  
    2. from employee  
    3. where sex like 'f' 
    4. union 
    5. select avg(salary)  
    6. from employee  
    7. where sex like 'f' and addr like ' %' 

    문제 37. employee 표 에서 월급 이 직원 의 평균 월급 을 초과 하 는 직원 정 보 를 조회 합 니 다.
    
      
      
      
      
    1. Select * from employee where salary>(select avg(salary)  from employee) 

    문 제 는 38. 현재 판매 실적 이 40000 위안 을 넘 는 업무원 번호 와 판매 실적 을 찾아내 고 판매 실적 에 따라 큰 것 부터 작은 것 까지 순 위 를 매 긴 다.
    
      
      
      
      
    1. Select sale_id ,sum(tot_amt)  
    2. from sales   
    3. group by sale_id   
    4. having sum(tot_amt)>40000  
    5. order by sum(tot_amt) desc 

    문제 39. 회사 남자 업무원 이 받 고 주문 금액 이 2000 위안 을 넘 는 주문 번호 와 주문 금액 을 찾아낸다.
    
      
      
      
      
    1. Select order_no,tot_amt  
    2. From sales ,employee  
    3. Where sale_id=emp_no and sex='M' and tot_amt>2000 

    질문 40. sales 표 에서 주문 금액 이 가장 높 은 주문 번호 와 주문 금액 조회
    
      
      
      
      
    1. Select order_no,tot_amt from sales where tot_amt=(select max(tot_amt)  from sales) 

    문제 41. 각 주문 에서 주문 금액 이 24000 위안 을 넘 는 고객 이름과 주 소 를 조회 합 니 다.
    
      
      
      
      
    1. Select cust_name,addr from customer a,sales b where a.cust_id=b.cust_id and tot_amt>24000 

    문제 42. 모든 고객 의 총 주문 금액 을 구하 고 고객 번호 와 총 주문 금액 을 나타 내 며 총 주문 금액 의 내림차 순 으로 배열 한다.
    
      
      
      
      
    1. Select cust_id,sum(tot_amt) from sales  
    2. Group by cust_id   
    3. Order by sum(tot_amt) desc 

    문제 43. 모든 고객 이 주문 한 모든 제품 의 총 수량 과 평균 단 가 를 구하 고 고객 번호 에 따라 제품 번 호 는 작은 것 부터 큰 것 까지 배열 한다.
    
      
      
      
      
    1. Select cust_id,prod_id,sum(qty),sum(qty*unit_price)/sum(qty)  
    2. From sales a, sale_item b  
    3. Where a.order_no=b.order_no  
    4. Group by cust_id,prod_id  
    5. Order by cust_id,prod_id 

    질문
    
      
      
      
      
    1. Select order_no from sale_item  
    2. Group by order_no  
    3. Having count(*)>3 

    문제 45. 주문 을 조회 하 는 제품 은 적어도 10003 에서 주문 한 제품 의 주문 을 포함한다.
    
      
      
      
      
    1. Select  distinct order_no  
    2. From sale_item a  
    3. Where  order_no<>'10003'and  not exists (   
    4.        Select *  from sale_item b where order_no ='10003'  and not exists   
    5.               (select *  from sale_item c where c.order_no=a.order_no  and  c.prod_id=b.prod_id)) 

    문제 46. sales 표 에서 주문 금액 이 'E0013 업무원 이 1996 / 11 / 10 당일 에 받 은 모든 주문 금액' 보다 큰 것 을 찾 아 냈 고 이 주문 을 받 은 업무원 과 이 주문 의 금액 을 나 타 냈 다.
    
      
      
      
      
    1. Select sale_id,tot_amt   
    2. from sales  
    3. where tot_amt>all(select tot_amt from sales where sale_id='E0013' and order_date='1996/11/10'

     문제 47. 마지막 업 무 를 맡 은 직원 의 정 보 를 조회 합 니 다.
    
      
      
      
      
    1. Select *  
    2. From employee a  
    3. Where not exists   
    4.          (select * from  sales b where  a.emp_no=b.sale_id) 

    문제 48, 상해 시 에서 온 고객 의 성명, 전화, 주문 번호 및 주문 금액 조회
    
      
      
      
      
    1. Select cust_name,tel_no,order_no,tot_amt  
    2. From customer a ,sales b  
    3. Where a.cust_id=b.cust_id and addr=' ' 

    문 제 는 49. 각 업무원 의 매달 실적 을 조회 하고 업무원 번호, 월 별로 순 서 를 낮 춘 다.
    
      
      
      
      
    1. Select sale_id,month(order_date), sum(tot_amt)   
    2. from sales   
    3. group by sale_id,month(order_date)  
    4. order by sale_id,month(order_date) desc 

    문제 50. 각 제품 의 총 판매 수량 과 총 판매 금액 을 구하 고 제품 번호, 제품 명칭, 총 수량 과 총 금액 을 표시 하 며 제품 번호 에 따라 작은 것 부터 큰 것 까지 배열 해 야 한다.
    
      
      
      
      
    1. Select a.prod_id,prod_name,sum(qty),sum(qty*unit_price)  
    2. From sale_item a,product b  
    3. Where a.prod_id=b.prod_id   
    4. Group by a.prod_id,prod_name  
    5. Order by a.prod_id 

    문제 51. 총 주문 금액 이 'C0002' 고객 의 총 주문 금액 을 초과 하 는 고객 번호, 고객 이름과 주소 조회
    
      
      
      
      
    1. Select cust_id, cust_name,addr  
    2. From customer  
    3. Where cust_id  in (select cust_id from sales   
    4. Group by cust_id  
    5. Having sum(tot_amt)>  
    6.         (Select sum(tot_amt) from sales  where cust_id='C0002')) 

     문제 52. 실적 이 가장 좋 은 업무원 번호, 업무원 이름과 그 총 판매 금액 조회
    
      
      
      
      
    1. select emp_no,emp_name,sum(tot_amt)  
    2. from employee a,sales b  
    3. where a.emp_no=b.sale_id  
    4. group by emp_no,emp_name  
    5. having sum(tot_amt)=  
    6.          (select max(totamt)  
    7.           from (select sale_id,sum(tot_amt) totamt  
    8.                from sales  
    9.                group by sale_id) c) 

    문제 53. 모든 고객 이 주문 한 모든 제품 의 상세 한 명세 서 를 조회 하고 고객 번호, 고객 명, 제품 번호, 제품 명, 수량 과 단 가 를 표시 해 야 한다.
    
      
      
      
      
    1. select a.cust_id, cust_name,c.prod_id,prod_name,qty, unit_price  
    2. from customer a,sales b, sale_item c ,product d  
    3. where a.cust_id=b.cust_id and b.order_no=c.order_no and c.prod_id=d.prod_id 

    문 제 는 54. 각 부서 의 평균 임금 을 구하 고 평균 임금 에 따라 작은 것 부터 큰 것 까지 순 위 를 매 겨 야 한다.
    
      
      
      
      
    1. select dept,avg(salary) from employee group by dept order by avg(salary) 

    이 자료 들 이 당신 에 게 도움 이 되 기 를 바 랍 니 다!

    좋은 웹페이지 즐겨찾기