MySql实验嵌套查询_数据库实验:SQL嵌套查询
自測題:
1、查詢哪些課程沒有人選修列出課程號和課程名;
[code]select cno,cname
from course
where cno not in(
select distinct cno
from sc)[/code]
2、用子查詢實現如下查詢:
(1)查詢選修了1號課程的學生姓名和所在系;
[code]select sname,sno
from student
where sno in(
select sno
from sc
where cno=1)[/code]
(2)查詢“數據庫”成績在80分以上的學生的學號和姓名;
[code]Select sno,sname
From student
Where sno in(
select sno
from course,sc
where course.cno=sc.cno and course.cname='數據庫' and grade>=80)[/code](3)查詢計算機系最高成績。
[code]select top 1 grade
from student,sc
where student.sno=sc.sno and sdept='CS'
order by grade desc[/code]
3、查詢同時選修了1號和2號課程的學生學號
[code]select sno
from sc
where cno=1 and sno in(
select sno
from sc
where cno=2)[/code]
4、查詢選修了“離散數學”的學生姓名(連接查詢)
[code]select sname
from student
where sno in(
select sno
from course,sc
where course.cno=sc.cno and course.cname='離散數學')[/code]
5、查詢選修課程名為“數據庫”的學生姓名(子查詢)
[code]select sname
from student
where sno in(
select sno
from course,sc
where course.cno=sc.cno and course.cname='數據庫')[/code]
6、查詢與張天和張琪在同一個系的學生
[code]select *
from student
where sdept in(
select sdept
from student
where sname='張天' or sname='張琪')[/code]
查詢與張天或張琪不在同一個系的學生
[code]select *
from student
where sdept not in(
select sdept
from student
where sname='張天' or sname='張琪')[/code]
7、查詢比信息系所有學生年齡大的學生姓名
[code]select sname
from student s1
where s1.sage>all(
select sage
from student s2
where s2.sdept='CS')[/code]
8、查詢比張天平均成績高的學生姓名
[code]select sname
from student
where student.sno in(
select sno
from sc
group by sno
having avg(grade) >(
select avg(grade) as avg_grade2
from sc sc2,student
where student.sno=sc2.sno and sname='劉晨'
group by sc2.sno)
)[/code]9、查詢比學號為200215121學生年齡大的學生
[code]select *
from student s1
where s1.sage>(
select sage
from student s2
where s2.sno='200215121')[/code]
10、查詢各系總分最高的學生學號
[code]Select sdept,student.sno
from student,sc
where student.sno=sc.sno
group by sdept,student.sno
having sum(grade)>=all(
select sum(grade)
from student,sc
where student.sno=sc.sno and sdept=student.sdept
group by student.sno)[/code]
11、查詢選修了以6號課程為先行課的所有課程的學生學號。
[code]select distinct sno
from sc
where sc.cno in(
select cno
from course
where cpno=6)[/code]
總結
以上是生活随笔為你收集整理的MySql实验嵌套查询_数据库实验:SQL嵌套查询的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 突然报错package name doe
- 下一篇: mysql 重复航_mysql