该用户从未签到
|
复习:
; m2 I6 h2 S$ I* S3 H1.查询语句的基本格式。
/ V2 G) t: z& h+ [3 H$ Hselect ...
6 \* D2 H" e2 p/ C6 h1 jfrom 表名
4 i5 O" F7 f0 p( y) J2 ~- [where 条件
" ~9 J0 Q. _' y# @; d- [( Ggroup by 列名
- Z3 ~& W( P5 j/ q7 g' Ghaving 带组函数的条件
' O( @1 Z" \9 Vorder by 列名 h8 g/ i! Z( U/ U, h
" s3 U% h6 l8 K# S$ ^* g2.函数
, w r6 L' ^& ~ I1)组函数:count / avg / sum / max / min
) K7 }0 T' A2 f: J1 c& w, U- ~4 c( F- O+ b2 U9 |' _
2)单行函数:% T% W/ X* e2 o
a.字符函数:upper / lower / initcap
5 M2 r0 Q& ?9 X0 \% q length / lpad / rpad
/ o# s/ ~2 I8 F
, P* q, E9 z' j' U, o: q9 R- b l : left ; r: right; o) F* L2 [ ?& P
* v8 V3 r- `; E, |4 t7 l9 @select rpad(ename, 10, '*') from emp_ning;; R1 H& O9 R. v) r
" k9 h- C* z! N4 X& d1 p9 I% w( Cb.数字函数: round / trunc / mod* t2 k8 D) o: Q5 w
select mod(salary, 1000) from emp_ning;
' v8 E9 h; {7 {4 T8 H+ x& F6 V( w5 A5 W; y
c.日期函数: months_between / add_months / , Z. T3 E& g, f$ r) c3 H
last_day(sysdate)9 B! z: u9 i; C
# R0 g% L) Z8 H% Y# ld.转换函数: to_char / to_date / to_number
0 _7 r5 O1 E* g; m) ?- }; [+ z: N
& A/ p2 @) d8 R2 b) n+ a: S to_char to_number3 }2 y7 m! w' r, M N7 E% r
日期 ---------> 字符 ---------->数字
/ x! _3 ]& ^( T3 _ <--------- <---------
* |# I0 Z; A/ I, {! T( N' A to_date to_char# {/ J6 G4 |0 i$ g+ H: F( y
1 ~ \, T' a& c5 X3 a8 }1 s+ J7 G
& U1 `$ b, I$ m! }0 `9 p, z, B where password = '1234' and id = '1001', |! s7 ~: a7 ?. ]/ K0 S6 W; _
7 s2 J2 [# ]- Q! o
select to_number('$7,912,345.67','$9,999,999.99')9 s0 o9 K2 F' ~/ J6 \
from dual;' |0 P' ?1 n" w) S0 ^2 j& _
2 R) W' ]9 Z) K8 t
7912345.67
3 J+ K* F8 M4 f4 r7 t- [# T) m# M4 P, u$ T
select to_char(7912345.67, '$9,999,999.99')
3 z4 s& P) r& D/ o- |7 |* yfrom dual;
: A" q; c3 ~. S0 b& X- f' f% E# T7 N5 U* v" G+ ^( l7 g/ |
$7,912,345.67
# b* K8 U) l. M/ @0 n
, D, G2 ~/ i/ s. }2 T, {. |8 B9 ^e.通用函数:nvl / coalesce / decode/ @" ]1 O8 ~# S: |
% d+ ^; b, d: W. F使用频率比较高的单行函数:
1 F0 V, [3 H- S; gupper / round / to_char / to_date / nvl, |2 j4 z8 D( ?
3 ]2 b, Y) ` J! s0 d
& R \0 w- y$ N3 i9 Z: o( v" ^2 V一、子查询 g [' g0 H' v" j( x
1.谁的薪水比张无忌高?* T6 j+ I/ K% i1 L4 C
子查询
6 W2 ]; S% T j2 d& i% Oselect salary from emp_ning* W$ P! s8 b6 A7 R& R9 o3 x! ?& Z. e0 k3 i
where ename = '张无忌';# Q" X. R* a7 J) G, h+ u) x4 t
/ B. c3 [4 w6 \# j I# j% o10000
4 m: n" D# d+ ^& r# g. ~# W; M5 t8 L
) A# U( E; r$ f主查询7 M* l7 z, h S
select ename from emp_ning
% c/ W3 W% k" u) `! Z% O/ Fwhere salary > 10000;
; X* ?7 \: R! k' T2 Q* Y1 E
" i. l6 s7 l1 l2 |9 R# B, c+ Y张三丰6 i p% j6 U+ V) O$ h/ |6 N
5 z- W8 ^0 H8 ?7 |# ]6 m# {- r
合并到一条SQL语句:
+ w4 P. a) b% ^8 oselect ename from emp_ning
' u: ~- d. Y: U6 b, Ywhere salary > (select salary from emp_ning" n" X6 f6 T0 L) j8 E3 s
where ename = '张无忌');
5 ~; q6 I, q# I$ h$ m+ d
( x4 `8 y5 U$ o1 l- k3 s) n7 Z" w- K> < >= <= = <>: 单行比较运算符,
% m$ r7 L2 I. O) t& y0 }: E只能和一个数字比较。) f# `: C" u# t, Z0 `9 j
8 t% B4 Z: J [7 i2 ]' }4 n1 Z: |
insert into emp_ning(empno, ename, salary)' B5 t6 }3 \9 @# g; p4 b
values(1014, '张无忌', 8000);
/ U5 F' N) U ^7 P( V, U2 k, `. ~
! ~, k1 S5 W& J0 O' ^- F谁的薪水比所有叫张无忌的人的薪水都高?大于最大: \8 k2 e. x& y L$ V4 e6 g6 ]5 {
100009 q! `6 ~# Z% W5 W2 b6 A: Q$ j
8000) F }* y# @( n( x
* `4 D- ^# Q: x8 g; yselect ename from emp_ning
) s v7 v5 v p* cwhere salary > ALL(select salary from emp_ning
" t5 k3 W) A6 I* y where ename = '张无忌');, s% K8 V8 ]! x: G/ d* T
. N2 Q1 Q! ^3 U* {, R0 q; e$ }6 y哪些人的薪水比任何一个叫张无忌的人高都行?大于最小) ]0 @& |/ A( p. Y; f6 K
10000
% f2 C( o4 L$ \80008 T# |, f2 w N6 u
# C7 a* E- I6 T3 G" x1 ^
select ename from emp_ning6 e7 B8 I. y& S, g: Y+ d$ K1 i
where salary > ANY(select salary from emp_ning
7 j9 h3 S( N& v1 | where ename = '张无忌');& G/ B; f/ e' o) k, u
/ i$ _. V% _0 `2 H
2.谁和刘苍松同部门?列出除了刘苍松之外的员工名字。* A- a3 m) R7 U% @1 ~4 ?& D( L" u
select deptno from emp_ning
! f& `2 ]0 e! P# b1 n( ]) S8 g2 cwhere ename = '刘苍松';
/ b# n. `; Y) a9 ]* a6 a; U) m* R4 t10
8 \) i/ ~* i* l/ P! w, |
+ O: K5 A0 w; m) uselect ename from emp_ning' I: G0 r1 t4 N E
where deptno = 10+ w6 E: t6 ~6 `& a6 ] B; S' W7 F
and ename <> '刘苍松';1 F( B- L0 w" V
$ R$ j3 {. O$ W) I. h/ lselect ename,salary,job , a; ^$ k) w7 |7 E. U. X5 B+ N' ?
from emp_ning
7 h' x5 S" D' s% F5 Ywhere deptno = (select deptno from emp_ning: q& G: k+ x) _. u( v
where ename = '刘苍松')
& z# T& B" I2 G& z5 @and ename <> '刘苍松';+ N. g( W; B. K1 i* D& T @, x
+ X+ s; Q: `; A0 J# a. A
--如果子查询得到的结果是多个,不能使用单行比较运算符3 K: n9 u: A% N, Q
等号。改为:in
4 O; e! M) e6 `% d$ |- Einsert into emp_ning(empno, ename, deptno)
9 q7 w6 A$ \; j% O& lvalues(1015, '刘苍松', 20);
) U" Y; {# s- D" A
( n% \4 a8 e$ y8 d& Iselect ename,salary,job,deptno $ C' |( B6 y/ ]6 V; l( t- [ @
from emp_ning: y% ^$ {$ ?. Q$ n# q% u
where deptno in (select deptno from emp_ning
* u: f( c& c6 X" x2 } where ename = '刘苍松')
9 d3 x5 K0 r: \# Qand ename <> '刘苍松';
% H: v M% U/ v8 D- G7 H4 f) G2 O5 V& [
根据子查询返回的结果的行数:0 o! g1 [$ Y: o( D9 |7 ]3 Z4 L$ s
返回一行:> < >= <= = <>2 c' |+ [! M% \- g( u t
返回多行:>ALL >ANY <ALL <ANY in
- D- C& P/ P5 H, D- K1 y. B& Z' C- m' w# x
3.每个部门拿最高薪水的是谁?; `, B6 A) \- ~# ~8 m" C
--返回多行
8 q' {: l1 u% `' ]+ W3 `) `select deptno, max(salary)" Z8 G% w, y! U" _ w( S' {3 T% a
from emp_ning p4 A* Q; C6 v' C- x
where deptno is not null3 T) d* M* c4 J5 E" v5 ~
group by deptno;5 x: X- ~! M( W
- i; s! _# ?7 [, T
30 50004 u, E5 J7 i- `, F9 V2 A: _* R
20 150009 L) G0 l1 b6 }7 d
10 10000
& g2 J4 G8 C' s. y& ]2 @0 c, T' _1 @8 c
select ename, salary, job, deptno+ Y" x( }" W" d( ^+ n V' ^( \! M
from emp_ning( _7 {- F9 s! }- I6 K+ M, u
where (deptno, salary)
9 q1 Y W' s( g1 r1 W in (select deptno, max(salary)% D! d/ s x% s1 {$ ?, F2 e9 h
from emp_ning- P5 A) ^2 x8 v7 q6 l
where deptno is not null# c- F# A: ?7 j5 L5 v2 C
group by deptno);
% x; a8 w3 Z1 P& n0 O- o
3 W5 c1 [6 C s5 G+ p9 E4.哪个部门的人数比部门30的人数多?) i7 J+ M0 {& D: y1 |* M
select count(*) from emp_ning
$ V' j7 K8 o3 n" { d7 _: vwhere deptno = 30;; g, z/ k9 L0 s7 q# Z i, `, U, O
3: o) y" n! Z9 B
2 l6 U8 A. U0 D: x! J+ j
select deptno, count(*)
* ?. s% H2 Z z5 K$ afrom emp_ning
! {- \% `8 Z8 j* V; b/ q! W$ C. K6 Lgroup by deptno7 D3 b% `% G5 b% n( M( y2 n
having count(*) > (select count(*) from emp_ning U# c* ~% g* V6 x) K
where deptno = 30);
, v/ Z+ }, \: T2 z* i/ x/ }
0 o/ Q% q' k. @8 a8 F3 z5.哪个部门的平均薪水比部门20的平均薪水高?+ ?5 d0 e) I/ M) H
select deptno, avg(nvl(salary,0)) avg_s
/ t1 }. e0 w3 U0 efrom emp_ning4 t5 ], l% x9 h$ a7 E8 i7 N: A/ Z
group by deptno
+ W) `+ n5 Y: W! E2 ]+ f5 Shaving avg(nvl(salary,0)) >, T9 ~8 ~- E) v! W
(select avg(nvl(salary,0))
8 f9 ?/ E5 V* w- F* ] from emp_ning
6 \( g0 u3 `- t9 ^! I where deptno = 20);
, F2 R M8 @7 F& m9 x- \+ }$ v4 D9 h/ c7 V* D5 Q& B* u
6.列出员工名字和职位,这些员工所在的部门平均薪水
4 ~ k, ^6 P6 l3 G$ N" D8 `7 ~大于5000元。. O8 C" ^9 S2 z
select deptno, avg(nvl(salary,0))# q4 ^' R$ W2 Y3 O
from emp_ning1 e* `5 u/ n+ ]+ E+ p
group by deptno
+ z( A3 k# j5 k( s1 c' ?% D& D4 h5 T' Hhaving avg(nvl(salary,0)) > 5000;; @& Q, L$ k# K9 y# J, q, ~% `
10 5750
) x$ V! E" p# G20 8000 " a& m1 _; u& t4 P
4 w5 b, W, i7 |! N5 Oselect ename, job
3 N/ g8 y; K0 r5 P; l0 n( z4 Z3 sfrom emp_ning
q! Z! W, i8 ?! N( owhere deptno in (select deptno9 z. x4 Z1 P: U* I! R# |& ?
from emp_ning: c( B. Z, W3 M* y1 }
group by deptno/ b! a0 n- ?7 u. D
having avg(nvl(salary,0)) > 5000);6 ~3 R$ `9 G" E$ O
% Z0 z, @7 T* I+ S f7.谁是张无忌的下属?: l" E8 ]6 @8 l9 |7 |+ v8 X
select empno from emp_ning$ L7 i! l& o1 d* F- y8 N
where ename = '张无忌';
. ]% e; _* E/ j2 v( f1001
- z7 ]9 k1 @: @. Y/ W1014+ d9 a' X, B0 o7 [3 j+ A% M$ X4 t
* E. a+ g* R0 F3 {4 Kselect ename from emp_ning' u8 b8 I+ g! `7 _6 [) A9 a
where mgr in (1001, 1014);
# B" w" b" w- \0 Y% N* A) h& d7 q7 H; z0 _ o
select ename from emp_ning8 q+ _) ?1 `9 w5 A; s6 l8 L9 k% m
where mgr in (select empno from emp_ning" B, a$ H' N% x( P5 d% Z
where ename = '张无忌');* C7 ^, G0 Y4 y/ G2 j
+ y+ V* j$ A' ]8.研发部有哪些职位?
; P2 _6 u! I% w% @7 F5 g, xselect distinct job from emp_ning, l7 `- t0 F9 i# H+ f
where deptno = (select deptno
7 g$ I8 |. B5 Q- s; b from dept_ning/ r# P. P6 [! g1 r2 z
where dname = '研发部');
% P \9 M% V2 S& t. t2 V: P7 b. |/ N, L! Y* H+ \/ d4 w
, ]; H/ \: |' H9 A' ~
|
|