学习数据库必须掌握的54条SQL查询语句1 --1、查找员工的编号、姓名、部门和出生日期,如果出生日期为空值,显示日期不详,并按部门排序输出,日期格式为yyyy-mm-dd。
2 select emp_no,emp_name,dept,isnull(convert(char(10),birthday,120),日期不详) birthday
3 from employee
4 order by dept
5
6 --2、查找与喻自强在同一个单位的员工姓名、性别、部门和职称
7 select emp_no,emp_name,dept,title
8 from employee
9 where emp_name喻自强 and dept in
10 (select dept from employee
11 where emp_name=喻自强)
12
13 --3、按部门进行汇总,统计每个部门的总工资
14 select dept,sum(salary)
15 from employee
16 group by dept
17
18 --4、查找商品名称为14寸显示器商品的销售情况,显示该商品的编号、销售数量、单价和金额
19 select a.prod_id,qty,unit_price,unit_price*qty totprice
20 from sale_item a,product b
21 where a.prod_id=b.prod_id and prod_name=14寸显示器
22
23 --5、在销售明细表中按产品编号进行汇总,统计每种产品的销售数量和金额
24 select prod_id,sum(qty) totqty,sum(qty*unit_price) totprice
25 from sale_item
26 group by prod_id
27
28 --6、使用convert函数按客户编号统计每个客户1996年的订单总金额
29 select cust_id,sum(tot_amt) totprice
30 from sales
31 where convert(char(4),order_date,120)=1996
32 group by cust_id
33
34 --7、查找有销售记录的客户编号、名称和订单总额
35 select a.cust_id,cust_name,sum(tot_amt) totprice
36 from customer a,sales b
37 where a.cust_id=b.cust_id
38 group by a.cust_id,cust_name
39
40 --8、查找在1997年中有销售记录的客户编号、名称和订单总额
41 select a.cust_id,cust_name,sum(tot_amt) totprice
42 from customer a,sales b
43 where a.cust_id=b.cust_id and convert(char(4),order_date,120)=1997
44 group by a.cust_id,cust_name
45
46 --9、查找一次销售最大的销售记录
47 select order_no,cust_id,sale_id,tot_amt
48 from sales
49 where tot_amt=
50 (select max(tot_amt)
51 from sales)
52
53 --10、查找至少有3次销售的业务员名单和销售日期
54 select emp_name,order_date
55 from employee a,sales b
56 where emp_no=sale_id and a.emp_no in
57 (select sale_id
58 from sales
59 group by sale_id
60 having count(*)=3)
61 order by emp_name
62
63 --11、用存在量词查找没有订货记录的客户名称
64 select cust_name
65 from customer a
66 where not exists
67 (select *
68 from sales b
69 where a.cust_id=b.cust_id)
70
71 --12、使用左外连接查找每个客户的客户编号、名称、订货日期、订单金额订货日期不要
您可能关注的文档
- 实验6+汉诺塔游戏的迭代实现.ppt
- 实验二 文件系统及磁盘管理.doc
- 实验二、重金属污染对植物叶绿素含量.ppt
- 学习情境七声卡网卡安装.ppt
- 实验基本操作3:物质的称量.ppt
- 实用手册-高处作业.doc
- 实验三 栈和队列及其应用(I).doc
- 实验三:探究求合力的方法.ppt
- 珠宝商综合险条款分解.doc
- 屋顶绿化垂直绿化技术探讨.ppt
- 小区绿化施工协议书.docx
- 墙面施工协议书.docx
- 1 古诗二首(课件)--2025-2026学年统编版语文二年级下册.pptx
- (2026春新版)部编版八年级道德与法治下册《3.1《公民基本权利》PPT课件.pptx
- (2026春新版)部编版八年级道德与法治下册《4.3《依法履行义务》PPT课件.pptx
- (2026春新版)部编版八年级道德与法治下册《6.2《按劳分配为主体、多种分配方式并存》PPT课件.pptx
- (2026春新版)部编版八年级道德与法治下册《6.1《公有制为主体、多种所有制经济共同发展》PPT课件.pptx
- 初三教学管理交流发言稿.docx
- 小学生课外阅读总结.docx
- 餐饮门店夜经济运营的社会责任报告(夜间贡献)撰写流程试题库及答案.doc
最近下载
- ISO9001-2026质量管理体系标准版中英文及变化点解析.pdf VIP
- HGT 21629-1999 管架标准图(四).docx VIP
- 广州市民办学校申办审批表.doc VIP
- 无人机测绘技术与应用课件21--无人机驾驶员职业规划.ppt
- 2025年袋鼠数学LevelB试卷及答案.pdf VIP
- 《重症凝血病标准化评估中国专家共识(2025)》解读PPT课件.pptx VIP
- 《动物防疫法》培训解读课件.pptx VIP
- 无人机测绘技术与应用课件20--无人机飞行原理.ppt
- 先进控制技术及其应用.docx VIP
- 2025-2026学年济南版(2024)初中生物八年级上册《血液是物质运输的载体》教学设计.docx
原创力文档

文档评论(0)