sql题目复习

题目一:

为管理岗位业务培训信息,建立3个表:
S (Sid,SN,SD,SA)    Sid,SN,SD,SA 分别代表学号、学员姓名、所属单位、学员年龄
C (Cid,CN,CTeacher)    Cid,CN 分别代表课程编号、课程名称、cteacher任课老师(任课老师列与题目一无关,主要为题目二所添加
SC ( Sid,Cid,G )    Sid,Cid,G 分别代表学号、所选修的课程编号、学习成绩

--1. 使用标准SQL嵌套语句查询选修课程名称为 税收基础 的学员学号和姓名
select sid,sn from tab_s where sid in (select sid from tab_sc where cid=(select cid from tab_c where cn='税收基础'))

--2. 使用标准SQL嵌套语句查询选修课程编号为’C2’的学员姓名和所属单位
select sn,sd from tab_s where sid in (select sid from tab_sc where cid=2)

--3. 使用标准SQL嵌套语句查询不选修课程编号为’C5’的学员姓名和所属单位
select sn,sd from tab_s where sid not in (select sid from tab_sc where cid=2)

--4. 使用标准SQL嵌套语句查询选修全部课程的学员姓名和所属单位(不会做,先看看第6题)
select sid,sd from tab_s
where sid in (select sid from tab_sc group by sid having count(sid)=(select count(*) from tab_c))

--5. 查询选修了课程的学员人数
select count(distinct sid) from tab_sc

--6. 查询选修课程超过5门的学员学号和所属单位
select sid,sd from tab_s
where sid in (select sid from tab_sc group by sid having count(sid)>5)


题目二:

--1. 找出没有选修过“英语老师”老师讲授课程的所有学生姓名
select sn from tab_s where sid not in(select distinct sid from tab_sc where cid=(select cid from tab_c where cteacher='英语老师'))

--2. 列出有二门以上(含两门)不及格课程的学生姓名及其平均成绩
--第一步(找出两门(含两门)不及格课程的学生id)
--select sid from tab_sc where g<60 group by sid having count(distinct cid)>=2
select tab_S.sid,tab_s.sn,AVG_SCGRADE=AVG(tab_sc.g)
from tab_s,tab_sc , 
(select sid from tab_sc where g<60 group by sid having count(distinct cid)>=2) A  
where tab_S.Sid=A.sid and tab_sc.Sid=A.sid
group by tab_s.sid,tab_s.sn

--3. 列出既学过“1”号课程,又学过“2”号课程的所有学生姓名
select sid,sn from tab_s
where sid in(
select sid from tab_sc
where cid in (1,2)
group by sid
having count(distinct cid)=2)

--4.列出“1”号课成绩比“2”号同学该门课成绩高的所有学生的学号





题目转载自:http://www.cnblogs.com/finejob/articles/974900.html

以上为自己整理、复习所做。答案仅供参考。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值