SQL查询语句复习题! c8 }0 K7 y; e; e* i
新建学生-课程数据库的三个表:$ ^) v) Z3 B1 [' o
学生表:Student(Sno,Sname,Ssex,Sage,Sdept) Sno为主码;9 g8 J6 U# b p* p* L! `
课程表:Course(Cno,Cname,Cpno,Credeit) Cno为主码;
, d3 G/ b0 w& Z0 P1 n* P+ n学生选修表:SC(Sno,Cno,Grade) Sno,Cno,为主码;) _( A7 G! b" W& V1 M
Student
1 T) ^" g- i Z5 b学号5 Y7 h) q h8 e5 C+ D
Sno姓名- \: P2 {: \1 E* G
Sname性别
' @+ I6 E/ b, N9 y5 [& YSsex年龄5 c% y: W; z) y) x" f
Sage所在系! G" o" E; u; {2 K- B" E
Sdept
8 o' R) g7 x2 J+ b95001李勇男20CS( l, P1 j" h$ K
95002刘晨女19IS+ ]8 ]- F# x1 |, @
95003王敏女18MA
* w' ~( J' e6 X0 g: m95004张立男19IS9 N" ^ ~ J, i" `/ {
课程号4 ?! ~) x' e2 Z
Sno课程名
; Z! P) L7 p2 t P0 F1 i$ [1 G2 uCname先行课
& U$ t) z& C7 H. l ICpno学分
: ?. Q0 G" p- o. `/ t5 O; iCredit# _9 D3 ^, L) K' k, j6 d3 q
1数据库54
+ d' w8 ~4 X1 e4 U/ w# Y ^2数学2
! x7 \- |4 O% z* P4 B3信息系统14
7 A4 R: a" c6 D% _8 K F, F4操作系统63
5 V! s) g' L- Q1 F, T7 U4 M7 [5数据结构74
9 j$ J8 M- R& ?( G Q6数据处理2
$ G2 K$ k; N. l3 O) I/ v+ s7Pascal语言64
7 N% h) k! m+ k+ X- R6 @- n% C5 ECourse:
; `" p$ J; U5 K2 b- K6 }% h8 `9 G# @' i7 [! m9 G5 A
$ R% r% P0 l6 O/ V; N# D$ v! P
! V8 `* _1 [, q* L4 ~3 R8 Q
2 u+ t; M! J, {; X$ |3 I
( Q# g& F5 H* ~) RSC: ) k( D1 g* y$ K! S& D x* z1 \
学号
1 M, v& i: B- N: b; QSno课程号- Y: g S! A/ ]0 B/ y; B, Z* T$ B
Cno成绩. g* q8 g0 w5 k9 r6 `( o
Grade
& M+ m* f" q( q$ S9 J! o$ L95001192
P. h* j% r' N" m5 n' Z95001285) t# @- }$ _5 Y; ~# Z& l- x
95001388
. l: ?; D9 @7 m1 s95002290
! D: g3 k7 \: L$ @) {& H3 s95002380
. w' k# o6 i1 m+ K, ~7 O$ @ y$ N- z& w2 Q, p
" r( n, {2 u. _" E$ {
7 {3 h( c& J+ i }
8 L A) C/ a) O1 `' @一:查询表中的列和行
, V1 d( T: e( H# f6 b9 S3 ]+ c1:查询全体学生的学与姓名/ P3 h! `7 L7 D8 B8 B& }- L6 I, v
sele sno,sname from student
, P3 {( O; F* O2:查询全体学生的姓名、学号、所在系。% {# Q/ Y; t# V& P
sele sno,sname,sdept from student 3 M* l; b4 \- s# ?7 W' b
3:查询全体学生的详细记录8 ]( ]- X* S- f- t6 k4 S
sele * from student& g" T$ X0 ~% ^/ W1 i% W% ~. \
4:查询全体学生的姓名及出生年份
% P' a# H1 m$ F" m4 I. S ]/ Gsele sno,sage from student; C/ T8 \& y1 ]2 D3 K
5:查询全体学生的姓名,出生年份及所在系,要用小写字母表示系名0 W, G0 H! `* @! o( r4 V w& B
6:查询选修了课程的学生学号/ r T5 Z" u" p$ N
sele sno,cno from sc2 I- i5 l7 j# ~, ^9 B
7:查询选修了课程的学生姓名
, @* ?! Q# ?& Z7 A9 rsele distinct sname from student,sc where student.sno=sc.sno9 T4 h5 R9 Y% y$ T' O% c
二:条件查询:
, r# W, s- L9 {/ E* }' ^3 s常用的查询条件: I& S0 `( q, h+ `/ W$ j) _" }
1 v; l, a8 R T- A! f查询条件谓词
& s7 l5 f8 m" s比较=,<,>,>=,<=,!=,<>,!>,!<;
* I8 z/ ~3 M) o$ C3 D l) vnot+上述比较运算符8 y- {7 p- h, X$ ^: i k1 E3 Z
确定范围Between and,Not between And,
5 ]- Z( J5 N T! F0 W+ c% \确定集合IN,not IN$ V1 u' n% q) k9 z S9 _: ~9 X
字符匹配Like,Not Like 6 \5 ]! \0 N0 {, N5 n- N
空值IsNull,ISNOTNULL9 M+ E% X4 R- [$ n, M; h2 u
多重条件AND,OR: U! j: `6 [0 l+ x
1:查询计算机系全体学生的姓名8 z+ y/ U- @5 a1 P' `% f# H
sele sname from student where sdept=”CS”6 G5 S/ L$ M. H3 r' R0 e
2:查询所有年龄在20岁以下的学生姓名及其年龄( }6 ]2 r" }- [; ^, B9 m n; w9 r
sele sname,sage from student where sage<206 N% g6 l+ \; _- H. B
3:查询考试成绩有不及格的学生的学号
- f, J! g+ S/ _1 o+ R$ ~- D$ F6 Msele student.sno from student,sc where student.sno=sc.sno and grade<60
! B$ f5 r% } c& j* `4:查询年龄在20到23间的学生的姓名,系别及年龄* A0 c# @! z- \2 D' e
sele sname,sdept,sage from student where sage between 20 and 23
0 M# _8 S" [( t! G; j% [5: 查询年龄不在20到23间的学生的姓名,系别及年龄1 Z# N) X o/ u3 W) ]
sele sname,sdept,sage from student where sage not between 20 and 23
) ?. D3 D0 _+ Q% w! H0 c6:查询信息系(IS),数学系(MA)和计算机系(CS)学生的姓名和性别
7 d" D9 i& p8 k: rsele sname,ssex from student where sdept in("IS","MA","CS") 8 A8 x( Q3 P% h; I; b
7:查询不是信息系(IS),数学系(MA)和计算机系(CS)学生的姓名和性别' @! Q7 i' g0 d \4 t: O/ G3 N
sele sname,ssex from student where sdept not in("IS","MA","CS")
( ^3 q6 i2 v. n5 r7 T' h6 U8:查询学号为”95001”的学生详细情况
, Z$ k# f+ `7 R4 }6 }: l2 ysele * from student where sno=95001: I5 f" m2 e$ S/ J+ B* }$ d
9:查询所有姓刘的学生的姓名,学号和性别(where name like ‘刘%’)5 }. q( Q& \% F- q0 q+ W; |
sele sname,sno,ssex from student where sname like ';刘%';
+ I. \9 j& M9 ]6 x10:查询姓”欧阳”且命名为三个汉字的学生的姓名
/ c0 N1 N; |5 ^sele sname from student where sname like ';欧阳_';* `3 O6 Z" w3 u( L
11:查询名字中第2个字为”阳”字的学生姓名和学号(where sname like ‘_ _阳%’)
6 @+ a" R5 D( S, i' Z$ J% S7 Esele sname,sno from student where sname like ';_ _阳%';
& e3 G3 p {( Q12:查询所有不姓刘的学生姓名
: ?* G# d1 t; Y# S$ \5 R4 P' zsele sname from student where sname not like ';刘%';
, x' V' e1 ` ?13:查询DB_Design课程的课程号和学分(where cname like ‘Db\_Design’Escape’\’)
7 w$ z' D" {; F7 m: m7 J# Y [/ Osele cno,gredit from course where cname like ‘Db\_Design’Escape’\’/ x- e! O% `4 n& e' q9 K
14:查询以”DB_”开头,且倒数第3个字符为i的课程的详细情况(where cname like ‘DB\_%i__’escape’\’)
3 b" p w9 [2 O, H3 c& q‘DB\_%i__’escape’\’) sele cno,gredit from course where cname like ‘Db\_%i__’escape’\’
# G, V8 }; F, ?$ f+ n# x15:查询缺少成绩的学生的学号和相应的课程号( P4 i1 w+ z' f9 V8 M
sele student.sno,cno from student,sc where grade is null
' G& h8 q; Y) w16:查询所有成绩的学生学号和课程号(where grade is not null)
2 C" ], m3 y5 _sele student.sno,cno from student,sc where grade is not null
! Z2 ~* i/ f$ A* y+ m% ^17:查询计算机系年龄在20岁以下的学生姓名
$ Y$ q" C' _* H' F* {; o9 Dsele sname from student where sdept=”CS” and sage<20
% C5 d; m7 [+ m) }! S18:查询选修了3号课程的学生的学号及其成绩,分数降序排列
# B* n9 o$ n5 a% b) C8 psele student.sno,grade from student,sc where student.sno=sc.sno and sc.cno=3 order by grade desc5 T P. Q ]" P. }9 [4 x
19:查询全体学生情况,结果按所在系的号升序排列,同一系中的学生按年龄降序
) G- f; E8 p9 i3 M8 ?sele * from student order by sdept,sage desc
7 N) a2 ^' ], }6 x7 v三:使用集函数
* g! w5 y, X* ~" d `! |( e8 Hcount,sum,avg,max,min
L( C. \2 w' Y% L# v2 h1:查询学生的总人数
" M) h7 a2 F! s- x2:查询选修了课程的学生人数(select count(distinct sno))
- |, n% J, O: B3 P3:计算1号课程的学生平均成绩
* J) f. _+ |6 U, I4:查询选修1号课程的学生最高分数5 F6 u4 @% @. d5 ?' L7 G
5:求各个课程号及相应的选课人数( selsect cno,count (sno); from sc; group by cno)
7 c3 y' g7 `1 M8 u ?# u6:查询选修了3门以上的课程的学生学号
; s" q( o" i2 o1 [ select sno% S5 T" h$ r% ^7 p
from sc
& ]. d) c4 ~8 { group by sno . S& z, {3 H( h" L: f
having count(*)>3
" p6 a2 G7 J5 j" h! X& j四:连接查询:
, _$ x; p! Z- ?2 g<1>等值与非等值的连接查询
( b" F8 Q7 z! o7 _' g; I, S8 K 在连接查询中用来连接两个有的条件称为连接条件或连接谓词,,当连接运算符号为”=”时,称为等值连接,使用如,=,<,>,<=,>=,!=连接时称非等值连接
3 b5 @1 C4 H. k1:查询每个学生及其选修课程的情况! j. Q2 l/ E6 p6 b0 b; M3 Y4 v
select student.*,sc.*
; d* e% g. ~& B6 n) v5 D from student,sc$ Q9 Z7 `4 z4 b, N$ u
where student.sno=sc.sno
, m/ B% }5 ^ a" u: }; }8 @4 D% F<2>自身连接6 C5 T6 f# B! }6 r* T9 w3 j7 G X% E
连接操作在同一个表中进行连接查询
7 I/ W; m# J& l, H# O& W% y2:查询每一门课的间接先修课(即先修课的先修课)2 z* W' z/ h5 F' v3 q9 Z! b7 C y( q
select first .cno,second.cno2 s% j/ ?1 Z& L6 Z+ E- c
from course first ,course second- w5 V% X- `+ i
where first.cno=second.cno
3 T) D+ g' l/ _- _8 c五:复合条件连接
- O1 `1 a6 j+ T* o6 g1:查询选修2号课程且成绩在90分以上的所有学生。% b2 f5 d# {6 ~- ~) p" v) @7 N z
Select student,sname/ J! R1 _7 L1 k( N
Form student, sc V0 G$ t; g% g
Where student.sno=sc.sno And $ a9 O3 s" ~) N; [6 Z3 F; y
Sc.cno=’2’ and sc.grade>904 ]! p5 K+ d& u5 ~& p7 h- M
六:嵌套查询 b- F9 x" e6 v( Z0 M5 o) P
1:带有谓词in的子查询2 P" K5 N7 r* q; y' L1 c! D" L: B
<1>查询与“刘晨”在同一个系学习的学生
! W* e! H4 i1 g! _/ tselect sno,sname,sdept
# K( S8 y& P1 n# i$ g4 Gfrom student : |' k0 y5 M( I; j& `# d
where sdept in(. i1 r9 k$ M" ~( n: J' X, A. ~1 w- J
select sdept ; a2 a2 q! {4 G
from student
# }$ r6 a# C2 ^( K$ l9 V& Y1 }9 i where sname=”刘晨”). Z4 s8 r7 a0 J9 b3 a5 L2 L
或:select s1.sname,s1.sdept9 t. t) A, u. l% U1 N
from student s1,student s2
) B3 N6 r4 ~1 `/ P. {where s1.dept=s2.dept and s2.name=”刘晨”6 w; Z r; b. y% F( |. }
<2>查询选修了课程名为“信息系统”的学生学号和姓名' g1 z8 e6 w; @" b. F/ m; X
select sno,sname ' y' Y& P b4 O. Q- p) n
from student2 @$ G; _! Z8 M2 G
where sno in; Y0 D4 a, a& J- ^
( select sno
2 X0 }; [$ ^, I1 h# m$ ?. m$ c from sc
8 z- Q, D# S- j' N where cno in
) g( g( u) Q2 p% |4 s/ O (select cno
6 R- E' C0 }5 @% k' i+ e9 O# [ from course! g. @; a7 T% j$ v7 R0 p
where cname-“信息系统”)' {6 m) E+ J" U% z
或:select sno,sname. l1 G6 Y" h( O4 n
from student,sc,course
5 t; }0 O! z2 w |9 t6 |( _ where student.sno=sc.sno and$ l2 C O& K1 k: ^
sc.cno=course.cno and
5 E0 d. C7 I' w4 o4 w course.cname=’信息系统’): U) N A& Q9 r" i! V7 [
2:带有Any 或all谓词的子查询, C" ?% ~8 B3 {; p1 _( d
<1>查询其他系中比信息系中某一学生年龄小的学生姓名和年龄2 J. M0 u' X1 M0 J7 I" G& ~ [5 l
select sname, sage
. n4 C$ V5 Q6 Gfrom student3 Y" _0 B4 y9 u, }. f9 L
where sage <any(select sage8 Y7 F3 v: r+ l6 E! N k' l6 O+ S+ p
from student
* l X0 e5 p# q/ fwhere sdept=’is’
/ q8 \5 {7 A0 Mand sdept<>’is’
9 u$ n; R& J& q! V- n( a) T/ d或用集函数:select sname, sage$ c( r3 G; G7 x. U
from student4 }* g1 e. X9 V1 D; j
where sage<
9 b4 F4 z( o; [" @: Z1 n0 A (select max(sage)
' i/ @/ v$ E) G1 R. }) J6 _ ^3 t from student
7 U2 n) O$ k3 L4 A where sdept=’is’)6 H8 d+ W* I5 v) P2 k
and sdept<>’is’
( B) m j' l( f2 D( K<2> 查询其他系中比信息系所有学生年龄都小的学生姓名及年龄3 x6 A! b8 x- }' h
select sname, sage
7 N3 G7 E* U- Z1 C5 `from student
& B1 b& y& N4 H$ A# p+ M/ Cwhere sage<all: X, h" m3 C: ~& N" h/ K9 _
(select sage
- e9 F2 ^% E- H$ x+ r from student- ^8 D+ b4 g3 z0 _7 s- c! @
where sdept=’is’)
* F- u; S. o, }$ f* e% G* }& j and sdept<>’is’
- l3 E5 r6 }. T- q+ I1 A; |1 e9 Z3 带有Exitst谓词的子查询
& ]" K& z9 z0 y( H+ o% g3 T7 W* G$ M<1>查询所有选修了1号课程的学生姓名
0 s' k$ o9 E* v, U- wselect sname
9 J6 v" K" X5 X i1 W8 bfrom student4 u. Y" D; w/ X; z0 W# L. v
where exists ) M# J0 M4 N; ?, b0 N
(select *' s( H: ^6 D& x8 y) C
from sc# `; O# a: r; s0 b' k$ u2 Z
where sno=student.sno and cno=’1’)
* {9 V2 O2 r5 A B. E1 o<2>查询没有选修1号课程的学生姓名
p9 }/ ` {' ?3 k/ P+ y% u( ?" H select sname/ }& E( v/ X) e5 p! C# K0 x
form student% h k& W3 S( U1 X, ?
where not exists ! p v% u6 {) f0 W
(select *
- V S5 g! h8 N$ ^" \ form sc
' [3 Z v) v0 w# ^3 r% O4 G where sno=stuedent.sno and cno=’1’) }8 ]3 t4 M* X% q' w/ g
<2>查询选修所有全部课程的学生姓名
5 ~/ V9 R2 p2 @select sname
1 ]7 d0 a3 R0 s* P: `6 x from student+ Y' P0 w8 |* s
where not exists
! r! P# ] U" V' ~( c$ v! D (select * 8 m" b0 @, k( @
from course$ H6 k L+ j2 ], f4 n
where not exists
5 O, ?' f4 P; u3 q. ]1 r s (select * " |8 I: V! s' ~4 V# {
from sc
8 a- e& i2 ^* ] where sno=student.sno( P4 \5 V9 T" ]/ d/ v
and cno=course.cno)
0 m2 w. z0 U& T- @<3>查询到少选修了学生95002选修的全部课程的学生号码
' q$ P, m# M# _0 z select distinct sno
; ~, L$ L# W) L8 o8 R) R% v from sc scx& }( u* v9 u8 }1 ~0 [0 o9 X: P! f
where not exists
6 h1 x8 w7 ^$ b. p4 X, ` ( select *
1 e# x4 L- N* O9 _* E9 s# z+ w0 x from sc scy- K# }6 [. `6 G
where scy.sno=’95002’ and
$ \" ^; y& P0 N' Y2 P not exists
' K1 {# {7 k) f6 } ( select * 1 n# n5 f& h1 M/ q. Y
from sc scz
3 b! V4 C+ z4 O$ ~3 g+ B3 N0 t- k where scz.sno=scx.sno and* z( ?. x9 w7 S4 F" o8 F% ]" x
scz.cno=scy.cno)/ E& t) I' F6 q% l
|