SQL查询习题及答案
- 格式:doc
- 大小:62.50 KB
- 文档页数:9
36.设教学用的四个基本表(S,C,T,SC)
(2)查询年龄大于23岁的女同学的学号和姓名
select 姓名,学号
from s
where 性别='女' and 年龄>23
(3)查询至少选修了刘老师所讲授课程中的一门课程的女同学姓名
select s.姓名
from s,sc
where s.性别='女'and s.学号=sc.学号 and sc.课程编号 in
(select c.课程编号
from c,t
where c.教师编号=t.教师编号 and t.姓名='刘%'
)
(4)查询至少选修了2门课程的学生学号
select sc.学号
from sc
group by 学号
having count(课程编号)>2
(5)查询全部学生都选修的课程号与课程名
select c.课程编号,c.课程名称
from c,sc
where sc.课程编号=c.课程编号 and sc.学号=
(select distinct s.学号
from s
)
(6)计算机系每个教师讲授的课程号
select t.教师编号,课程编号
from c,t
where t.所在系='计算机系
(7)查询没有选修过任何一门课程的学生的学号
select s.学号
from s
where s.学号 not in
(select distinct sc.学号
from sc
)
(10)统计个系教师的人数
select count (教师编号)
from t group by 所在系
(11)统计出教师人数超过10人的系的名称
select t.所在系
from t
group by 所在系
having count(教师编号)>10
(12)在选课表SC中查询成绩为NULL的学生的学号和课程号
select 学号 课程编号
from sc
where 成绩='NULL'
(13)姓王的同学的年龄、姓名、选课名称、成绩
select 年龄,姓名,课程名称,成绩
from s,c,sc
where s.学号=sc.学号 and c.课程编号=sc.课程编号 and s.姓名='王%'
(14)查询年龄大于女同学平均年龄的男同学姓名和年龄
select 姓名,年龄
from s
where 性别='男' and 年龄>
(select avg(年龄)
from s
where 性别='女'
)
37.在数据库{USER、ORDER}中,用户需要查询“所有于2009年5月25日下订单的女顾客姓名”。
(1)试写出该查询的关系代数表达式。
Q1=ΠUserName(σO.DateCreated <’2009-5-25’ AND U.Sex
单表查询
3.9输出图书表的所有内容
Select *
Form 书籍表
3.10输出图书表中的ISBN、书名、作者等信息 select 书号,书名,作者
from 书籍表
3.11 输出用户表中用户的真实姓名、性别、出生年月
select 性别,真实姓名,出生年月
from 用户表
3.12查询得到每本书的ISBN、书名、折扣价格及折扣数
select 书号,书名,价格,折扣价格,(价格-折扣价格)/价格
from 书籍表
3.13选择用户表的女性用户
select *
from 用户表
where 性别='女'
3.14选择用户表中1970-1979年的男性用户的信息
select *
from 用户表
where 性别='男' and 出生年月 between '1970-1-1' and '1979-12-31'
3.15在订单表中查找出“待支付”,“已支付”的订单
select *
from 订单表
where 支付方式 ='待支付' or 订单状态='已支付'
3.16查找所有西安的用户
select 用户编码
from 用户表
where 地址 like '%西安%'
3.17查找用户表中邮编不为空的用户信息
select *
from 用户表
where 邮编 is not null
3.18找出施伯乐编写有关数据库的书
elect *
from 书籍表
where 作者='施伯乐' and 书名 like'%数据库%'
连接查询
3.20查询图书的书名、ISBN号及出版社信息
select b.书名,b.书号,p.*
from 书籍表 b,出版社信息表 p
where b.出版社编号=p.出版社编号
3.21查询“数据库系统基础教程”书的ISBN号和订单号及订购量
select b.书号,od.订单编号,od.数量
from 书籍表 b,订单细节表 od
where b.书籍编码=od.书籍编码 and 书名 = '数据库系统基础教程'
3.22查询至少有2个订单订购的图书的书号
方法一:
select b.书号 from 书籍表 b
where 书籍编码 in
(
select od.书籍编码
from 订单细节表 od
group by od.书籍编码
having count(订单编号)>=2)
3.23查询至少有2个订单订的书的基本信息
select *
from 书籍表 b
where 书籍编码 in
(
select distinct a.书籍编码
from 订单细节表 a,订单细节表 b
where a.书籍编码=b.书籍编码 and a.订单编号!=b.订单编号)
3.24查询出清华大学出版社所出版的图书的订单号
select distinct od.订单编号
from 书籍表 b,订单细节表 od,出版社信息表 p
where b.书籍编码=od.书籍编码 and b.出版社编号=p.出版社编号
and 出版社名称 like '%清华大学%'
嵌套查询
选择由中国铁道出版社所出版图书的名称
select 书名
from 书籍表
where 出版社编号 in
(
select 出版社编号
from 出版社信息表
where 出版社名称 like '%中国铁道%')
3.25查找出比计算机类的书库存量都大的图书的详细信息
方法一:
select *
from 书籍表 b
where b.数量 >ALL
(
select b.数量
from 书籍表 b
where b.类别编号='TP')
方法二:
select *
from 书籍表 b
where b.数量 >
(
select max(b.数量) from 书籍表 b
where b.类别编号='TP')
3.26查找出其他类的图书至少比计算机类的某一本书价钱高的图书的详细信息
方法一:
select *
from 书籍表 b
where b.类别编号!='TP 'and b.价格 >any
(
select b.价格
from 书籍表 b
where b.类别编号='TP')
方法二:
select *
from 书籍表 b
where b.类别编号!='TP' and b.价格 >
(
select min(b.价格)
from 书籍表 b
where b.类别编号='TP')
3.27查找出没有销售记录的书
select b.书籍编码,b.书名
from 书籍表 b
where b.书籍编码 not in
(
select od.书籍编码
from 订单细节表 od)
3.28查询清华大学出版社所出版的图书的订单号
select distinct od.订单编号
from 订单细节表 od
where od.书籍编码 in
(
select b.书籍编码
from 出版社信息表 p,书籍表 b
where b.出版社编号=p.出版社编号 and 出版社名称 like '%清华大学%')
3.29查询出收货人李华所收到的图书号及订货人姓名
select od.书籍编码,u.真实姓名
from 订单细节表 od,用户表 u
where od.订单编号 in
(
select o.订单编号
from 订单表 o
where u.用户编码=o.用户编号 and 收货人姓名='李华')
3.30查找出没有销售记录的书
select *