/ j! ^0 X d6 g7 |0 P哪些人的薪水比任何一个叫张无忌的人高都行?大于最小 * B) h5 W4 }8 u; J* E10000 3 x3 U5 ?# s5 s U0 h3 O! X8000 3 u- \. ^7 o8 t Y! W+ B m3 b. Y: M8 w+ ^$ Xselect ename from emp_ning " \- x4 r$ h' |% o U( R' Nwhere salary > ANY(select salary from emp_ning4 L0 l- c* I8 o3 k3 V
where ename = '张无忌');! m, z9 \' e$ Q+ r4 z" [
6 F2 B$ W5 j9 X% s/ q4 G8 h) a
2.谁和刘苍松同部门?列出除了刘苍松之外的员工名字。 4 z' n, D& s( N- kselect deptno from emp_ning. I7 h& N8 G8 {& c( ^# x" w- w
where ename = '刘苍松'; 6 T) S# t1 d2 l, d10 3 J5 o; C }1 u5 K, a* a% |, ~2 l* h* g1 ?
select ename from emp_ning- i4 [, e7 d( ?8 b6 @( Q
where deptno = 100 D9 d/ B3 g. g T% o- M
and ename <> '刘苍松'; & H. ~' W' N0 f5 p- v9 r9 _. e, R) x+ P& i; h
select ename,salary,job + t1 n$ b1 M, z+ ^5 zfrom emp_ning 1 A6 k u! z+ ?. J; F5 U( s+ `) D6 C) x, hwhere deptno = (select deptno from emp_ning- \2 q: I$ u1 w4 _- X( l( @
where ename = '刘苍松') 9 ^- j+ x) J" }- V& F# ~9 K2 ?0 I. mand ename <> '刘苍松';6 J9 M; ^0 s3 J. c4 `
7 N" D2 u5 Y. |$ D--如果子查询得到的结果是多个,不能使用单行比较运算符 ' o7 Y( Q$ i& T, U) v3 w5 {* E等号。改为:in& i! Q0 A, Z# ?; p; Q* Z
insert into emp_ning(empno, ename, deptno); {" B' V" d! T& p* _* Z
values(1015, '刘苍松', 20);! {9 T/ M/ s8 g o) M, N9 ]
* F" F% S' q6 Q5 G
select ename,salary,job,deptno m4 \9 \- S' e
from emp_ning ( e9 \( g, r: n" a2 Xwhere deptno in (select deptno from emp_ning. [2 @7 V u5 e& d' G3 s
where ename = '刘苍松')6 K2 ~" K0 o* |/ Z
and ename <> '刘苍松';0 F# q0 v. i/ i. ]3 ^
$ ~ h7 {" o3 q B0 o( ]
根据子查询返回的结果的行数: % v$ c' t8 `' k+ g返回一行:> < >= <= = <> " g* c5 G9 B- A, f返回多行:>ALL >ANY <ALL <ANY in $ x6 o6 g1 ]' D5 O: k0 H) L, [! T! l$ j: X
3.每个部门拿最高薪水的是谁?! s( @# ?3 B4 M
--返回多行! w- D0 V- ~# D. o9 K
select deptno, max(salary)# E4 I/ u9 ]' s' ^7 l- m) ?7 |
from emp_ning5 F1 H% F1 L* p4 X- }4 h. B/ e
where deptno is not null ! u& t, o# N" `. A- ]3 Vgroup by deptno;1 e+ g, u* z1 P1 m7 n, S1 a
+ D$ Q7 A3 p& F+ P9 Z2 m% b 30 5000 r% t% ^) K% c# x1 { v
20 150000 }7 o; y) g/ T, d
10 100002 G, b* k3 O) u6 D i+ K
5 P4 x+ q2 o: U8 b( _( Dselect ename, salary, job, deptno1 w. f; F0 z9 K. b7 k e
from emp_ning ' m" h. h* t" r$ ?3 I* Hwhere (deptno, salary) % @, R4 K1 a7 h9 Z. Z1 \
in (select deptno, max(salary) 0 g& b' R4 T& |7 u# u* o% y/ w from emp_ning. l4 x9 O! v9 U2 s
where deptno is not null+ S' T6 N/ p( Z/ l1 T7 L' S z
group by deptno);* O' ^! k k8 Q
8 S' A* _" @. N( F- ^3 P4.哪个部门的人数比部门30的人数多?1 ]0 s+ B9 J" f( G0 P- f
select count(*) from emp_ning " S; S y( {# U1 Q* x& twhere deptno = 30;* @" y( E5 o( X) m
3 0 ~% G+ u2 T6 W( V! Z8 D , ^! X/ W3 S7 D/ H3 lselect deptno, count(*) ; R5 ~0 H! n( c; sfrom emp_ning" q. s2 K/ ], |! o/ _
group by deptno& |0 T" d4 N1 y; F# t' q
having count(*) > (select count(*) from emp_ning9 K* V) w5 {; _% e9 R
where deptno = 30); 8 p/ K! v( F0 M& u ) t0 t! c/ r: R$ e$ V. Q3 r: d9 S5.哪个部门的平均薪水比部门20的平均薪水高? $ u6 s% t/ \" H1 L+ l3 I2 iselect deptno, avg(nvl(salary,0)) avg_s! D, ~& L- |$ H, M! E' R5 E# G
from emp_ning( A- a- r& v5 m5 S2 H9 F8 q
group by deptno 9 U2 l7 ^ Z6 O% H# Zhaving avg(nvl(salary,0)) ># B( j& d, t4 Y: j
(select avg(nvl(salary,0))7 b+ Y" S3 M3 M/ u4 n2 J: d
from emp_ning6 m' U0 @0 M. c8 O
where deptno = 20);! N( t) L" M v, U7 E1 Z
% A3 w" D6 J9 R1 }/ N8 f/ H
6.列出员工名字和职位,这些员工所在的部门平均薪水: e6 q& K% u5 l8 P
大于5000元。; \% P( z, g, G2 o
select deptno, avg(nvl(salary,0)) . ?: c% m$ I, A' U7 m3 E; R$ X1 Qfrom emp_ning# A" f- k/ m5 i2 P) e! s
group by deptno # n9 s( L' M, j; A- ?5 G1 E9 Zhaving avg(nvl(salary,0)) > 5000;+ _* M5 k; \* x, }, \4 v
10 5750 6 `7 w( K# u, U# O& K3 D20 8000 0 }# S5 Z; \ m, y, i& _# h- {/ o6 |
1 L' U6 [" V% F' P' Y: A8 Lselect ename, job) |. y& @7 y8 N' L) f: S, `* C3 d+ F
from emp_ning * m. U/ j2 c4 ?) U& K* Hwhere deptno in (select deptno / R4 S# _3 D7 p( p from emp_ning # v% P# b7 s2 @# w# J$ O# Z, _7 B" r group by deptno _$ O, f7 V) L( |, a8 C& Q
having avg(nvl(salary,0)) > 5000);% b, R {8 w8 z5 ]/ B: i1 }
8 h' P# Y4 w4 y" c1 e7.谁是张无忌的下属?5 o5 D% Z5 S: l' a& U" x& V! U
select empno from emp_ning 6 _9 W9 D! h3 j4 a( u D: Owhere ename = '张无忌';2 a2 W2 { C7 l, [7 S5 ^
1001 3 P% ~5 n) h& R3 v7 l" h( _1014, k% a4 j9 P2 w4 P( Z% B4 l7 J
4 }6 s5 l( p2 s' ~
select ename from emp_ning , Y4 s( ~& `! ]8 I1 l6 mwhere mgr in (1001, 1014); ! e9 @: Q% c7 C8 X( | # r5 Y: w# p P. d ]8 P1 [select ename from emp_ning$ \2 K6 d( {1 K; i
where mgr in (select empno from emp_ning / b$ I0 b* L- |0 t) f+ c" W% H" H where ename = '张无忌'); 0 W+ c- k3 c% n- Z! Y' I 6 w) T; N" F" V( h8 S+ x# V8.研发部有哪些职位? " l! r" ]7 }$ ^5 r" \select distinct job from emp_ning 2 u/ m; R. e/ a: Q. C$ c6 U. a: ~where deptno = (select deptno, M/ W* l' Y1 {
from dept_ning 2 x. t3 u6 y1 {, b( e% Y& ~! @7 p where dname = '研发部');4 L6 I T, y: Q1 g- s