将一个查询块嵌套在另一个查询块的WHERE子句或HAVING短语的条件中的查询称为嵌套查询。
涉及到多个表的查询,除了可以使用多表连接查询以外,还可以利用嵌套子查询来完成。
由于连接查询比较耗时,当表中记录数很大时,嵌套查询的效率可能会比连接查询快。
下面开始做题。
1查询选修平均分为60分(包括60分)以上的学生的各门课成绩,要求输出学号,姓名,课程名和成绩,并按学号升序排序。
select student.sno,sname,cname,grade
from student,sc,course
where student.sno=sc.sno and course.cno=sc.cno
group by student.sno
having avg(grade)>=60
order by student.sno ;
2查询同时选修了“数据库基础”和“计算机网络”两门课的学生的学号,姓名。
表结构如第一题。
select sno,sname
from student
where sno in
(select sno
from course,sc
where course.cno=sc.cno and cname='数据库基础'and sno in
(select sno
from course,sc
where course.cno=sc.cno and cname='计算机网络'));
3查询同时选修了“数据库基础”和“计算机网络”两门课的学生的学号,姓名。
表结构如第一题。
select *
from student
where sname!='王大力' and sdept in
(select distinct sdept
from student
where sname='王大力');
4SQL查询:查询选修了全部课程的学生的学号,姓名,系名。
表结构如第一题。
select sno,sname,sdept
from student
where not exists
(select *
from course
where not exists
(select *
from sc
where sno=student.sno and cno=course.cno));
5SQL查询:查询没有同时选修“计算机导论”和“计算机网络”两门课的学生的学号,姓名。
表结构如第一题。
select sno,sname
from student
where sno not in
(select sno
from sc,course
where sc.cno=course.cno and cname='计算机导论'and sno in
(select sno from sc,course where sc.cno=course.cno and cname='计算机网络'));
6查询每个学生成绩最高的成绩信息
有课程表,学生表,成绩表如下,请完成查询,输出成绩信息学号、课程号及成绩,最高分可能有多门成绩。
select *
from sc student
where grade=(select max(grade) from sc where sc.sno=student.sno);
7SQL查询:查询哪些客户从未有订单,输出客户编号和客户名称。
表结构如下:【注意order表,语句中写
order
】select customer_id,customer_name
from customer
where customer_id not in(select customer_id from`order`);
8找出至今没有人选修过的课程。
select cno,cname
from course
where cno not in
(select cno from sc);
9SQL查询:查询同时选修了c01,c02,c03课程学生,输出学号,姓名
表结构如第一题。
select distinct student.sno, sname
from student,sc
where student.sno = sc.sno
and student.sno in
(select sno from sc
where cno = 'c01'
and sno in
( select sno from sc
where cno = 'c02'
and sno in
(select sno from sc
where cno = 'c03'
)
)
) ;
10查询选修了c03课程的学生学号和姓名。
表结构如第八题。
select student.sno,sname
from student,sc,course
where student.sno=sc.sno and course.cno=sc.cno
and sc.cno='c03';
11SQL查询:查询没有被订购的商品,输出商品id,商品名称,类别id,按照类别排序。
相关表结构:
1、商品表:product
2、类别表:category
3、订单表:order
4、订单明细:order_detail
select product_id,product_name,category_id from product
where product.product_id not in(select order_detail.product_id from order_detail);
12SQL查询:查询“王杰”的客户信息,按客户编号排序。
客户表customer,结构如下:
员工表employee,结构如下:
select * from customer where sale_employee_id in
(select employee_id from employee
where employee_name='王杰')
order by customer_id;
13SQL查询:查询没有参加选课的学生,输出系名,学号,姓名,性别,按系名(升序),学号(升序)排序。
表结构如第一题。
select sdept,student.sno,sname,ssex
from student left join sc on
student.sno=sc.sno
where sc.cno is null
order by sdept ,sno;
14查询超过该课程平均分的成绩信息
有课程表,学生表,成绩表如下,查询超过该课程平均分的成绩信息,输出学号,课程号及成绩。
表结构如第八题。
select sno,sc.cno,grade from sc,(
select cno,avg(grade) as avggrade
from sc
group by cno)as avg_sc
where sc.cno=avg_sc.cno
and grade>avggrade;
15查询选修了学号为9521102同学选修的全部课程的学生学号和姓名
表结构如第八题。
select sno,sname from student
where sno in
(select sno from sc
where cno in
(select cno from sc
where sno='9521102'));
16查询选课门数最多的学生的学号和姓名
有课程表,学生表,成绩表如下,查询选课门数最多的学生的学号和姓名,结果可能不只一行。
表结构如第八题。
select student.sno,student.sname
from student
where student.sno in
(select sc.sno from sc
group by sc.sno
having count(sc.cno)=
(select count(sc.cno)
from sc
group by sc.sno
order by count(sc.cno) desc limit 1));
17查询所有人都选修了的课程号与课程名。
表结构如第八题。
select course.cno,cname
from course where cno in(
select distinct cno from sc
group by cno
having count(*)=(select count(*) from student));
18查询每个男生的选课门数(要求用嵌套查询实现),要求输出学号、选课门数,并按序号升序排序。
表结构如第一题。
select sc.sno,count(*)
from sc
where sno in(select sno from student where ssex='男')
group by sno;
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)