该用户从未签到
|
复习:
+ K0 n" Q0 d. x- j1 V t! \( ]1.查询语句的基本格式。
/ E7 w3 B# U, Y1 M" Lselect ...2 }7 y& n+ r$ t# a: u- I8 i4 r5 k
from 表名- ]" X3 } I! X5 B7 I" A+ o9 w
where 条件 y0 H0 L6 U( u3 U! |+ D5 ^
group by 列名
7 j- A/ C8 U H+ g: o7 N$ ehaving 带组函数的条件: q+ M, \" K/ y. ~/ |9 D! Z6 g5 C
order by 列名 s% r6 ~, u, P* l3 M* U2 \& K; @7 ~7 u
9 R& u* S+ H! e0 C
2.函数
! ?9 R; |0 z4 _& @) B1)组函数:count / avg / sum / max / min6 B8 J8 [3 ^7 F9 b, ~( v _
5 r8 N/ a+ m- a! O
2)单行函数:; K# ^% q, I5 h" M
a.字符函数:upper / lower / initcap
! l; z2 R2 n6 e6 V& h6 ^ length / lpad / rpad
8 _/ A3 }7 X8 [1 Y. X' |1 L# k/ z, w
& p' T1 x! `# A$ Q& t* ^ l : left ; r: right
$ s1 c; t; v+ ^2 Y' C* K" d$ J1 s; T' f! m
select rpad(ename, 10, '*') from emp_ning;
5 @- { m t& h
) W8 O1 l; j4 p9 x$ @) rb.数字函数: round / trunc / mod
( x( c( H5 l7 K2 W3 x" i# cselect mod(salary, 1000) from emp_ning;
, f% r9 [0 L+ f4 \6 X1 @3 N8 G
; q9 m$ ]6 y, _4 j7 R9 s* Pc.日期函数: months_between / add_months / - [: d7 d1 p4 V7 P( G
last_day(sysdate)# f" l; l. K8 y9 Y( [3 W( _; G) d
9 Q' S+ N. ~# X, M \6 n% ^8 A' H
d.转换函数: to_char / to_date / to_number
" v' N l) U/ O, v$ f1 x
4 F) t0 x2 L' A9 n8 g* F to_char to_number
1 O" t* m1 X* a) J! K- r9 `& X 日期 ---------> 字符 ---------->数字
0 D% ~! P& v8 I8 F! ]1 \ c0 m <--------- <---------6 N/ w' x5 h* w' `* {- a6 e5 k
to_date to_char @* W, M+ j" l* t, L- B* J
5 W8 J; t8 M% w T4 k2 N: y" u" W9 b6 X5 H* j; H# ~ O
where password = '1234' and id = '1001'" Q+ M/ s. U* k' o: A7 o5 ~ j
' s/ A3 g/ [8 h4 M select to_number('$7,912,345.67','$9,999,999.99')
# m8 Z0 n0 W; [! D from dual;8 j, Y1 o" @3 [' b
% ?. l; j: }6 ~2 X7912345.67
8 l i, x- j6 A2 {3 B, e/ w0 F0 V% l s0 g6 X- |/ R
select to_char(7912345.67, '$9,999,999.99')
9 X, b; A: c) s( ufrom dual;
- y: @ J7 `* Q$ g. c/ m8 E1 b" I3 L( P! c% J' A" j
$7,912,345.67
4 {+ U1 X& k c* v3 o- a, s9 {- \0 z1 R1 G
e.通用函数:nvl / coalesce / decode
/ \9 L* s5 p1 _+ [. q0 Q& }# p$ V" X- h/ j) O% Z0 q" x
使用频率比较高的单行函数:. V( L7 b' N& S; t* P- r7 y
upper / round / to_char / to_date / nvl
9 d( n! x3 I( _$ r# Y1 h% h- P( J/ M8 h1 V. Y2 b1 H
. r& V) ^# N# R' J& t5 Z一、子查询
8 N, A/ r7 w5 M$ {* z+ w- p7 I) x1.谁的薪水比张无忌高?
; x# v% L8 |8 I" D: t6 O子查询% J) n$ ~/ m0 `1 P: v
select salary from emp_ning& z C" q/ [, t' r* K
where ename = '张无忌';( [! s' z, _- s+ g7 ]
$ v B% ~! ?+ Y/ a5 R
10000
1 f2 k. I; N$ m( w6 k( M& o
- ~2 H( t+ c) | D7 N主查询$ d R4 k& h5 [" ?( i: x
select ename from emp_ning
5 M$ j9 _: Y/ T4 s2 }% Q1 v3 uwhere salary > 10000;
) C) Z- D' ?0 o. |! y2 \9 ^/ @0 b1 L* l$ v
张三丰
& \( ]- n+ ^- k, s! i X C+ Q" m2 d* W4 K7 w
合并到一条SQL语句:
, K8 r6 N ^6 ?select ename from emp_ning8 s U9 N) c ]9 R `# D
where salary > (select salary from emp_ning& J) H7 L+ u, [
where ename = '张无忌');! }) d, _; T3 D+ J3 a: d& [; k
/ u; v% I0 C" Y; e/ U6 ?( b7 _
> < >= <= = <>: 单行比较运算符,
6 H6 j& q! n' T只能和一个数字比较。
# S6 ?. \" R* P- n p2 y1 |
3 Q8 R/ E+ j3 s6 ?8 h2 ]1 {insert into emp_ning(empno, ename, salary)9 Y+ y% i( k5 g8 }' q* A
values(1014, '张无忌', 8000);# x* P' ?$ i2 @/ r( V1 n' t7 [# R
$ E$ [) a7 ~4 Z2 x8 M- [9 T8 L
谁的薪水比所有叫张无忌的人的薪水都高?大于最大! ~# L1 X2 S; A! n
100009 w7 s/ k" [4 z$ D* k* T! p; r
8000
. b7 o6 T( W+ `5 ?; ^9 ^9 o8 x. g5 |* z( N
select ename from emp_ning
' ~( `8 x& O$ n/ Lwhere salary > ALL(select salary from emp_ning8 i7 e* @2 Z8 K# i
where ename = '张无忌');1 s S6 S9 ^# Y6 q4 J: ^ |9 |% J5 P
2 {4 O) p1 ?5 e. p5 H
哪些人的薪水比任何一个叫张无忌的人高都行?大于最小, _9 @. y, |; c1 k0 J7 O M
10000
, z: m0 z9 H4 Y& J7 p' L& b8000
4 o+ K- k3 V% @: h0 {9 i5 @4 H% A0 L3 I+ }* G
select ename from emp_ning
2 x' E# ?' Q8 L; K9 R5 r' |% {where salary > ANY(select salary from emp_ning; B) S4 Q0 e4 {# e# {& b1 o' H
where ename = '张无忌');
" f2 H9 b3 s+ O& H x, o0 _+ x* t5 D ]6 ^ b( y7 i( p1 ?! Q
2.谁和刘苍松同部门?列出除了刘苍松之外的员工名字。2 ]3 F2 g; c ^0 ?$ W- ~* ]: h6 Q
select deptno from emp_ning
# ~) q% L. I& {1 Q' cwhere ename = '刘苍松';7 l$ I' Q6 X+ g
109 b, h; P+ o, W" q3 ^) J% \$ V8 K
) [* s6 p4 s: J3 d$ h3 B( A
select ename from emp_ning
) N; Q" Q. z$ @; ]where deptno = 10
# ]3 R3 v+ [% x" t7 `and ename <> '刘苍松';) D* v3 X+ s7 n) a" S
3 g* q j7 T/ P4 |) ^" C, r, X
select ename,salary,job 1 Z1 j- y p5 w3 s P1 ?" }6 Z
from emp_ning, Y) q' w% t6 d ?( D% D8 n! S
where deptno = (select deptno from emp_ning
' x8 ?$ t9 b! _) ]& D( |7 i1 g where ename = '刘苍松')8 p1 A, q9 w" A0 P& K$ L* K; S
and ename <> '刘苍松';1 R! X1 x% n( Y/ f( x2 d$ Z: J. o1 S
^( r3 O2 ~2 u$ Z+ D--如果子查询得到的结果是多个,不能使用单行比较运算符0 h. `9 x4 A d6 C0 `% h. T
等号。改为:in6 s+ h4 i/ ]" d Q2 M4 H5 j& g, h
insert into emp_ning(empno, ename, deptno)
5 I5 [* J& Q/ J* O) K; pvalues(1015, '刘苍松', 20);
( g/ B& v% c/ B! \. _/ d7 _! {; A) c+ n1 t8 g
select ename,salary,job,deptno ! i! E1 x. ]- m+ o& z6 U) B
from emp_ning
+ F+ h: Q- ]) L7 K8 I. qwhere deptno in (select deptno from emp_ning7 B1 s3 P) u1 j+ q% V! x7 K1 U3 O
where ename = '刘苍松')) \) q T- q7 g. u- o" |1 e
and ename <> '刘苍松';, `: d& `2 F. _/ [: ]' Z3 C
- d) n ~6 T: n6 T* i% g根据子查询返回的结果的行数:# q9 ~; a1 L$ ^: p3 a2 g8 |' J* s
返回一行:> < >= <= = <>
8 Q: ]! o0 N# L7 l- c- F$ o& Y0 K返回多行:>ALL >ANY <ALL <ANY in
- y# f" P; Z. `3 R
, T6 `2 q. `7 b2 w, h3.每个部门拿最高薪水的是谁?2 l o9 s) T. M2 N; w. Y
--返回多行
; T" m. I; W2 Hselect deptno, max(salary) T$ Z$ v3 @: L$ `2 V- h. M
from emp_ning
* H, S4 U8 f' E! R! Fwhere deptno is not null+ v+ j& \" ]4 X, i' g, a$ U+ c
group by deptno;& k6 t# B4 {$ I. [" t
$ g, U8 g( W) R$ _& S
30 5000
* d; W1 V z$ {3 a 20 15000
' N6 F) a' S/ S l7 J5 H3 T) o 10 10000
- @" t5 Y2 I1 I% l' w+ e, B% [0 |' B8 w0 M2 i' \
select ename, salary, job, deptno8 h7 K m1 o. N% }' z1 g* F; J
from emp_ning* E$ R, E9 y8 c& p
where (deptno, salary) 7 f) c7 W! e1 d9 d. N! }' J
in (select deptno, max(salary)* Z3 q: l) S K" P. q$ |. d
from emp_ning& m/ H+ M3 {" Z% T0 X
where deptno is not null
- D: [4 D# _' [ c- |3 r2 Z group by deptno);7 V# I$ Y9 I1 O
+ }. L# h* @, e+ ^8 l! u5 `4.哪个部门的人数比部门30的人数多?
! T2 O1 {2 `+ c* Zselect count(*) from emp_ning5 ^8 x- j- V: X1 W: k" Y: q. ]8 B
where deptno = 30;0 G: P. ^, X. h! ?6 e2 C
3
0 K3 Y; I# E& [; U" ]$ [
Q3 c7 Q0 }& Y$ W0 k4 M iselect deptno, count(*)
/ ?7 D3 }# V, {( Hfrom emp_ning
- \2 s h" n( Ggroup by deptno
, K! z4 \! ~( a2 p S6 [3 thaving count(*) > (select count(*) from emp_ning& c# t) @# Q! A% i
where deptno = 30);
0 \4 S& U/ X! H% `) I) g) |8 ^# P& O$ m- {: {8 ~0 \4 E* v2 M( o
5.哪个部门的平均薪水比部门20的平均薪水高?. [! J/ b5 S. y2 v& a% @2 W9 u
select deptno, avg(nvl(salary,0)) avg_s* z; H# e, x2 M- M
from emp_ning2 r% B0 `5 u1 e9 Z& Z ^. q7 G
group by deptno
5 t; L$ Y8 v# xhaving avg(nvl(salary,0)) >9 T; p% S1 r9 B9 A) G! ?' W
(select avg(nvl(salary,0))( u6 u2 g2 K( j
from emp_ning
6 \6 @4 r( N' p3 [ where deptno = 20);
6 |- h0 v- t) \9 W
/ a" I0 ]+ F: C: e6.列出员工名字和职位,这些员工所在的部门平均薪水
9 R& _4 M D; j( I$ T大于5000元。" `: L0 m* a: a6 e, U, I5 K
select deptno, avg(nvl(salary,0)). I2 N7 k' e# f( V) ~
from emp_ning, ~* Z4 u) S5 M5 V+ c* ~% Q
group by deptno) @/ A% \, l0 a3 E3 f9 ~
having avg(nvl(salary,0)) > 5000;) f7 [/ b1 a* D
10 5750
} D( N3 C7 G% `* X. {$ C2 X+ T20 8000 " H. y3 q7 B# g- `% @
7 Q$ I$ b- G7 E# k* z0 L( `select ename, job
1 |; n) K. N- i+ l7 G* M G* V4 sfrom emp_ning8 M7 m+ n% e7 J8 K
where deptno in (select deptno
/ z, a* u4 m# P& m, F; } from emp_ning3 }0 M. _, Y' x8 l7 H
group by deptno/ [. N. ?0 y0 i6 p, c
having avg(nvl(salary,0)) > 5000);
7 Y. z8 }. I+ K8 x. m# A# V& y- m; j
7.谁是张无忌的下属?
^2 o7 }; e, X& O7 [select empno from emp_ning. m! |# i( a9 @9 W7 Z* r
where ename = '张无忌';) @3 u+ z% a, {. G! [* n
1001
+ L; H( J# |9 ]* [5 B' M5 n8 \* c- \1014" v6 r" d* J0 D* k* ?
" N7 Y+ }) {7 L& E
select ename from emp_ning+ w4 p* x6 ?) Z7 `; C, B
where mgr in (1001, 1014);& w4 }4 P6 d0 Y3 s0 T
* @+ R+ P: |6 R6 Y @* @' `select ename from emp_ning
: v: O% D5 ?/ Z" i. P( P2 ?9 I. u7 lwhere mgr in (select empno from emp_ning
( \% x1 x' D3 g# ?6 H where ename = '张无忌');) A' v' _& T" j
[- r# X% w2 k ~0 V, P; q0 w# m8.研发部有哪些职位?8 v' a+ h6 ?% _
select distinct job from emp_ning
# f7 @6 d! g2 V4 H( swhere deptno = (select deptno* ?. {7 o3 s( u9 U2 t" S# W
from dept_ning- u! b; T/ J; F4 r. [
where dname = '研发部');7 H& y4 {5 o; F* b+ u m' A+ T
+ @% T6 R8 }+ Y0 D0 ?. B
! c. Z0 i; Y9 d; _9 G9 j7 ?! _
|
|