学习数据库必须掌握的54条SQL查询语句.doc

学习数据库必须掌握的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、使用左外连接查找每个客户的客户编号、名称、订货日期、订单金额订货日期不要

文档评论(0)

1亿VIP精品文档

相关文档