自动化测试框架爱好者,非培训机构、非托、非盈利,纯个人!软件自动化测试交流 请加我QQ 459864689!更多自动化测试的信息请访问我的个人小站www.zhushangyuan.cn,专门研究selenium自动化测试框架的分享站点。

几道经典的SQL语句题

上一篇 / 下一篇  2008-11-18 13:27:25 / 个人分类:数据库

1.Consider the following schema:

)l,e [aC Th0

Students (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[0

a.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

 

d Gtx4C5H B0

Solution 151Testing软件测试网#KL0yA;?mO2{

SELECT S.sname

^WL%C O"K0

FROM Students S, Students_Taking_Courses T1, Students_Taking_Courses T2

a dy,O kZ0

WHERE S.sid=T1.sid AND S.sid=T2.sid AND T1.cid=3160 AND T2.cid=2214

8i ?"iZN!t*zW0

 

h/yvko.S*VN;}0

Solution 2

*L/X;HqY.Ee/k0

(SELECT S.sname

&I5A M YN5H&Vs X0

FROM Students S, Students_Taking_Courses T51Testing软件测试网 Ne0{I9E7j LW9]

WHERE S.sid=T.sid AND T.cid=3160 )

'UWZ1?CO_ M0

INTERSECT

,g J,ur8^@W"U+P0

(SELECT *

6A,F6Y9p;p'}\m0

FROM Students S, Students_Taking_Courses T

v-cY7I+?$g1C4f0

WHERE S.sid=T.sid AND T.cid=2214 )51Testing软件测试网!~/vYrvv$m/TP

 

F^ws9f7{#S-nqD0

b.51Testing软件测试网K#G-U0T WR

Find the names of all the students who have never taken any courses.

k#Ic?;U6]m;j0

 

dZ T aR+L9oaN0

Solution 1

4^-`KN0g!a0

SELECT S.sname

kF;d/[~-[(IV0

FROM Students S51Testing软件测试网'}5Mw Z8Aj

WHERE NOT EXISTS ( SELECT *FROM Students_Taking_Courses T51Testing软件测试网#\K2U+_-o

WHERE S.sid=T.sid )

yp"J c;C3_&U"ex~A0

 51Testing软件测试网"q6D K6J-sB}-H6F

Solution 251Testing软件测试网e;W8|s6IAt s

SELECT S.sname

T0DARv:~Xx q0

FROM Students S

S*_ w6Ub_0

WHERE 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)S7QJjQ

 

fW4l8s?'|1C0

Solution 151Testing软件测试网?B7Z;YQ#]

SELECT S.sname51Testing软件测试网S? h&fO+qJc8X?

FROM Students S51Testing软件测试网^IK1v(Jm

WHERE NOT EXISTS ( SELECT *FROM Students_Taking_Courses T51Testing软件测试网h#@yM+Dpx@

WHERE S.sid=T.sid AND Score <75)

~s,kY'S0

ORDER BY S.sname

_#P:K(~jh#w0

 51Testing软件测试网'K8zN9P5YIZ

Solution 2

I]R0V2]:j}f0

SELECT S.sname51Testing软件测试网 @*X8w,l#~ T2j;u2Fq&l

FROM Students S51Testing软件测试网#a'[*ZpNI3y%u0v

WHERE S.sid NOT IN ( SELECT T.sid51Testing软件测试网0^.T%cmp-g"iY

FROM Students_Taking_Courses T

I wiC E!^X*Kl z0

WHERE Score <75)

{ _ V9J}^0

ORDER BY S.sname

kL vk Irl8B0

 51Testing软件测试网.x_ `w m

d.51Testing软件测试网P{b-o#L

Find the average score for all the students who have taken more than 3 courses.

Y[d.j7h$@ y%P,Q0

 

%^ d4mz5|j"d,i1C,S0

Solution

`.mCmk[0

SELECT S.sname, AVG(T.score) AS average51Testing软件测试网5[6^j'F0b'P

FROM Students S, Students_Taking_Courses T

Y;["N2sI0

WHERE S.sid=T.sid51Testing软件测试网.aPSWHO-N

GROUP BY S.sid, S.sname

8R(r%T&E}%a w0

HAVING COUNT(T.sid)>351Testing软件测试网!a9x&DkIi

 

!^HhfF!jry#w0

e.

E0d%uQ!K.y0

Find the grades of all courses taken by a student whose sid is 123, and display the sid and the cid.51Testing软件测试网(g0uL[4O+|q}

Solution51Testing软件测试网/nF%YSf!F

SELECT T.sid, T.cid, T.score51Testing软件测试网] d/~&EK+uh+a S

FROM Students_Taking_Courses T51Testing软件测试网 V j-s[.Zs7b LV

WHERE T.sid = 12351Testing软件测试网s+Czg kH3a

 51Testing软件测试网(R'w-h ? z%x x#\uP3C

f.

7o0j+Rr*v6M m#I0

Find the names of all the students who are on the honor roll (average of scores greater than 90),51Testing软件测试网Wr%UsYf+e*fub

and display in increasing alphabetical order by name.51Testing软件测试网} [HO;X#r5g

Solution51Testing软件测试网lTOX;Z)Q @w(wLY

SELECT S.sname51Testing软件测试网K xI/V6zp2I

FROM Students S, Students_Taking_Courses T51Testing软件测试网*cC8EIlS;i:fJ$r x

WHERE S.sid=T.sid51Testing软件测试网x"O{et8{w

GROUP BY S.sid,S.sname

d FR q`'?'B0

HAVING AVG(T.score)>90

oRbn$s0

ORDER BY S.sname51Testing软件测试网:HW%\?w tv

 

j#L#H+lC {t0

g.51Testing软件测试网 z.Zk3q8]

Find the course that has been taken by the most students.

OrY)V&{5xZ[0

Solution51Testing软件测试网8V5sd4?QoyX5g

SELECT Temp.cid, Temp.count51Testing软件测试网#ZrJm+~2Y8i2p^

FROM (SELECT COUNT(sid) AS count, T.cid AS cid

,b*R6h(tT3n Y0

FROM Students_Taking_Courses T51Testing软件测试网rP J{hF

GROUP BY T.cid) Temp

1sr y:G:m bh-L1i0

WHERE Temp.count = (SELECT MAX(Temp2.count)

j0Q!C$mm,ORy|!`0

FROM (SELECT COUNT(sid) AS count

4_BR6Q RaBE0

FROM Students_Taking_Courses T51Testing软件测试网1nwBy J9Oz

GROUP BY T.cid ) Temp2)

%uQ5R*mtw2o0

 

)A wz~K:^ax;g6XV3Z0

h.

*h+cM@'_p4g0

Find the names of all students who have failed more than one fifth of their classes (score < 70).51Testing软件测试网L3X Ep[,I{T

Solution1

,L["Hwxj0

SELECT S.sname51Testing软件测试网/rVj:](T8x}

FROM Students S, Students_Taking_Courses T51Testing软件测试网#urjkF o8i9Sr

WHERE T.score<70 AND S.sid=T.sid51Testing软件测试网)j[OzP mz

GROUP BY T.sid, S.sname51Testing软件测试网J:s#B_7n!bKy

HAVING count(cid) > 0.2*(SELECT COUNT(*) FROM Students_Taking_Courses T2 WHERE T2.sid=T.sid GROUP BY T.sid)

Xjl;\6W+RrC0

 51Testing软件测试网J\f(@I!Y

Solution2

9x%eCBD }?3kr6s&d0

SELECT S.sname
9AT G!oaz` d)X9k }0FROM Students S, Students_Taking_Courses T,Students_Taking_Courses T251Testing软件测试网&_UuoX6Zuu Dh!A9q]
WHERE T.score<70 AND S.sid=T.sid AND T2.sid=T.sid
/IoAnC0GROUP BY T.sid, S.sname
]8~p4w%L:W0HAVING count(T.cid) > 0.2*(COUNT(T2.cid))
51Testing软件测试网3a([3^@w$? vQ

i.51Testing软件测试网;U5h"HabJSKT

Find the average of all students taking 3160.

3DaI[-O m@.TT/[0

Solution

(xe dZk6K L0

SELECT AVG(T.score) AS average

0K _9|"wkCn.}1E6V~0

FROM Students_Taking_Courses T

ITE4ox0

WHERE T.cid=316051Testing软件测试网P+x;|6bD`H4TEW

 

{Ql#hj Y)_*T0

j.51Testing软件测试网v*r%ixy

Find the names of all students making grade below 60.51Testing软件测试网Qv.j&ZI M

Solution

DP'NnlI0W0

SELECT DISTINCT S.sname51Testing软件测试网Z(r!{5bb4]5`

FROM Students S, Students_Taking_Courses T51Testing软件测试网(vwYZ tnAh

WHERE T.score<60 AND S.sid=T.sid

3a,G8c@.x!UdP0

 51Testing软件测试网(\]0R;v4l_z#B

k.51Testing软件测试网N"HtqTo

Find the best performance class and the worst.

zUMEp.\/P0

Solution for the best class

1e#D,I!]W N wtN0

SELECT Temp.cid, Temp.average51Testing软件测试网oiS5JTH(x

FROM (SELECT AVG(T.score) AS average, T.cid

1zf4^7i*b)n K0u0

FROM Students_Taking_Courses T

0ow HF3N!J0

GROUP BY T.cid) Temp

H5\m0Qc7cJbk4y0

WHERE Temp.average = (SELECT MAX(Temp2.average)51Testing软件测试网^SEU%K4P?

FROM (SELECT AVG(T.score) AS average51Testing软件测试网5qM_ x eX

FROM Students_Taking_Courses T

-f9J"F'tg+?0

GROUP BY T.cid ) Temp2)51Testing软件测试网_ DD&AC O Uj.L

 

-GT}&C"_nF1~K0

Solution for the worst class

u8d2U W\N{9VyD0

SELECT Temp.cid, Temp.average

5~ ve'yTh]0[1\0

FROM (SELECT AVG(T.score) AS average, T.cid

#y7ggaN&C)Ua)p0

FROM Students_Taking_Courses T

6@H+I'k_W0

GROUP BY T.cid) Temp51Testing软件测试网 ]$n~ LdQA ?

WHERE Temp.average = (SELECT MIN(Temp2.average)51Testing软件测试网P {'Vfx"p

FROM (SELECT AVG(T.score) AS average51Testing软件测试网%l @,Z*Ud b

FROM Students_Taking_Courses T

3t y#{(g#E Nnh-S0

GROUP BY T.cid ) Temp2)51Testing软件测试网DG&p$gc*u'VN

l.51Testing软件测试网5^D.{!Lm4yW|v ?)hv

Find the total number of the course the university has provided.51Testing软件测试网gQ~$`?j#Rq.z

Solution51Testing软件测试网YopCdJ3D8U_

SELECT COUNT(DISTINCT T.cid)

4\'e%Q2n ?0

FROM Students_Taking_Courses T

+Y_sE;C2v\)a p0

 51Testing软件测试网M(S@0S+r(J_["Nj^

m.51Testing软件测试网]/s&X(n:](d1~\&e)v

Find the total number of the course that a student has taken for every student, and display in increasing order by students name.51Testing软件测试网%VJ5?cU

Solution51Testing软件测试网memv7AL

SELECT S.sname, COUNT(T.cid)

(iF?3?&O#KB0

FROM Students S, Students_Taking_Courses T51Testing软件测试网)Vfs/Hl}Z t

WHERE S.sid=T.sid

iT Hg'j;D6q0

GROUP BY S.sid, S.sname

.m0odE(RL?0

ORDER BY S.sname51Testing软件测试网&S9j*R)F0l7U1hYd3gS

 

@1EO,Sg+E$p2vG r0

 

3],wz%k7s T\0

附:oracle数据库下的例子:51Testing软件测试网g;\l"h2H

create table STUDENTS

PLhY^T#[0

(

yO0_H-w9mo0

 SID  NUMBER not null,51Testing软件测试网)S^ `kJq[

 SNAME VARCHAR2(10)51Testing软件测试网%NYo/s/s5e&W

);51Testing软件测试网^ y.kc|

alter table STUDENTS51Testing软件测试网-K.Y"S}zZ t){3si

 add constraint SIDCIDPK primary key (SID);51Testing软件测试网 d"e6B lx:n6lxW

create table STUDENTS_TAKING_COURSES

u:p7e;l @'a(h7A0

(

8W6cW8j x y0

 SID  NUMBER,

/D*W!}a c*D|0

 CID  NUMBER,51Testing软件测试网-B$dq*b-y$Lm7z"D7u

 SCORE VARCHAR2(5)

J+Y:SKd:f^0

);

"qY\b"a0

insert into STUDENTS (SID, SNAME)51Testing软件测试网&~j6r-Eu#T,b3P

values (1, 'zhushy');51Testing软件测试网O*["h!O2o/x!F

insert into STUDENTS (SID, SNAME)

O;u'N Bw;S7K8t0

values (2, 'zhushy2');51Testing软件测试网&e:Z*t'Gj

insert into STUDENTS (SID, SNAME)51Testing软件测试网zJxe3s-YJx @

values (3, 'zhushy3');

;p^:KW*U0

insert into STUDENTS (SID, SNAME)

/jS |FmzrY8C8f0

values (4, 'zhushy4');51Testing软件测试网_7lM ?+T ~3~

insert into STUDENTS (SID, SNAME)

vXAV5L!s@0

values (5, 'zhushy4');51Testing软件测试网&W%iF[9}+c$D

insert into STUDENTS_TAKING_COURSES (SID, CID, SCORE)

Ky&bK9Bf0

values (1, 1, '77');

#z-w%rHq)_ @"a6dw0

insert into STUDENTS_TAKING_COURSES (SID, CID, SCORE)51Testing软件测试网1C,s'@#MO-_6Q;n*f

values (2, 2, '62');51Testing软件测试网)|Z+[4} }y!l c7G

insert into STUDENTS_TAKING_COURSES (SID, CID, SCORE)

I} g ~&vI\(P0

values (2, 3, '52');

F Z-c~`0

insert into STUDENTS_TAKING_COURSES (SID, CID, SCORE)

hey;bA#Fh0

values (3, 4, '94');51Testing软件测试网~~Rq Z

insert into STUDENTS_TAKING_COURSES (SID, CID, SCORE)

|k U.{0M:C,]{0

values (3, 5, '84');51Testing软件测试网N+\ku.p4{e_+P

insert into STUDENTS_TAKING_COURSES (SID, CID, SCORE)

,Ye)JV(s~P0

values (3, 6, '84');

b(n!zy1W#~5f0

insert into STUDENTS_TAKING_COURSES (SID, CID, SCORE)51Testing软件测试网2o#S} DDd+zd

values (4, 7, '78');

tkb6VwES0

insert into STUDENTS_TAKING_COURSES (SID, CID, SCORE)

0v1r } `n.g4Nt@0

values (4, 8, '99');51Testing软件测试网1R p0S2@ T5^

insert into STUDENTS_TAKING_COURSES (SID, CID, SCORE)

J2H-d@&XJ0

values (4, 9, '100');51Testing软件测试网?&N.Q1?'wD{

insert into STUDENTS_TAKING_COURSES (SID, CID, SCORE)51Testing软件测试网M#m;y,h.A.B)`FF0S

values (4, 8, '64');

;F"f'sI,RA#? ]2f0

commit;

x)U9j B,c4~3x0

TAG: 数据库

learning_test 引用 删除 define_NULL   /   2009-01-16 18:33:23
55, 我还以为是DB2,.好久没用碰了数据库,级联查询都快忘记了……
学习了^_^
 

评分:0

我来说两句

Open Toolbar