频道:电脑学院·操作系统·软件教程·设计学院· 网络编程·硬件教程·电脑医院·专家装机·在线问答电脑软硬件应用网支持手机WAP浏览
推荐:黑客防线·QQ 相关·服务器架设·局域网技术·办公自动化·笔记本·图像处理·网页设计·数据库电脑软硬件应用网Rss 2.0
您现在的位置: 电脑软硬件应用网 >> 设计学院 >> 网络编程 >> 数据库 >> 正文
学习DB2数据库必须掌握的五十四条常用语句           ★★★ 【字体:
学习DB2数据库必须掌握的五十四条常用语句
作者:Bert   文章出处:赛迪网   更新时间:2008-5-9 16:04:12

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

select emp_no,emp_name,dept,isnull(convert(char(10),birthday,120),'日期不详') birthday

from employee

order by dept

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

select emp_no,emp_name,dept,title

from employee

where emp_name<>'喻自强' and dept in

(select dept from employee

where emp_name='喻自强')

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、使用左外连接查找每个客户的客户编号、名称、订货日期、订单金额订货日期不要显示时间,日期格式为yyyy-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

[1] [2] [3] [4] 下一页

本文引用自www.45it.com电脑软硬件应用网
  • 上一篇文章:

  • 下一篇文章:
  • 最新热点 最新推荐 相关
    Oracle新手最常碰到的6个错误…
    带参数的sql和不带参数的sql…
    详细讲解Sybase数据库乱码问…
    讲解SQL Server数据库被挂马…
    个人经验总结:数据库分散存…
    实例讲解Oracle数据库自动增…
    解决由于操作不当出现的ERRO…
    讲解更改MySQL数据库root密码…
    个人经验总结:如何限制sa登…
    SQL 2005 express远程访问和…