1.Consider the following schema:
)l,e[a C Th0Students (sid: integer, sname: string)51Testing软件测试网 w"w%vy]
M%d]6S
Students_Taking_Courses (sid: integer, cid: integer, score: real)51Testing软件测试网cK`VT4T.LY}d
In the schema, (sid, cid) is the primary key for Students_Taking_Courses. Write the following queries in SQL.51Testing软件测试网]Io7Tq
&]vo'v+kK[0a.51Testing软件测试网)b!gO%BG,nz3~_
Find the names of all the students who have taken both course #3160 and course #2214.
,JUJ$X:{E3\0
dGtx4C5H
B0Solution 151Testing软件测试网#KL0yA;?mO2{
SELECT S.sname
^WL%CO"K0FROM Students S, Students_Taking_Courses T1, Students_Taking_Courses T2
ady,OkZ0WHERE S.sid=T1.sid AND S.sid=T2.sid AND T1.cid=3160 AND T2.cid=2214
8i?"iZN!t*zW0
h/yv ko.S*VN;}0Solution 2
*L/X;HqY.Ee/k0(SELECT S.sname
&I5A
MYN5H&Vs
X0FROM Students S, Students_Taking_Courses T51Testing软件测试网
Ne0{I9E7jLW9]
WHERE S.sid=T.sid AND T.cid=3160 )
'UWZ1?CO_
M0INTERSECT
,g J,ur8^@W"U+P0(SELECT *
6A,F6Y9p;p'}\m0FROM Students S, Students_Taking_Courses T
v-cY7I+?$g1C4f0WHERE S.sid=T.sid AND T.cid=2214 )51Testing软件测试网!~/vYrvv$m/TP
F^ws9f7{#S-nqD0b.51Testing软件测试网K#G-U0TWR
Find the names of all the students who have never taken any courses.
k#Ic?;U6]m;j0
dZT
aR+L9oaN0Solution 1
4^-`K N0g!a0SELECT S.sname
kF;d/[~-[(IV0FROM Students S51Testing软件测试网'}5MwZ8Aj
WHERE NOT EXISTS ( SELECT *FROM Students_Taking_Courses T51Testing软件测试网#\K2U+_-o
WHERE S.sid=T.sid )
yp"Jc;C3_&U"ex~A0 51Testing软件测试网"q6D K6J-sB}-H6F
Solution 251Testing软件测试网e;W8|s6IAt s
SELECT S.sname
T0DARv:~Xx q0FROM Students S
S*_ w6Ub_0WHERE S.sid NOT IN ( SELECT T.sid FROM Students_Taking_Courses T)
)h~j6j
Sm!@ C0 51Testing软件测试网7J%Me"A$D*e!w
c.51Testing软件测试网n [HWh3v5w}tf
P
Find the names of all the students who have no scores below 75, and display in an alphabetical order.51Testing软件测试网c:M+~j)S7Q JjQ
fW4l8s?'|1C0Solution 151Testing软件测试网?B7Z;YQ#]
SELECT S.sname51Testing软件测试网S?
h&f