公务员考试专用训练软件《公务员考试百宝箱》1.0版
对于下述3个关系:
s (s#, sn, sa, sex)
c (C#, cn, teacher)
sc (s#, c#, grade) ZT+0|"Ey~(1W!*(q*m [ 本 资 料 来 源 于 贵 州 学 习 网 公务员考试专业指导 http://Www.gzU521.com ] ZT+0|"Ey~(1W!*(q*m
试用sql的查询语句表达下列查询:
(1) 检索liu老师所授课程的课程号和课程名。
select c#, cn
from c
where teacher=’liu’
(2) 检索年龄大于23岁的男学生的学号和姓名。
select s#, sn
from s
where sa>23 and sex=’男’
(3) 检索学号为s3学生所学课程的课程名和任课老师。
方法一:
select cn, teacher
from c
where c# in
(select c#
from sc
where s#=’s3’)
方法二:
select cn, teacher
from c, sc
where c.c#=sc.c# and sc.s#=’s3’
方法三:
select cn, teacher
from c
where exists
(select *
from sc
where sc.c#=c.c# and s#=’s3’)
公务员考试专用训练软件《公务员考试百宝箱》1.0版
公务员考试专用训练软件《公务员考试百宝箱》1.0版
(6) 检索至少选修两门课程的学生学号。
方法一:
select distinct s1.s#
from sc as sc1, sc as sc2
where sc1.s#=sc2.s# and sc1.c#!=sc2.c#
方法二:
select s#
from sc
group by s#
having count(*)>=2
(7) 检索全部学生都选修的课程的课程号和课程名。
方法一:
select c#, cn
from c
where not exists
(select *
from s
where not exists
(select *
from sc
where sc.s#=s.s# and sc.c#=c.c#)
)
方法二:
select c#, cn
from c
where not exists
(select *
from s
where s# not in
(select s#
from sc
where sc.c#=c.c#)
方法三:
select c#, cn
from c
where c# in
(select c#
from sc
group by c#