SQL 클 리 어
웹 개발 자로 서 데이터베이스 와 접촉 하지 않 는 것 은 거의 불가능 합 니 다!이 를 통 해 알 수 있 듯 이 SQL 문 구 를 파악 하 는 것 이 웹 개발 자 에 게 얼마나 중요 한 지 알 수 있다.다음은 제 가 컴퓨터 를 정리 할 때 찾 은 자료 입 니 다. 괜 찮 으 면 꺼 내 서 여러분 과 공유 하 겠 습 니 다.쓸데없는 소리 하지 마, 어 때? 보면 알 아.주제 에 들 어가 면 본 고의 주요 내용 은 다음 과 같다.
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. 같은 부서 에 있 는 직원 의 성명, 성별, 부서 와 직명 찾기
- select emp_no ,emp_name ,dept ,
- isnull(convert(char(10),birthday,120),' ') birthday
- from employee
- order by dept
문제 3. 부서 별로 총결산 하여 각 부서 의 총 임금 을 통계 한다.
- select dept,sum(salary)
- from employee
- group by dept
문제 4. 상품 명 이 14 인치 디 스 플레이 상품 의 판매 현황 을 찾 아 해당 상품 의 번호, 판매 수량, 단가 와 금액 을 표시 합 니 다.
- select a.prod_id,qty,unit_price,unit_price*qty totprice
- from sale_item a,product b
- where a.prod_id=b.prod_id and prod_name='14 '
문제 5. 판매 명세서 에서 제품 번호 에 따라 집계 하고 각 제품 의 판매 수량 과 금액 을 통계 한다.
- select prod_id,sum(qty) totqty,sum(qty*unit_price) totprice
- from sale_item
- group by prod_id
문제 6. convert 함 수 를 사용 하여 고객 번호 에 따라 모든 고객 의 1996 년 주문 총 금액 을 통계 한다.
- select cust_id,sum(tot_amt) totprice
- from sales
- where convert(char(4),order_date,120)='1996'
- group by cust_id
문제 7. 판매 기록 이 있 는 고객 번호, 명칭 과 주문 총액 찾기
- select a.cust_id,cust_name,sum(tot_amt) totprice
- from customer a,sales b
- where a.cust_id=b.cust_id
- group by a.cust_id,cust_name
문제 8. 1997 년 에 판매 기록 이 있 는 고객 번호, 명칭 과 주문 총액 을 찾 습 니 다.
- select a.cust_id,cust_name,sum(tot_amt) totprice
- from customer a,sales b
- where a.cust_id=b.cust_id and convert(char(4),order_date,120)='1997'
- group by a.cust_id,cust_name
질문 9. 판매 최대 의 판매 기록 찾기
- select order_no,cust_id,sale_id,tot_amt
- from sales
- where tot_amt=
- (select max(tot_amt)
- from sales)
질문 10. 최소 3 번 의 영업 사원 명단 과 판매 날 짜 를 찾 습 니 다.
- select emp_name,order_date
- from employee a,sales b
- where emp_no=sale_id and a.emp_no in
- (select sale_id
- from sales
- group by sale_id
- having count(*)>=3)
- order by emp_name
문제 11. 존재 하 는 양사 로 주문 기록 이 없 는 고객 이름 찾기
- select cust_name
- from customer a
- where not exists
- (select *
- from sales b
- where a.cust_id=b.cust_id)
문제 12. 왼쪽 연결 을 사용 하여 모든 고객 의 고객 번호, 명칭, 주문 날짜, 주문 금액 을 찾 습 니 다.주문 날 짜 는 시간 을 표시 하지 마 십시오. 날짜 형식 은 yyy - mm - dd 입 니 다.고객 번호 에 따라 정렬 하고 같은 고객 은 주문 내림차 순 으로 정렬 하여 출력 한다.
- select a.cust_id,cust_name,convert(char(10),order_date,120),tot_amt
- from customer a left outer join sales b on a.cust_id=b.cust_id
- order by a.cust_id,tot_amt desc
문제 13. 16M D 램 의 판매 현황 을 찾 아 해당 판매원 의 성명, 성별, 판매 날짜, 판매 수량 과 금액 을 표시 해 야 한다. 그 중에서 성별 은 남자, 여자 로 표시 한다.
- select emp_name , = case a.sex when 'm' then ' '
- when 'f' then ' '
- else ' '
- end,
- = isnull(convert(char(10),c.order_date,120),' '),
- qty , qty*unit_price as
- from employee a, sales b, sale_item c,product d
- where d.prod_name='16M DRAM' and d.pro_id=c.prod_id and
- a.emp_no=b.sale_id and b.order_no=c.order_no
문제 14. 모든 사람의 판매 기록 을 찾 아 판매원 의 번호, 성명, 성별, 제품 명칭, 수량, 단가, 금액 과 판매 날 짜 를 표시 하도록 요구한다.
- select emp_no ,emp_name , = case a.sex when 'm' then ' '
- when 'f' then ' '
- else ' '
- end,
- prod_name , = isnull(convert(char(10),c.order_date,120),' '),
- qty , qty*unit_price as
- from employee a left outer join sales b on a.emp_no=b.sale_id , sale_item c,product d
- where d.pro_id=c.prod_id and b.order_no=c.order_no
문제 15. 판매 금액 이 가장 큰 고객 이름과 총 대금 찾기
- select cust_name,d.cust_sum
- from customer a,
- (select cust_id,cust_sum
- from (select cust_id, sum(tot_amt) as cust_sum
- from sales
- group by cust_id ) b
- where b.cust_sum =
- ( select max(cust_sum)
- from (select cust_id, sum(tot_amt) as cust_sum
- from sales
- group by cust_id ) c )
- ) d
- where a.cust_id=d.cust_id
문제 16. 매출 총액 이 1000 위안 이하 인 판매원 번호, 이름과 매출 액 찾기
- select emp_no,emp_name,d.sale_sum
- from employee a,
- (select sale_id,sale_sum
- from (select sale_id, sum(tot_amt) as sale_sum
- from sales
- group by sale_id ) b
- where b.sale_sum <1000
- ) d
- where a.emp_no=d.sale_id
문제 17. 최소 3 가지 상품 을 판매 한 고객 번호, 고객 이름, 상품 번호, 상품 명, 수량 과 금액 찾기
- select a.cust_id,cust_name,b.prod_id,prod_name,d.qty,d.qty*d.unit_price
- from customer a, product b, sales c, sale_item d
- where a.cust_id=c.cust_id and d.prod_id=b.prod_id and
- c.order_no=d.order_no and a.cust_id in (
- select cust_id
- from (select cust_id,count(distinct prod_id) prodid
- from (select cust_id,prod_id
- from sales e,sale_item f
- where e.order_no=f.order_no) g
- group by cust_id
- having count(distinct prod_id)>=3) h )
문제 18. 적어도 세계 기술 개발 회사 와 같은 고객 번호, 명칭 과 상품 번호, 상품 명칭, 수량 과 금액 찾기
- select a.cust_id,cust_name,d.prod_id,prod_name,qty,qty*unit_price
- from customer a, product b, sales c, sale_item d
- where a.cust_id=c.cust_id and d.prod_id=b.prod_id and
- c.order_no=d.order_no and not exists
- (select f.*
- from customer x ,sales e, sale_item f
- where cust_name=' ' and x.cust_id=e.cust_id and
- e.order_no=f.order_no and not exists
- ( select g.*
- from sale_item g, sales h
- where g.prod_id = f.prod_id and g.order_no=h.order_no and
- h.cust_id=a.cust_id)
- )
문제 19. 표 에 있 는 모든 유 씨 직원 의 번호, 부서, 월급 을 찾 습 니 다.
- select emp_no,emp_name,dept,salary
- from employee
- where emp_name like ' %'
문제 20. 모든 주문서 금액 이 20000 이상 인 모든 고객 번 호 를 찾 습 니 다.
- select cust_id
- from sales
- where tot_amt>20000
문제 21. 통계표 에서 직원 들 의 월급 이 40000 - 60000 사이 인 사람
- select count(*)as
- from employee
- where salary between 40000 and 60000
문제 22. 조회 표 에 있 는 같은 부서 의 직원 들 의 평균 임금 이지 만 '주소' 만 조회 하 는 것 은 '상하 이 시' 의 직원 이다.
- select avg(salary) avg_sal,dept
- from employee
- where addr like ' %'
- group by dept
문제 23. 표 의 주소 가 '상하 이 시' 인 직원 의 주 소 를 '베 이 징 시' 로 바 꿉 니 다.
- update employee
- set addr like ' '
- where addr like ' '
문제 24. 업무부 또는 회계부 여 직원 의 기본 정보 찾기
- select emp_no,emp_name,dept
- from employee
- where sex='F'and dept in (' ',' ')
문제 25. 각 제품 의 판매 금액 의 총 계 를 나타 내 고 판매 금액 에 따라 큰 것 에서 작은 것 으로 수출 한다.
- select prod_id ,sum(qty*unit_price)
- from sale_item
- group by prod_id
- order by sum(qty*unit_price) desc
문제 26. 번호 가 'C0001' 과 'C0004' 에 있 는 고객 번호, 고객 이름, 고객 주 소 를 선택한다.
- select CUST_ID,cust_name,addr
- from customer
- where cust_id between 'C0001' AND 'C0004'
문제
- select count(distinct prod_id) as ' '
- from sale_item
문제 28 、 업무부 직원 급여 3% 인상
- update employee
- set salary=salary*1.03
- where dept=' '
문제 29. employee 표 에서 월급 이 가장 낮은 직원 정 보 를 찾 아 냈 다.
- select *
- from employee
- where salary=
- (select min(salary )
- from employee )
질문 30. join 을 사용 하여 고객 의 이름 이 '고객 병' 이 구 매 한 화물 의 '고객 이름', '주문서 금액', '주문 날짜', '전화번호' 를 조회 합 니 다.
- select a.cust_id,b.tot_amt,b.order_date,a.tel_no
- from customer a join sales b
- on a.cust_id=b.cust_id and cust_name like ' '
문제 31. sales 표 에서 주문 금액 이 'E0013 업무원 이 1996 / 10 / 15 일 에 받 은 모든 주문 금액' 보다 큰 것 을 찾 아 냈 다.
- select *
- from sales
- where tot_amt>all
- (select tot_amt
- from sales
- where sale_id='E0013'and order_date='1996/10/15')
- order by tot_amt
문제 32. 'P0001' 제품 의 평균 판매 단 가 를 계산한다.
- select avg(unit_price)
- from sale_item
- where prod_id='P0001'
문제 33. 회사 여 직원 이 받 은 주문 서 를 찾 아 라.
- select sale_id,tot_amt
- from sales
- where sale_id in
- (select sale_id from employee
- where sex='F')
문제 34. 같은 날 입사 한 직원 찾기
- select a.emp_no,a.emp_name,a.date_hired
- from employee a
- join employee b
- on (a.emp_no!=b.emp_no and a.date_hired=b.date_hired)
- order by a.date_hired
문 제 는 35. 현재 실적 이 232000 위안 을 넘 는 직원 번호 와 이름 을 찾 아 라.
- select emp_no,emp_name
- from employee
- where emp_no in
- (select sale_id
- from sales
- group by sale_id
- having sum(tot_amt)<232000)
문제 36. employee 표 에서 모든 여 직원 의 평균 임금 과 주소 가 '상하 이 시' 에 있 는 모든 여 직원 의 평균 임금 을 조회 했다.
- select avg(salary)
- from employee
- where sex like 'f'
- union
- select avg(salary)
- from employee
- where sex like 'f' and addr like ' %'
문제 37. employee 표 에서 월급 이 직원 의 평균 월급 을 초과 하 는 직원 정 보 를 조회 합 니 다.
- Select * from employee where salary>(select avg(salary) from employee)
문 제 는 38. 현재 판매 실적 이 40000 위안 을 넘 는 업무원 번호 와 판매 실적 을 찾아내 고 판매 실적 에 따라 큰 것 부터 작은 것 까지 순 위 를 매 긴 다.
- Select sale_id ,sum(tot_amt)
- from sales
- group by sale_id
- having sum(tot_amt)>40000
- order by sum(tot_amt) desc
문제 39. 회사 남자 업무원 이 받 고 주문 금액 이 2000 위안 을 넘 는 주문 번호 와 주문 금액 을 찾아낸다.
- Select order_no,tot_amt
- From sales ,employee
- Where sale_id=emp_no and sex='M' and tot_amt>2000
질문 40. sales 표 에서 주문 금액 이 가장 높 은 주문 번호 와 주문 금액 조회
- Select order_no,tot_amt from sales where tot_amt=(select max(tot_amt) from sales)
문제 41. 각 주문 에서 주문 금액 이 24000 위안 을 넘 는 고객 이름과 주 소 를 조회 합 니 다.
- Select cust_name,addr from customer a,sales b where a.cust_id=b.cust_id and tot_amt>24000
문제 42. 모든 고객 의 총 주문 금액 을 구하 고 고객 번호 와 총 주문 금액 을 나타 내 며 총 주문 금액 의 내림차 순 으로 배열 한다.
- Select cust_id,sum(tot_amt) from sales
- Group by cust_id
- Order by sum(tot_amt) desc
문제 43. 모든 고객 이 주문 한 모든 제품 의 총 수량 과 평균 단 가 를 구하 고 고객 번호 에 따라 제품 번 호 는 작은 것 부터 큰 것 까지 배열 한다.
- Select cust_id,prod_id,sum(qty),sum(qty*unit_price)/sum(qty)
- From sales a, sale_item b
- Where a.order_no=b.order_no
- Group by cust_id,prod_id
- Order by cust_id,prod_id
질문
- Select order_no from sale_item
- Group by order_no
- Having count(*)>3
문제 45. 주문 을 조회 하 는 제품 은 적어도 10003 에서 주문 한 제품 의 주문 을 포함한다.
- Select distinct order_no
- From sale_item a
- Where order_no<>'10003'and not exists (
- Select * from sale_item b where order_no ='10003' and not exists
- (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 당일 에 받 은 모든 주문 금액' 보다 큰 것 을 찾 아 냈 고 이 주문 을 받 은 업무원 과 이 주문 의 금액 을 나 타 냈 다.
- Select sale_id,tot_amt
- from sales
- where tot_amt>all(select tot_amt from sales where sale_id='E0013' and order_date='1996/11/10')
문제 47. 마지막 업 무 를 맡 은 직원 의 정 보 를 조회 합 니 다.
- Select *
- From employee a
- Where not exists
- (select * from sales b where a.emp_no=b.sale_id)
문제 48, 상해 시 에서 온 고객 의 성명, 전화, 주문 번호 및 주문 금액 조회
- Select cust_name,tel_no,order_no,tot_amt
- From customer a ,sales b
- Where a.cust_id=b.cust_id and addr=' '
문 제 는 49. 각 업무원 의 매달 실적 을 조회 하고 업무원 번호, 월 별로 순 서 를 낮 춘 다.
- Select sale_id,month(order_date), sum(tot_amt)
- from sales
- group by sale_id,month(order_date)
- order by sale_id,month(order_date) desc
문제 50. 각 제품 의 총 판매 수량 과 총 판매 금액 을 구하 고 제품 번호, 제품 명칭, 총 수량 과 총 금액 을 표시 하 며 제품 번호 에 따라 작은 것 부터 큰 것 까지 배열 해 야 한다.
- Select a.prod_id,prod_name,sum(qty),sum(qty*unit_price)
- From sale_item a,product b
- Where a.prod_id=b.prod_id
- Group by a.prod_id,prod_name
- Order by a.prod_id
문제 51. 총 주문 금액 이 'C0002' 고객 의 총 주문 금액 을 초과 하 는 고객 번호, 고객 이름과 주소 조회
- Select cust_id, cust_name,addr
- From customer
- Where cust_id in (select cust_id from sales
- Group by cust_id
- Having sum(tot_amt)>
- (Select sum(tot_amt) from sales where cust_id='C0002'))
문제 52. 실적 이 가장 좋 은 업무원 번호, 업무원 이름과 그 총 판매 금액 조회
- select emp_no,emp_name,sum(tot_amt)
- from employee a,sales b
- where a.emp_no=b.sale_id
- group by emp_no,emp_name
- having sum(tot_amt)=
- (select max(totamt)
- from (select sale_id,sum(tot_amt) totamt
- from sales
- group by sale_id) c)
문제 53. 모든 고객 이 주문 한 모든 제품 의 상세 한 명세 서 를 조회 하고 고객 번호, 고객 명, 제품 번호, 제품 명, 수량 과 단 가 를 표시 해 야 한다.
- select a.cust_id, cust_name,c.prod_id,prod_name,qty, unit_price
- from customer a,sales b, sale_item c ,product d
- where a.cust_id=b.cust_id and b.order_no=c.order_no and c.prod_id=d.prod_id
문 제 는 54. 각 부서 의 평균 임금 을 구하 고 평균 임금 에 따라 작은 것 부터 큰 것 까지 순 위 를 매 겨 야 한다.
- select dept,avg(salary) from employee group by dept order by avg(salary)
이 자료 들 이 당신 에 게 도움 이 되 기 를 바 랍 니 다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
깊이 중첩된 객체를 정확히 일치 검색 - PostgreSQL목차 * 🚀 * 🎯 * 🏁 * 🙏 JSON 객체 예시 따라서 우리의 현재 목표는 "고용주"사용자가 입력한 검색어(이 경우에는 '요리')를 얻고 이 용어와 정확히 일치하는 모든 사용자 프로필을 찾는 것입니다. 즐거운 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.