')
(41) 查询其他系中比信息系所有学生年龄都小的学生姓名及年龄。分别用
ALL谓词和集函数
----用ALL select sname,age from student where age
(42) 查询所有选修了1号课程的学生姓名。(分别用嵌套查询和连查询)
----嵌套查询 select sname from student where sno in (select sno from cs where cno=1) ----连接查询 select sname from student,cs where student.sno=cs.sno and cs.cno=1
(43) 查询没有选修1号课程的学生姓名。
select sname from student where sno in (select sno from cs where cno!=1)
(44) 查询选修了全部课程的学生姓名。
select sname from student where not exists (select * from course where not exists (select * from cs where cs.sno=student.sno and cs.cno=course.cno))
(45) 查询至少选修了学生95002选修的全部课程的学生号码。
select distinct sno from sc scx where not exists (select * from cs scy where scy.sno='95002' and not exists (select * from sc scz where scz.sno=scx.sno and scz.cno=scy.cno))
(46) 查询计算机科学系的学生及年龄不大于19岁的学生的信息。
select * from student where dept='计算机科学系' or age<19
(47) 查询选修了课程1或者选修了课程2的学生的信息。
select student.* from student,cs where student.sno = cs.sno and (cs.cno=1 or cs.cno=2)
(48) 查询计算机科学系中年龄不大于19岁的学生的信息。
select * from student where age<=19 and dept='计算机科学系'
(49) 查询既选修了课程1又选修了课程2的学生的信息。
select * from student where sno in( select sno from cs where cno='003' and sno in( select sno from cs where cno='004' )) ----用exists查询 select * from student where exists ( select * from cs where student.sno=cs.sno and cno='003' and sno in( select sno from cs where cno='004' ))
(50) 查询计算机科学系的学生与年龄不大于19岁的学生的差集。
select *from student where dept='计算机科学系' and age>19
(51) 通过查询求学号为1学生的总分和平均分。
select sum(cj) as '总分',avg(cj)'平均分' from cs where sno=1
(52) 求出每个系的学生数量
select dept,count(sno) as '学生个数' from student group by dept
(53) 查询平均成绩大于85的学生学号及平均成绩。
select sno,avg(cj) from cs group by sno having avg(cj)>85
(54) 要求查寻学生的所有信息,并且查询的信息按照年龄由高到低排序,如果年龄相等,则按照学号从低到高排序
select * from student order by age desc,sno asc
1.在SELECT语句中DISTINCT、ORDER BY、GROUP BY和HAVING子句的功能各是什么?
答 各子句的功能如下。
DISTINCT:查询唯一结果。
ORDER BY:使查询结果有序显示。 GROUP BY:对查询结果进行分组。 HAVING:筛选分组结果。
2.在一个SELECT语句中,当WHERE子句、GROUP BY子句和HAVING子句同时出现在一个查询中时,SQL的执行顺序如何?
答 其执行顺序如下:
(1)执行WHERE子句,从表中选取行。 (2)由GROUP BY对选取的行进行分组。 (3)执行聚合函数。
(4)执行HAVING子句选取满足条件的分组。