! X, H4 ~/ D( J( N' c合并到一条SQL语句: ) h8 t1 \6 _8 dselect ename from emp_ning4 J: k8 n, J+ ~8 A5 q
where salary > (select salary from emp_ning9 H! O6 m& `' k9 p
where ename = '张无忌'); $ v+ \- x: F. b% n) j/ M: r 9 x* ^- M8 `* j+ Y/ \% K1 T> < >= <= = <>: 单行比较运算符, 7 _8 ~+ c2 a4 d只能和一个数字比较。0 m$ q) B* `& I0 l7 |( K# @* B
; k# _) _" t* m8 S% L$ ?1 `insert into emp_ning(empno, ename, salary) / E) f: l+ Y2 l2 ?( \+ M3 q, uvalues(1014, '张无忌', 8000);+ o# e1 N" p$ t$ P. u
0 Z0 @+ o% G1 ~' y$ s9 d+ v
谁的薪水比所有叫张无忌的人的薪水都高?大于最大2 o8 R0 ?6 a# I9 Y, N$ `
10000 - I; _3 f. H4 B8 W" H' L3 c8000 0 s M4 s' P9 ]7 C8 \5 s 6 q& q# J% }+ cselect ename from emp_ning 6 W$ G+ x3 C* p, @& ^6 i8 ?, V5 Ewhere salary > ALL(select salary from emp_ning% E7 a7 s9 [* e- C h/ C3 f5 {7 K% E% g
where ename = '张无忌');0 V3 I# E9 b, j6 l: a% u6 q
9 [' z% G# q3 B6 [( \; ]
哪些人的薪水比任何一个叫张无忌的人高都行?大于最小 , O' q! P7 \6 u* }3 r4 r100003 R! ?3 j- g- @/ T% F
80009 h+ m/ v3 m1 B- X; L9 k
S7 y2 c# P! Z! kselect ename from emp_ning% ?, s8 _0 _- i2 ^0 v' n$ _
where salary > ANY(select salary from emp_ning$ @3 ?* X3 t1 }+ z8 a% E9 T
where ename = '张无忌'); ' m# d, x) V, _5 Q% n) A & Y+ W7 [& T l* |" c2.谁和刘苍松同部门?列出除了刘苍松之外的员工名字。 - H4 ~# v, x1 L4 |" a& `select deptno from emp_ning5 L# T0 r5 @7 d( \' p3 L
where ename = '刘苍松'; l0 u9 `/ _# B3 E5 D10 , D: F; D$ K- j9 W- Z' N( F9 Q9 W1 c' G
select ename from emp_ning3 \5 N) B3 T* D/ x
where deptno = 10 ) A. U& L5 X; d6 d- mand ename <> '刘苍松'; * l! Z0 E9 F( ~$ [ 2 h( y5 { ~+ n4 J" C/ Uselect ename,salary,job , T7 A3 |: h9 Q5 W% e7 k, p: }from emp_ning) G4 ?. j' H% j, I% B" c) }( U
where deptno = (select deptno from emp_ning+ w. H* f$ ^- A5 q, z
where ename = '刘苍松'). R8 X% n9 _4 A8 y- J. x( R# }
and ename <> '刘苍松';8 [8 @ B6 q; z6 u3 w- J8 ^" x
* S) Z6 J4 E1 {' i
--如果子查询得到的结果是多个,不能使用单行比较运算符2 I# |& A2 j, N$ C$ \
等号。改为:in/ O+ U/ n! b d$ H7 n5 ~
insert into emp_ning(empno, ename, deptno) ' d3 ~% u* y4 b- E0 Z* ^( M* Lvalues(1015, '刘苍松', 20);7 O: d8 t* ?- u
" a) w- q+ x- h# g& T
select ename,salary,job,deptno & e! u3 k) l% k- ?7 c# gfrom emp_ning l: E5 q- o9 I4 H1 C7 V
where deptno in (select deptno from emp_ning2 _) f z+ S$ H
where ename = '刘苍松') 5 W* |7 q: Z! B- aand ename <> '刘苍松'; `: z: d' p1 f: f2 i- L1 `. C y% z$ P& m9 n5 H8 X
根据子查询返回的结果的行数:" Y' z8 x+ M P" b O: Y! y
返回一行:> < >= <= = <>4 L. c7 Q" p" o/ E4 v
返回多行:>ALL >ANY <ALL <ANY in 1 h# e4 Z$ q0 m" V0 u8 X( k; l, n) C, D( ]& S* N3 H6 Z8 K2 o
3.每个部门拿最高薪水的是谁?* e7 A3 Y$ U* e
--返回多行 , W. v: _$ \. k5 K& Y: E6 f- I2 uselect deptno, max(salary) # ?4 k# H7 S; u) _3 C" mfrom emp_ning # d: b8 ~9 J D2 gwhere deptno is not null4 z( O Y5 T* j" `
group by deptno; 0 T! Q$ V8 S& S; ~$ V- x4 ] $ M$ S+ W7 |* y; |! E 30 5000 + M+ j4 z3 j" Y8 l% z$ e+ _ 20 15000 / U, l9 b* M. e; C/ } 10 10000 & m& @3 X1 y% Y! N* m8 i5 C. v- y. _/ y9 |0 t( M
select ename, salary, job, deptno ' D: K, \8 F9 K L% cfrom emp_ning + W- ?1 m7 u9 \3 ]where (deptno, salary) $ |# w9 q1 c* J2 c1 b6 i+ y6 r
in (select deptno, max(salary)- d' x: N% I' m
from emp_ning : a# l, Q3 v# S) S/ d! L7 ^1 N, o where deptno is not null / {: t& T! k F% b! Z group by deptno); * q' j" u+ X' g+ A& Z% B ' {: y/ d! d0 }, A# S. b* y4.哪个部门的人数比部门30的人数多?6 V& k1 R1 e+ c1 k, t, e+ q
select count(*) from emp_ning 0 \3 z( }$ m( [0 ywhere deptno = 30; / o. o9 B& p' u3 r8 x& k30 Q. w0 R! L- s! e2 A) s( G- ^- d
+ `# r1 {! A! b: O( F6 M
select deptno, count(*) ' U3 ^1 f. v6 b. L/ X' Yfrom emp_ning + y. j( v) I h! M# U+ W$ [- [group by deptno5 F5 e% E! O; G7 Q0 S
having count(*) > (select count(*) from emp_ning 0 B6 C8 `! m9 t2 p. j where deptno = 30);; D0 T1 m* M7 O+ _
4 v. R( m- [# S! g
5.哪个部门的平均薪水比部门20的平均薪水高?* @, W$ ~) t6 D2 F. d
select deptno, avg(nvl(salary,0)) avg_s 8 m4 z) K9 s" L. m5 j& pfrom emp_ning ) x3 o5 q U. A) }1 w& Bgroup by deptno+ M+ G8 s6 g F+ m
having avg(nvl(salary,0)) > # x: M' M* m/ \; K3 T1 t8 L (select avg(nvl(salary,0))4 a/ {5 T, T! q1 i# Y+ g0 W+ b
from emp_ning3 J5 I1 i3 I0 k' z
where deptno = 20); % w, I2 E, ^0 `+ u+ g. X. p5 E , p4 f; H0 W4 a; ~; D* q& H6.列出员工名字和职位,这些员工所在的部门平均薪水 ( ~4 u- ~+ t+ I2 v: `8 `( V9 f9 s大于5000元。8 d, \5 q7 S- W+ x- r/ \
select deptno, avg(nvl(salary,0))" z! ~4 S: Y1 A/ w" `
from emp_ning , C& s# n/ c9 r# Z3 B1 `group by deptno: g2 c/ `/ D+ G$ C2 B0 @ [' L
having avg(nvl(salary,0)) > 5000; * p0 e2 J/ M: Q. T {9 w/ I/ Y10 57509 B% W" n: c7 w6 z1 O
20 8000 0 e, r1 o6 D; o( y! l0 P
) n/ S& ^2 Q3 y+ T7 Iselect ename, job : M) f: {0 m+ b6 E! m, Ffrom emp_ning( m( F" n+ U# e/ J
where deptno in (select deptno4 e3 I; k% B- R
from emp_ning6 Y% c4 W& E" M8 y, y
group by deptno4 Q% d( S& x G `/ {7 Y% C
having avg(nvl(salary,0)) > 5000);/ c2 z2 G1 c" u$ G! `5 ~6 b, G
8 N8 L: M7 ]% D4 M& O7.谁是张无忌的下属? " m+ i7 G3 \0 o5 `# t8 B# lselect empno from emp_ning3 d0 j+ W# X& ?, L ?
where ename = '张无忌'; 6 F1 D: R" c5 p {+ c10014 p5 O) b- h2 u( a# s7 G: {8 B: k* v
10148 e! t6 M7 a' o( a5 C
( x C5 p' b2 v- Kselect ename from emp_ning+ W( I& g w& m% B- r
where mgr in (1001, 1014); $ E: j' M" U& F9 |5 S! [0 I9 N n/ s e& V8 U4 ^4 N* J
select ename from emp_ning 8 o* Y8 s1 t# `' d6 l8 C3 fwhere mgr in (select empno from emp_ning) L- Z& f/ p# o! Z
where ename = '张无忌');1 y. k2 c5 p( }7 [7 R9 S% D
' o9 ~3 a u& A5 J" u8.研发部有哪些职位? - W, e9 i$ b& R3 Fselect distinct job from emp_ning ' h: w x) d! t3 twhere deptno = (select deptno! h8 |( J1 N1 J& p$ _
from dept_ning" p4 v2 s8 w$ v7 x% `; j
where dname = '研发部'); 1 p6 w6 u. c* O! B- i S: P+ a0 V/ C! S' H