《数据库概论》课程期中复习题目汇总
一、为某百货公司设计一个E-R模型。
百货管辖若干个连锁商店,每家商店经营若干商品,每家商店有若干职工,但每个职工只能服务于一家商店。
实体类型“商店”的属性有:商店编号,店号,店址,店经理。实体类型“商品”的属性有:商品编号,商品名,单价,产地。实体类型“职工”的属性有:职工编号,职工名,性别,工资。在联系中应反映出职工参加某商店工作的开始时间,商店销售商品的有销售量。
试画出反映商店、商品、职工实体类型及联系类型的ER图,并将其转换成关系模式集。
二、关系代数
设有三个关系:
S(S#,SNAME,AGE,SEX)
SC(S#,C#,GRADE)
C(C#,CNAME,TEACHER)
说明:S#——学号SNAME——学生姓名AGE——年龄SEX——性别
C#——课程号CNAME——课程名TEACHER——教师GRADE——成绩
试用关系代数写出查询语句。
(1)检索LIU老师所授课程的课程号、课程名。
(2)检索年龄大于23岁的男学生的学号与姓名。
(3)检索学号为S3学生所学课程的课程名与任课教师名。
(4)检索至少选修LIU老师所授课程中一门课程的女学生的姓名。
(5)检索W ANG同学不学的课程号。
(6)检索至少选修两门课程的学生学号。
(7)检索全部学生都选修的课程的课程号与学生学号。
(8)检索选修课程包含LIU老师所授课程的学生学号。
(用∞代表自然连接):
(1)πC#,CNA ME(σTEA CHER='LIU'(C))
(2)πS#,SNAME(σAGE>'23'∧SEX='男'(S))
(3)πCNA ME,TEACHER(σS#='S3'(SC∞C))
(4)πSNAME(σSEX='女'∧TEA CHER='LIU'(S∞SC∞C))
(5)πC#(C)-πC#(σSNAME='WANG'(S∞SC))
(6)πS#(σ1=4∧2≠5(SC×SC)) (SC自乘之后,同一个学号下两个课程号不同的元组)
(7)πC#,S#(SC∞(πS#,C#(SC)÷πS#(S))))
(8)πS#(σTEA CHER='LIU'(S∞SC∞C))
三、SQL语句(1)
1、建立一个数据库和五张表的表结构。
(1)/*员工人事表employee */
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)/*销货明细表sale_item */
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 产品名称
4、通过T-SQL语句修改表约束。
1)分别为每张表建立主键约束。
2)为相关表建立外键约束。
3)在表employee加入CHECK约束:输入的员工编号必须以E开头的5位数编号,性别只能为M/F。
4)为销售主表sales中的发票编号字段建立UNIQUE约束。
5)创建一个视图,该视图只含上海客户信息,即客户号、客户姓名、住址
6)对视图添加一条记录数据(注意:分别查看customer表和该视图的结果)
7)删除视图中所有姓“王”的客户数据
8)通过视图修改表内某一客户的姓名
9)有两个基本表employee和sales,创建一个视图,该视图包含相同业务员的编号、姓名、订单号、销售总金额
10)将上述视图中订单号为10001的记录的销售总金额改为60000
11)给上述视图添加一条记录数据
12)删除上述视图
(1)建立数据库jianghong
create database jianghong
on
(name='jianghong',
filename='E:\数据库\jianghong.mdf',
size=3,
maxsize=10,
filegrowth= 1 )
log on
(name='jianghonglog',
filename='E:\数据库\jianghonglog.ldf',
size=1,
maxsize=5,
filegrowth=1)
go
(2)建立数据表
①创建employee表:
create table employee
(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,
data_hired datetime not null,
birthday datetime null,
salary int not null,
addr char(50) null,
Mod_date datetime Default(getdate()),) ②创建customer表:
create table 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,)
③创建sales表:
create table 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,)
④创建sale_item表:
create table sale_item
(order_no int not null ,
prod_id char(5) not null ,
qty int not null,
unit_price numeric(7,2) not null,
order_date datetime null,
primary key (order_no,prod_id),)
⑤创建product表:
create table product
(prod_id char(5) not null primary key,
prod_name char(20) not null,)
(3)为相关表建立外键约束
①alter table sales add foreign key (cust_id) references customer(cust_id);
②alter table sale_item add foreign key (order_no) references sales(order_no);
③alter table sale_item add foreign key (prod_id) references product(prod_id);
(4)为表employee加入check约束:
alter table employee add constraint c1 check(emp_no like 'E____' and (sex='M' or sex='f'));
(5)为表sales发票号建立unique约束:
alter table sales add unique(invoice_no);
(6)创建一个视图,该视图只含上海客户信息,即客户号、客户姓名、住址:create view sh_kehu
as
select cust_id,cust_name,addr
from customer
where addr like '上海'
2
(7)删除视图中所有姓“王”的客户数据:
DELETE
FROM sh_kehu
WHERE cust_name LIKE '王%'
(8)通过视图修改表内某一客户的姓名
update sh_kehu
set cust_name='江洪好'
where cust_id='4224'
(9)有两个基本表employee和sales,创建一个视图,该视图包含相同业务员的编号、姓名、订单号、销售总金额:
create view em_sa
as
select sale_id,emp_name,order_no,tot_amt
from employee,sales
where employee.emp_no=sales.sale_id
(10)将上述视图中订单号为10001的记录的销售总金额改为60000:update em_sa
set tot_amt='60000'
where order_no=10001
(11)删除上述视图:
drop view em_sa
drop view sh_kehu
5、使用SQL语句实现下述查询操作。
1)查找表中所有姓刘的职工的工号,部门,薪水
2)查找所有定单金额高于20000的所有客户编号
3)查找出职称为经理和职称为职员的女员工的姓名、职称、性别。
4)选取销售数量最多的前5条订单订单号、数量。
5)按部门进行汇总,统计每个部门的总工资
6)由employee表中查找出薪水最高的员工信息。
7)由sales表中查找出订单金额大于“E0013业务员在1996/10/15这天所接每一张订单的金额”的所有订单。
8)检索product 表和sale_item表中数量大于2的相同产品的产品编号、产品名称、数量、单价。
9)查找出employee表中住址相同的员工的姓名、性别、职称、薪水、住址。
10)查找所有经理的姓名、职称、薪水。
11)查找出姓“王”并且姓名的最后一个字为“功”的员工。
12)查找住在上海或北京的女员工,并显示其姓名、所属部门、职称、住址。
13)在表sales中挑出销售金额大于等于10000元订单。
14)选取订单金额最高的前10%的订单数据。
15)查找出职称为经理或职称为职员的女员工的信息。
16)删除sales表中作废的订单(其发票号码为I000000004)。
17)计算出一共销售了几种产品。
18)显示sale_item表中每种个别产品的订购金额总和,并且依据销售金额由大到小排列来显示出每一种产品的排行榜。
19)计算每一产品每月的销售金额总和,并将结果按销售(月份,产品编号)排序。
20)由sales表中查找出销售金额最高的订单。
21)由sales表中查找出订单金额大于“E0013业务员在1996/10/15这天所接任一张订单的金额”的所有订单,并显示承接这些订单的业务员和该条订单的金额。
22)找出公司女业务员所接的订单。
23)找出公司中姓名相同的员工,并且依据员工编号排序相识这些员工信息。
24)找出目前业绩未超过200000元的员工。
25)计算公司内各个部门的工资支出总和。
26)计算每一产品销售数量总和与平均销售单价。
27)查找出employee表中部门相同且住址相同的女员工的姓名、性别、职称、薪水、住址。28)检索product 表和sale_item表中相同产品的产品编号、产品名称、数量、单价。
29)检索product 表和sale_item表中单价高于2400元的相同产品的产品编号、产品名称、数量、单价。
30)查找员工的编号、姓名、部门和出生日期,如果出生日期为空值,
--显示日期不详,并按部门排序输出,日期格式为yyyy-mm-dd。
31)查找与喻自强在同一个单位的员工姓名、性别、部门和职称
32)查找商品名称为14寸显示器商品的销售情况,
--显示该商品的编号、销售数量、单价和金额
33)在销售明细表中按产品编号进行汇总,统计每种产品的销售数量和金额
34)按客户编号统计每个客户1996年的订单总金额
35)查找有销售记录的客户编号、名称和订单总额
36)查找在1997年中有销售记录的客户编号、名称和订单总额
37)查找一次销售最大的销售记录
38)查找至少有3次销售的业务员名单和销售日期
39)用存在量词查找没有订货记录的客户名称
40)使用左外连接查找每个客户的客户编号、名称、订货日期、订单金额
--订货日期不要显示时间,日期格式为yyyy-mm-dd
--按客户编号排序,同一客户再按订单降序排序输出
41)查找16MDRAM的销售情况,要求显示相应的销售员的姓名、
--性别,销售日期、销售数量和金额,其中性别用男、女表示
42)查找每个人的销售记录,要求显示销售员的编号、姓名、性别、
--产品名称、数量、单价、金额和销售日期
43)查找销售金额最大的客户名称和总货款
44)查找销售总额少于1000元的销售员编号、姓名和销售额
45)查找至少销售了3种商品的客户编号、客户名称、商品编号、商品名称、数量和金额46)查找至少与世界技术开发公司销售相同的客户编号、名称和商品编号、商品名称、数量和金额
47)统计表中员工的薪水在40000-60000之间的人数
48)查询表中的同一部门的职工的平均工资,但只查询"住址"是"上海市"的员工
49)将表中住址为"上海市"的员工住址改为"北京市"
50)查找业务部或会计部的女员工的基本信息。
51)显示每种产品的销售金额总和,并依销售金额由大到小输出。
52)选取编号界于…C0001?和…C0004?的客户编号、客户名称、客户地址。
53)将业务部员工的薪水上调3%。
54)由employee表中查找出薪水最低的员工信息。
55)使用join查询客户姓名为"客户丙"所购货物的"客户名称","定单金额","定货日期","电话号码"
56)计算'P0001'产品的平均销售单价
57)找出公司女员工所接的定单
58)找出同一天进入公司服务的员工
59)找出目前业绩超过232000元的员工编号和姓名。
60)查询出employee表中所有女职工的平均工资和住址在"上海市"的所有女职工的平均工资
61)在employee表中查询薪水超过员工平均薪水的员工信息。
62)找出目前销售业绩超过40000元的业务员编号及销售业绩,并按销售业绩从大到小排序。63)找出公司男业务员所接且订单金额超过2000元的订单号及订单金额。
64)查询sales表中订单金额最高的订单号及订单金额。
65)查询在每张订单中订购金额超过24000元的客户名及其地址。
66)求出每位客户的总订购金额,显示出客户号及总订购金额,并按总订购金额降序排列。67)求每位客户订购的每种产品的总数量及平均单价,并按客户号,产品号从小到大排列。68)查询订购了三种以上产品的订单号。
69)查询订购的产品至少包含了订单10003中所订购产品的订单。
70)在sales表中查找出订单金额大于“E0013业务员在1996/11/10这天所接每一张订单的金额”的所有订单,并显示承接这些订单的业务员和该订单的金额。
71)查询末承接业务的员工的信息。
72)查询来自上海市的客户的姓名,电话、订单号及订单金额。
73)查询每位业务员各个月的业绩,并按业务员编号、月份降序排序。
74)求每种产品的总销售数量及总销售金额,要求显示出产品编号、产品名称,总数量及总
金额,并按产品号从小到大排列。
75)查询总订购金额超过?C0002?客户的总订购金额的客户号,客户名及其住址。
76)查询业绩最好的的业务员号、业务员名及其总销售金额。
77)查询每位客户所订购的每种产品的详细清单,要求显示出客户号,客户名,产品号,产品名,数量及单价。
78)求各部门的平均薪水,要求按平均薪水从小到大排序。
1、查找表中所有姓刘的职工的工号,部门,薪水
selectemp_no,emp_name,dept,salary
fromemployee
whereemp_name like '刘%'
2、查找所有定单金额高于20000的所有客户编号
selectcust_id
from sales
wheretot_amt>20000
3、查找出职称为经理和职称为职员的女员工的姓名、职称、性别。selectemp_name,title,sex
fromemployee
wheretitle='经理'ortitle='职员'and sex='F'
4、选取销售数量最多的前5条订单订单号、数量。
select top5 with tiesorder_no,sup_id,qty
fromsale_item
order byqty desc
5、按部门进行汇总,统计每个部门的总工资
selectdept,sum(salary)
fromemployee
group bydept
6、由employee表中查找出薪水最高的员工信息。
select *
fromemployee
wheresalary=
(selectmax(salary )
fromemployee )
7、由sales表中查找出订单金额大于“E0013业务员在1996/10/15这天所接每一张订单的金额”的所有订单。
select *
from sales
wheretot_amt>all
(selecttot_amt
fromsales
wheresale_id='E0013'andorder_date='1996/10/15')
order bytot_amt
8、检索product表和sale_item表中数量大于2的相同产品的产品编号、产品名称、数量、单价。
selecta.prod_id,a.qty,a.unit_price,b.prod_name
fromsale_item as a innerjoin product as b /*如果改成leftjoin/rightjoin 试分析结果*/
on (a.prod_id=b.pro_id)anda.qty>2
order bya.prod_id
9、查找出employee表中住址相同的员工的姓名、性别、职称、薪水、住址。selecta.emp_name,a.sex,a.title,a.salary,a.addr,b.emp_name,b.sex,b.title,b.salary,b .addr
fromemployee as a innerjoin employee as b
on(a.emp_no!=b.emp_no) and(a.emp_name>b.emp_name) and (a.addr=b.addr) 10、查找所有经理的姓名、职称、薪水。
selectemp_name,title,salary
fromemployee
wheretitle=?经理?
11、查找出姓“王”并且姓名的最后一个字为“功”的员工。
select*
fromemployee
whereemp_namelike '王%功'
12、查找住在上海或北京的女员工,并显示其姓名、所属部门、职称、住址。selectemp_name,dept title,addr
fromemployee
wheresex='F'and (addr like '北京%' or addr like '上海%')
13、在表sales中挑出销售金额大于等于10000元订单。
select*from sales
wheretot_amt>=10000
14、选取订单金额最高的前10%的订单数据。
selecttop10 percent *
fromsales
orderbytot_amt desc
15、查找出职称为经理或职称为职员的女员工的信息。
select*from employee
wheresex='F'and ( title='经理' or title='职员')
16、删除sales表中作废的订单(其发票号码为I000000004)。deletefromsale_item
whereorder_noin
(select order_no
from sales
whereinvoice_no='I000000004')
17、计算出一共销售了几种产品。
selectcount(distinctprod_id)
fromsale_item
18、显示sale_item表中每种产品的订购金额总和,并且依据销售金额由大到小排列来显示出每一种产品的排行榜。
selectprod_id,sum(unit_price*qty)
fromsale_item
groupbyprod_id
orderbysum(unit_price*qty) desc
19、计算每一产品每月的销售金额总和,并将结果按销售(月份,产品编号)排序。
selectprod_id,month(order_date),sum(unit_price*qty)
fromsale_item
groupbyprod_id,month(order_date)
orderbyprod_id,month(order_date)
selectprod_id,month(order_date),sum(unit_price*qty)
fromsale_item
groupby prod_id,month(order_date)
orderby month(order_date),prod_id
20、由sales表中查找出销售金额最高的订单。
select*
fromsales
wheretot_amt=
(select max(tot_amt)
from sales)
21、由sales表中查找出订单金额大于“E0013业务员在1996/10/15这天所接任一张订单的金额”的所有订单,并显示承接这些订单的业务员和该条订单的金额。selectsale_id,tot_amt
fromsales
wheretot_amt> all
(select tot_amt
from sales
where sale_id='E0013' and order_date='1996-10-15')
22、找出公司女业务员所接的订单。
select*
fromsales
wheresale_idin
(select emp_no
from employee
where sex='f')
23、找出公司中姓名相同的员工,并且依据员工编号排序相识这些员工信息。select*
fromemployeea
whereexists
(select emp_name
from employee b
where a.emp_name=b.emp_name and a.emp_no<> b.emp_no)
orderbyemp_name
24、找出目前业绩未超过2000元的员工。
select*
fromemployee
whereemp_noin
(select sale_id
from sales
group by sale_id
having sum(tot_amt)>2000)
25、计算公司内各个部门的工资支出总和。
selectdept,sum(salary)
fromemployee
groupbydept
26、计算每一产品销售数量总和与平均销售单价。
selectprod_id,sum(unit_price*qty),avg(unit_price*qty)
fromsale_item
groupbyprod_id
27、查找出employee表中部门相同且住址相同的女员工的姓名、性别、职称、薪水、住址。
Selecta.emp_name,a.sex,a.title,a.salary,a.addr
fromemployeeas a inner join employee as b
on(a.dept=b.dept)and(a.addr=b.addr)
wherea.sex='F'andb.sex='F' and a.emp_no<>b.emp_no
Selecta.emp_name,a.sex,a.title,a.salary,a.addr
fromemployee a , employee b
where(a.dept=b.dept) and (a.addr=b.addr)
anda.sex='F'and b.sex='F' anda.emp_no<>b.emp_no
28、检索product 表和sale_item表中相同产品的产品编号、产品名称、数量、单价。
selecta.prod_id,b.prod_name,a.qty,a.unit_price
fromsale_itema, product b
wherea.prod_id=b.prod_id
/*将连接设为右连接*/
selecta.prod_id,b.prod_name,a.qty,a.unit_price
fromsale_itema, product b
wherea.prod_id=* b.prod_id
selecta.prod_id,b.prod_name,a.qty,a.unit_price
fromsale_itemas a right outer join product as b
on(a.prod_id=b.prod_id)
29、检索product 表和sale_item表中单价高于2400元的相同产品的产品编号、产品名称、数量、单价。
selectproduct.prod_id,product.prod_name,qty,unit_price
fromproduct,sale_item
where unit_price>2400 and
product.prod_id=sale_item.prod_id
orderbysale_item.prod_id
30、查找员工的编号、姓名、部门和出生日期,如果出生日期为空值,
--显示日期不详,并按部门排序输出,日期格式为yyyy-mm-dd。
selectemp_no ,emp_name ,dept ,isnull(convert(char(10),birthday,120),'日期不详') birthday
fromemployee
orderby dept
31、查找与喻自强在同一个单位的员工姓名、性别、部门和职称
selectemp_no,emp_name,dept,title
fromemployee
whereemp_name<>'喻自强' and dept in
(selectdept from employee
whereemp_name='喻自强')
32、按部门进行汇总,统计每个部门的总工资
selectdept,sum(salary)
fromemployee
groupby dept
33、查找商品名称为14寸显示器商品的销售情况,
--显示该商品的编号、销售数量、单价和金额
selecta.prod_id,qty,unit_price,unit_price*qty totprice
fromsale_item a,product b
wherea.prod_id=b.prod_id and prod_name='14寸显示器'
34、在销售明细表中按产品编号进行汇总,统计每种产品的销售数量和金额selectprod_id,sum(qty) totqty,sum(qty*unit_price) totprice
from sale_item
group byprod_id
35、使用convert函数按客户编号统计每个客户1996年的订单总金额selectcust_id,sum(tot_amt) totprice
from sales
whereconvert(char(4),order_date,120)='1996'
group bycust_id
36、查找有销售记录的客户编号、名称和订单总额
selecta.cust_id,cust_name,sum(tot_amt) totprice
from customera,sales b
wherea.cust_id=b.cust_id
group bya.cust_id,cust_name
37、查找在1997年中有销售记录的客户编号、名称和订单总额
selecta.cust_id,cust_name,sum(tot_amt) totprice
from customera,sales b
wherea.cust_id=b.cust_id and convert(char(4),order_date,120)='1997'
group bya.cust_id,cust_name
38、查找一次销售最大的销售记录
selectorder_no,cust_id,sale_id,tot_amt
from sales
where tot_amt=
(selectmax(tot_amt)
from sales)
39、查找至少有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
40、用存在量词查找没有订货记录的客户名称
select cust_name
from customer a
where not exists
(select *
from sales b
where a.cust_id=b.cust_id)
41、使用左外连接查找每个客户的客户编号、名称、订货日期、订单金额
--订货日期不要显示时间,日期格式为yyyy-mm-dd
--按客户编号排序,同一客户再按订单降序排序输出
selecta.cust_id,cust_name,convert(char(10),order_date,120),tot_amt
from customer a left outer join sales b ona.cust_id=b.cust_id
order by a.cust_id,tot_amt desc
42、查找16MDRAM的销售情况,要求显示相应的销售员的姓名、
--性别,销售日期、销售数量和金额,其中性别用男、女表示
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' andd.pro_id=c.prod_id and
emp_no=b.sale_id and b.order_no=c.order_no
43、查找每个人的销售记录,要求显示销售员的编号、姓名、性别、
--产品名称、数量、单价、金额和销售日期
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 ona.emp_no=b.sale_id , sale_item c,product d
where d.pro_id=c.prod_id andb.order_no=c.order_no
44、查找销售金额最大的客户名称和总货款
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
45、查找销售总额少于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
46、查找至少销售了3种商品的客户编号、客户名称、商品编号、商品名称、数量和金额
selecta.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_idand
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 )
47、查找至少与世界技术开发公司销售相同的客户编号、名称和商品编号、商品名称、数量和金额
selecta.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_idand
c.order_no=
d.order_no and not exists
(select f.*
from customer x ,sales e, sale_item f
where cust_name='世界技术开发公司' andx.cust_id=e.cust_id and
order_no=f.order_no and not exists
( select g.*
from sale_item g, sales h
where g.prod_id = f.prod_id andg.order_no=h.order_no and
h.cust_id=a.cust_id)
)
48、查找表中所有姓刘的职工的工号,部门,薪水
select emp_no,emp_name,dept,salary
from employee
where emp_name like '刘%'
49、查找所有定单金额高于20000的所有客户编号
select cust_id
from sales
where tot_amt>20000
50、统计表中员工的薪水在40000-60000之间的人数
select count(*)as 人数
from employee
where salary between 40000 and 60000
51、查询表中的同一部门的职工的平均工资,但只查询"住址"是"上海市"的员工
select avg(salary) avg_sal,dept
from employee
where addr like '上海市%'
group by dept
52、将表中住址为"上海市"的员工住址改为"北京市"
update employee
set addr like '北京市'
where addr like '上海市'
53、查找业务部或会计部的女员工的基本信息。
select emp_no,emp_name,dept
from employee
where sex='F'and dept in ('业务','会计')
54、显示每种产品的销售金额总和,并依销售金额由大到小输出。
select prod_id ,sum(qty*unit_price)
from sale_item
group by prod_id
order by sum(qty*unit_price) desc
55、选取编号界于…C0001?和…C0004?的客户编号、客户名称、客户地址。select CUST_ID,cust_name,addr
from customer
where cust_id between 'C0001' AND 'C0004'
56、计算出一共销售了几种产品。
select count(distinct prod_id) as '共销售产品数'
from sale_item
57、将业务部员工的薪水上调3%。
update employee
set salary=salary*1.03
where dept='业务'
58、由employee表中查找出薪水最低的员工信息。
select *
from employee
where salary=
(select min(salary )
from employee )
59、使用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 '客户丙'
60、由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
61、计算'P0001'产品的平均销售单价
select avg(unit_price)
from sale_item
where prod_id='P0001'
62、找出公司女员工所接的定单
select sale_id,tot_amt
from sales
where sale_id in
(select sale_id from employee
where sex='F')
63、找出同一天进入公司服务的员工
select a.emp_no,a.emp_name,a.date_hired
from employee a
join employee b
on (a.emp_no!=b.emp_no anda.date_hired=b.date_hired)
order by a.date_hired
64、找出目前业绩超过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)
65、查询出employee表中所有女职工的平均工资和住址在"上海市"的所有女职工的平均工资
select avg(salary)
from employee
where sex like 'f'
union
select avg(salary)
from employee
where sex like 'f' and addr like '上海市%'
66、在employee表中查询薪水超过员工平均薪水的员工信息。
Select * from employee where salary>(selectavg(salary) from employee)
67、找出目前销售业绩超过40000元的业务员编号及销售业绩,并按销售业绩从大到小排序。
Select sale_id ,sum(tot_amt)
from sales
group by sale_id
having sum(tot_amt)>40000
order by sum(tot_amt) desc
68、找出公司男业务员所接且订单金额超过2000元的订单号及订单金额。Select order_no,tot_amt
From sales ,employee
Where sale_id=emp_no and sex='M' and tot_amt>2000
69、查询sales表中订单金额最高的订单号及订单金额。
Select order_no,tot_amt from sales where tot_amt=(selectmax(tot_amt) from sales)
70、查询在每张订单中订购金额超过24000元的客户名及其地址。
Select cust_name,addr from customer a,sales b wherea.cust_id=b.cust_id and tot_amt>24000
71、求出每位客户的总订购金额,显示出客户号及总订购金额,并按总订购金额降序排列。
Select cust_id,sum(tot_amt) from sales
Group by cust_id
Order by sum(tot_amt) desc
72、求每位客户订购的每种产品的总数量及平均单价,并按客户号,产品号从小到大排列。
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
73、查询订购了三种以上产品的订单号。
Select order_no from sale_item
Group by order_no
Having count(*)>3
74、查询订购的产品至少包含了订单10003中所订购产品的订单。
Select distinctorder_no
From sale_item a
Where order_no<>'10003'and notexists (
Select * fromsale_item b where order_no ='10003' andnot exists
(select * fromsale_item c where c.order_no=a.order_no and
c.prod_id=b.prod_id))
75、在sales表中查找出订单金额大于“E0013业务员在1996/11/10这天所接每一张订单的金额”的所有订单,并显示承接这些订单的业务员和该订单的金额。Select sale_id,tot_amt from sales
where tot_amt>all(select tot_amt from sales wheresale_id='E0013' and order_date='1996/11/10')
76、查询末承接业务的员工的信息。
Select *
From employee a
Where not exists
(select * from salesb where a.emp_no=b.sale_id)
77、查询来自上海市的客户的姓名,电话、订单号及订单金额。
Select cust_name,tel_no,order_no,tot_amt
From customer a ,sales b
Where a.cust_id=b.cust_id and addr='上海市'
78、查询每位业务员各个月的业绩,并按业务员编号、月份降序排序。
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
79、求每种产品的总销售数量及总销售金额,要求显示出产品编号、产品名称,总数量及总金额,并按产品号从小到大排列。
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
80、查询总订购金额超过?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'))
81、查询业绩最好的的业务员号、业务员名及其总销售金额。
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)
82、查询每位客户所订购的每种产品的详细清单,要求显示出客户号,客户名,产品号,产品名,数量及单价。
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 andc.prod_id=d.prod_id 83、求各部门的平均薪水,要求按平均薪水从小到大排序。
select dept,avg(salary) from employee group by dept order byavg(salary)
四、游标问题(表结构同上例)
1、利用游标选取业务部门的员工编号和姓名,并执行游标。
2、利用游标查找所有女业务员的基本情况。
3、创建一游标,逐行显示表customer.的记录,并且用WHILE结构来测试游标的函数@@FETCH_STA TUS的返回值
五、触发器问题
1、设置一个触发器,该触发器仅允许“dbo”用户可以删除employee表内数据的,否则出错。
2、设置一个针对employee表的触发器,当有人操作该列值时,触发器将自动将该操作者的名称和操作时间记录在一张表内,以便追踪。
3、级联更新:当更新employee表中emp_no列的值时,同时更新sales表中的sale_id列的值,并且一次只能更新一行。
4、对employee表写一个允许用户一次只删除一条记录的触发器。
5、对employee表写一个UPDA TE触发器。当修改employee表的生日和雇佣日期时必须保证出生日期在雇佣日期之前,且年龄不小于25岁雇佣日期与出生日期必须间隔在20年之上
6、请使用游标和循环语句为sale_item表建立一个更新触发器updateSaleItem,当修改销售明细表中某个货品的数量或单价时自动修改销售主表中的相应定单的定单金额。(*)
六、SQL语句(2)
表结构如下:
Student(SNO,Sname,Sage,Ssex) 学生表
Course(CNO,Cname,TNO) 课程表
SC(SNO,CNO,score) 成绩表
Teacher(TNO,Tname) 教师表
建立这四张表的结构然后完成下面的问题。
问题:
1、查询“001”课程比“002”课程成绩高的所有学生的学号;
2、查询平均成绩大于60分的同学的学号和平均成绩;
3、查询所有同学的学号、姓名、选课数、总成绩;
4、查询姓“李”的老师的个数;
5、查询没学过“叶平”老师课的同学的学号、姓名;
6、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名;
7、查询学过“叶平”老师所教的所有课的同学的学号、姓名;
8、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名;
9、查询所有课程成绩小于60分的同学的学号、姓名;
10、查询没有学全所有课的同学的学号、姓名;
11、查询至少有一门课与学号为“1001”的同学所学相同的同学的学号和姓名;
12、查询至少学过学号为“001”同学所有一门课的其他同学学号和姓名;
13、把“SC”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩;
14、查询和“1002”号的同学学习的课程完全相同的其他同学学号和姓名;
15、删除学习“叶平”老师课的SC表记录;
16、向SC表中插入一些记录,这些记录要求符合以下条件:没有上过编号“003”课程的同学学号、2号课的平均成绩;
17、按平均成绩从高到低显示所有学生的“数据库”、“企业管理”、“英语”三门的课程成绩,按如下形式显示:学生ID,数据库,企业管理,英语,有效课程数,有效平均分
18、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分
19、按各科平均成绩从低到高和及格率的百分数从高到低顺序查询
20、查询如下课程平均成绩和及格率的百分数(用"1行"显示): 企业管理(001),马克思(002),OO&UML (003),数据库(004)
21、查询不同老师所教不同课程平均分从高到低显示
22、查询如下课程成绩第3名到第6名的学生成绩单:企业管理(001),马克思(002),UML (003),数据库(004)[学生ID],[学生姓名],企业管理,马克思,UML,数据库,平均成绩
23、统计列印各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60]
24、查询学生平均成绩及其名次
25、查询各科成绩前三名的记录:(不考虑成绩并列情况)
26、查询每门课程被选修的学生数
27、查询出只选修了一门课程的全部学生的学号和姓名
28、查询男生、女生人数
29、查询姓“张”的学生名单
30、查询同名同性学生名单,并统计同名人数
31、1981年出生的学生名单(注:Student表中Sage列的类型是datetime)
32、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列
33、查询平均成绩大于85的所有学生的学号、姓名和平均成绩
34、查询课程名称为“数据库”,且分数低于60的学生姓名和分数
35、查询所有学生的选课情况;