1. 程式人生 > >用SQL語言進行復雜查詢:對各表中的資料進行不同條件的連線查詢和巢狀查詢: 1)查詢每個學生及其選課情況; 2)查詢每門課的間接先修課

用SQL語言進行復雜查詢:對各表中的資料進行不同條件的連線查詢和巢狀查詢: 1)查詢每個學生及其選課情況; 2)查詢每門課的間接先修課

對各表中的資料進行不同條件的連線查詢和巢狀查詢:
1)查詢每個學生及其選課情況;
2)查詢每門課的間接先修課
3)將STUDENT,SC進行右連線
4)查詢有不及格的學生姓名和所在系
5)查詢所有成績為優秀(大於90分)的學生姓名
6)查詢既選修了2號課程又選修了3號課程的學生姓名、學號;
7)查詢和劉晨同一年齡的學生
8)選修了課程名為“資料庫”的學生姓名和年齡
9)查詢其他系比IS系任一學生年齡小的學生名單
10)查詢其他系中比IS系所有學生年齡都小的學生名單
11)查詢選修了全部課程的學生姓名
12)查詢計算機系學生及其性別是男的學生
13)查詢選修課程1的學生集合和選修2號課程學生集合的差
14)查詢李麗同學不學的課程的課程號
15)查詢選修了3號課程的學生平均年齡
16)求每門課程學生的平均成績
17)統計每門課程的學生選修人數(超過3人的才統計)。要求輸出課程號和選修人數,結果按人數降序排列,若人數相同,按課程號升序排列
18)查詢學號比劉晨大,而年齡比他小的學生姓名。
19)求年齡大於女同學平均年齡的男同學姓名和年齡
20)求年齡大於所有女同學年齡的男同學姓名和年齡
21)查詢至少選修了95002選修的全部課程的學生號碼

22)查詢95001和95002兩個學生都選修的課程的資訊

1.
SELECT  student.*,cno,grade
from student,sc
where student.sno=sc.sno 
;
2.
SELECT course1.cno,course2.cpno
from course course1,course course2
where course1.cpno=course2.cno
;
3.
SELECT student.*,sc.*
from student right join sc
on student.sno=sc.sno
;
4.
SELECT sname,sdept
from student,sc
where student.sno=sc.sno and 
grade<60 
;
5.
SELECT sname
from student,sc
where student.sno=sc.sno
group by sname
having min(grade)>=90
;
6.
SELECT student.sno,student.sname
from student,sc
where student.sno=sc.sno 
and cno='002'and sc.sno in(SELECT sno
from sc
where cno='003')
;
7.
SELECT student.*
from student
where sage=(select sage from student where sname='劉晨') and sname<>'劉晨' 
;
8.
SELECT sname,sage
from student,sc,course
where student.sno=sc.sno and course.cno=sc.cno and cname='資料庫'
;
9.
SELECT student.*
from student
where sage<any(SELECT sage from student where sdept ='is') and sdept <>'is'
;
10.
SELECT student.*
from student
where sage<all(SELECT sage from student where sdept ='is') and sdept <>'is'
;
11.
select sname,sdept
from student
where not exists
(
select *from course
where not exists
(
select *
from sc
where sno=student.sno and cno=course.cno
)
)
12.
SELECT *
from student 
where sdept='is' and ssex='男'
;
13.
SELECT sno
from sc 
where cno='001'and sno in
(SELECT sno
from sc 
where cno<>'002')
;
14.
SELECT distinct cno
from sc
where cno not in
(select cno 
from student,sc
where student.sno=sc.sno and sname='李麗' )
;
15.
SELECT avg(sage)
from student,sc
where student.sno=sc.sno and cno='003'
;
16.
SELECT cno,avg(grade)
from sc
group by cno
;
17.
SELECT cno,count(sno)
from sc 
group by cno
having count(sno)>3
order by count(sno) asc,cno desc
;
18.
SELECT *
from student
where 
sno>(select sno from student where sname='劉晨') 
and sage<(select sage from student where sname='劉晨') 
;
19.
SELECT sname,sage
from student
where 
sage>(select avg(sage) from student where ssex='女') 
and ssex='男'
;
20.
SELECT sname,sage
from student
where 
sage>all(select sage from student where ssex='女') 
and ssex='男'
;
21.
select sno
from sc
where 
cno in (SELECT cno from sc where sno='08002') 
and sno <>'08002'
group by sno
having count(cno)=(select count(cno) from sc where sno="08002")
;
22.
SELECT course.cno,cname,cpno,credit
from sc,course 
where sc.cno=course.cno and
sno='08001' and
sc.cno in (select cno from sc where sno="08002")
;