学习DB2数据库时,你要掌握的语句有哪些?


  本文标签:DB2数据库

  此文章主要向大家讲述的是学习DB2数据库时,我们大家所必须掌握的10条常用语句,以下就是文章对学习DB2数据库时,我们大家所必须掌握的10条常用语句的详细描述,望大家在浏览之后会对其有更深的了解  。

  数据库, 语句, 学习数据库, 语句, 学习

  

  1、查找员工的编号、姓名、部门和出生日期,如果出生日期为空值,显示日期不详,并按部门排序输出,日期格式为yyyy-mm-dd

  

  1. select emp_no,emp_name,dept,isnull(convert(char(10),birthday,120),日期不详) birthday   
  2. from employee   
  3. order by dept  

  2、查找与喻自强在同一个单位的员工姓名、性别、部门和职称

  

  1. select emp_no,emp_name,dept,title   
  2. from employee   
  3. where emp_name<>喻自强 and dept in   
  4. (select dept from employee   
  5. where emp_name=喻自强)  

  3、按部门进行汇总,统计每个部门的总工资

  1. select dept,sum(salary)   
  2. from employee   
  3. group by dept  

  学习DB2数据库必须掌握的语句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   

  以上的相关内容就是对学习DB2数据库必须掌握的五十四条常用语句的介绍,望你能有所收获  。