该用户从未签到
复习:
3 q, l% [8 Y% |6 }0 E: c 1.查询语句的基本格式。
- m ?9 h% N, z3 c% C0 F select ...
6 D2 R8 M1 P3 A# O4 B4 I3 ]% a2 N from 表名1 }; Y c3 v [2 c5 I
where 条件& h( F" ]- Q. X0 b! K e
group by 列名1 {* C$ M. Q& B0 w
having 带组函数的条件% V5 F+ f' y; s0 z V
order by 列名" R5 G8 f& G B k* P" Z
/ _; f! v& N7 Z+ k) O
2.函数
* ?( [3 T b! ` 1)组函数:count / avg / sum / max / min
8 w+ ?3 z$ q9 ^% \# ?( J
6 q0 `" v8 X- G 2)单行函数:: F3 g1 c2 ^# u6 b2 y2 {
a.字符函数:upper / lower / initcap
3 O. g2 R. F/ W/ p% W1 T E length / lpad / rpad
Q7 `, z$ |3 B
) }8 X* ~- D$ a; X l : left ; r: right: r+ c$ j- Y0 Z7 L& a
+ ^4 D" B' A$ m! t3 {4 x8 Y h5 Y select rpad(ename, 10, '*') from emp_ning;
' `2 y' g$ r8 J4 Q; E 8 ]. E6 f0 |6 ?: B9 O" l* r: F
b.数字函数: round / trunc / mod+ I, M( j( a' u f4 ^! g& s3 b
select mod(salary, 1000) from emp_ning;
9 c# L4 s/ y1 x2 T. _ j * c" Y, R6 B# F& `: ]+ W: y: ?
c.日期函数: months_between / add_months / 0 E' @3 t' y( {
last_day(sysdate); A# n: D, J: c2 Y5 S
2 x* n( T. f( v' \ U4 M7 W
d.转换函数: to_char / to_date / to_number. Y- g3 L, \, W7 v9 K/ Z$ p
( x g5 T, v) l7 B' m" V0 B to_char to_number6 x. T! G9 l) [7 R
日期 ---------> 字符 ---------->数字( b4 j- p9 Z: H/ |
<--------- <---------/ F2 Z7 K( @$ U2 v$ D
to_date to_char
3 S4 x% Y6 E4 J& j% `# f
- H1 C! Q. x, r! u
0 V9 x' b$ U+ y8 Y9 R where password = '1234' and id = '1001'
1 G3 r, K9 Y7 e& x/ E2 B , Q& U8 T& P7 e! p3 M1 ^6 N
select to_number('$7,912,345.67','$9,999,999.99')! ?) U) @: N8 J% @
from dual;$ i5 q+ b, C8 A2 W, I
' y# D5 T& ^0 b2 j, S& }, W2 L 7912345.67! @3 ^7 A* }* N/ c5 J. X
, g( c7 F/ P* L* r( q) m. t
select to_char(7912345.67, '$9,999,999.99')$ [9 }9 i) J. ]4 q
from dual;
5 f4 X7 p# z+ R' w
( h* m( ~& T- e0 f+ n $7,912,345.67! [% H( L H8 D
' {' K# X8 g. N5 ^3 u: s7 ~* [ e.通用函数:nvl / coalesce / decode( J" f0 ]5 ~3 q; W$ b( K! G2 I
, B& S. t( s1 ]: G5 ^/ Z: g 使用频率比较高的单行函数:$ @! W/ a/ l; a0 f" ]+ B
upper / round / to_char / to_date / nvl- W8 |& J1 R6 w8 l. i5 K7 J& v
3 T' F. x3 A0 ~* N. B- G / I% _, s: @1 W, S
一、子查询
* v& j- T0 [9 W4 p2 x 1.谁的薪水比张无忌高?
t) l( ?& P3 Q0 @) h. U: o& P: c5 G. d 子查询
- @1 @- k1 @! K+ G; p) F select salary from emp_ning |- L* R4 R3 H. V {+ V5 h0 R
where ename = '张无忌';* H5 {( f6 V7 R
! k4 K s. I+ m' ?7 j# c
10000
8 A; G& {, ]4 c; ?' v- Z8 k0 a
- u6 I. A. g) m1 [ 主查询
- F7 Y; i T/ g! v- B! D% T select ename from emp_ning% W7 K! ~. y& a" F$ |
where salary > 10000;; b. u% q1 N0 z L) E9 C# {
9 g0 \% v4 l3 ]
张三丰
l! s( J; ]$ a# V/ ?! F/ n 3 q* p q1 i% {6 S% p
合并到一条SQL语句:
7 n$ Z: E5 L3 X0 E, n select ename from emp_ning
- g) @7 J! S: y, Z9 d3 k where salary > (select salary from emp_ning1 t1 N# e; d+ o+ B. |
where ename = '张无忌');
6 s2 U; V. Q9 G1 `& R1 E9 D
6 T% C8 Q" {, ?. d$ i& P > < >= <= = <>: 单行比较运算符,* x2 R2 I/ M7 K7 Y* k( s
只能和一个数字比较。
; m _) `0 G1 K/ B
3 a9 M7 |; ?! T5 v insert into emp_ning(empno, ename, salary)
0 K) G* x5 J, \5 i values(1014, '张无忌', 8000);" Z# d, `. S6 W y% M
* m* P# [8 s* |$ R- H9 P* } Q 谁的薪水比所有叫张无忌的人的薪水都高?大于最大( ^ M& R% a9 {; j0 T3 o' E
100003 y) g3 C7 [8 H
8000
2 n/ F% @# C! R+ ?: b + X1 [1 Y# K3 f; C+ y5 j
select ename from emp_ning
5 L* W; u* M* ?6 {8 d+ P where salary > ALL(select salary from emp_ning
: ]- i# Q8 J$ V& Z6 n& g6 I where ename = '张无忌');
- f4 A5 N9 [5 P/ \" y- j; I
) ]! y! F s- c2 v- s) L: B 哪些人的薪水比任何一个叫张无忌的人高都行?大于最小
2 b+ g$ o0 E9 e/ \/ j1 G 10000
# e' Y# i$ ~- N0 N4 j" R/ b 8000- S M, f+ Y" _, v: Q
' s4 Z4 }6 N+ Z* ?1 C" v select ename from emp_ning
5 \% h) `6 D9 h8 h. S8 e where salary > ANY(select salary from emp_ning' F- p$ b; |6 ]( R o8 }1 @/ J0 u
where ename = '张无忌');! j. \" i" q( ]# l9 x" z V% J9 T
" O" @- d V/ g4 \0 _2 h 2.谁和刘苍松同部门?列出除了刘苍松之外的员工名字。
% u" r9 A! H. K select deptno from emp_ning
% G/ `6 \) n; p, O where ename = '刘苍松';6 u/ u2 S2 F. m1 X) w
10
7 S+ V* i$ g/ e/ a% f: X # }4 d% A6 I3 i
select ename from emp_ning
/ c$ A3 c4 _- q' m, w where deptno = 10
0 {9 }: ~# |+ N6 O& p$ y/ } and ename <> '刘苍松';. @9 x/ ^8 l$ ^4 c! A8 } [2 Y
% B- p" V! N) Y6 @9 ~0 G
select ename,salary,job
! ]2 r+ T! I3 g' v; Y, n from emp_ning
. ~6 b* _6 c' U6 l! Q b5 @5 X where deptno = (select deptno from emp_ning
7 M9 F J. @) ]( l where ename = '刘苍松')- W- l$ H' c" n; l i
and ename <> '刘苍松';# q) `5 P1 v- A1 a
: M1 b$ o& V* |6 X --如果子查询得到的结果是多个,不能使用单行比较运算符
l' R6 g4 [7 W, H2 C 等号。改为:in
' ?" X4 V2 N' {2 S- r insert into emp_ning(empno, ename, deptno)
9 \# W, c( W! z values(1015, '刘苍松', 20);
6 i$ r) ?7 F% b* s/ k- z* v
* N* V n6 L: l( h L select ename,salary,job,deptno / i( V0 s$ W' F/ n8 Z5 A
from emp_ning
1 m; Y) c4 }; e where deptno in (select deptno from emp_ning
/ p5 q- I# ~' d2 @/ D& Q+ g' s5 b where ename = '刘苍松')
1 n( [+ `5 e, ?! |# @5 a# p+ `+ h and ename <> '刘苍松';
4 }0 j( F+ _7 I5 \# q5 A
1 i6 d- z1 d# f" | I3 D- `; D4 z5 W 根据子查询返回的结果的行数:
4 `8 D C: k# R- w 返回一行:> < >= <= = <>
, e+ v" A* B$ ? 返回多行:>ALL >ANY <ALL <ANY in5 @0 i! s4 |( X$ q! \5 T: j5 C
* x" M+ B+ k0 Y 3.每个部门拿最高薪水的是谁?2 @- l0 u8 W" _! G! q9 r: @
--返回多行( g& \2 I$ R* }
select deptno, max(salary), J; q1 @2 ]3 }8 c
from emp_ning
! ]. Z+ H6 c1 ^5 \6 Z* [ where deptno is not null
* Y W M: D7 \& A$ e4 y group by deptno;- z% `5 ^! O; [ F
5 ?+ |* N$ j: F1 X$ H 30 5000
+ ]! t0 I+ F" l& L- i" H 20 15000
% i; W! d9 C1 j, P 10 10000% u6 K/ o+ r2 D' }8 m. f9 D4 k7 t+ ?! r
9 i+ v% _) d- Y# z7 k- [
select ename, salary, job, deptno3 z3 l6 S0 c7 Y
from emp_ning
- F! X4 [8 V3 K% _ where (deptno, salary)
1 d5 U: c# E; E# ?" n1 F; W U in (select deptno, max(salary)% a1 P4 X3 z( F$ E4 V6 n1 `" G
from emp_ning; W6 ^: x) c8 r) @5 K- K
where deptno is not null
2 {$ s* h' n+ G1 z group by deptno);. L' U; Q% s8 t, C* C' `1 ]
$ Q/ y$ u7 j+ N* R- x w 4.哪个部门的人数比部门30的人数多?: n3 K) i3 }& p+ u( D+ Z
select count(*) from emp_ning
$ b7 o2 j9 a; v" X2 u where deptno = 30;
8 U) b- @. y0 E2 Q/ F 3
6 O2 U* G$ F* ]* c5 E' F! T 4 a; s! z! v ^0 e4 C) f, i: i- s
select deptno, count(*)
% o2 S- n" F2 y2 i2 W from emp_ning3 y/ V$ v/ |/ z# N- y
group by deptno9 }8 l" s3 H$ E8 r
having count(*) > (select count(*) from emp_ning
6 m8 K. Q8 [. `2 t% v where deptno = 30);- ?* K: L4 j* P ?
5 j8 G: }, x- u; N$ x 5.哪个部门的平均薪水比部门20的平均薪水高?: B' t2 I; v8 r8 ]
select deptno, avg(nvl(salary,0)) avg_s# F, z" a7 |. Q5 m! ? L3 u
from emp_ning
) E) R7 E0 |* C' s" K2 A group by deptno
" r9 u+ j M$ ?4 `9 j! x; T6 Y having avg(nvl(salary,0)) >& F. T7 X; v6 j/ s& Q0 H* x8 ]
(select avg(nvl(salary,0))( ~9 k9 {! B; ?- L$ t
from emp_ning
. z7 V5 l4 H) B- Y where deptno = 20);& \: U$ h7 R0 w
9 k$ r1 J2 w: G C 6.列出员工名字和职位,这些员工所在的部门平均薪水) s- Y E }+ T% g+ A
大于5000元。3 e' G- a- M) x4 L
select deptno, avg(nvl(salary,0)); R. B' ~+ }8 \6 `0 _
from emp_ning1 c+ j7 \& n! N" d
group by deptno
( D. A( d8 e% u4 o' {6 _6 N having avg(nvl(salary,0)) > 5000;2 _( N3 ^7 C, {; L( \; f) B+ `
10 5750' f$ B' m- D7 e$ }
20 8000 & k8 v8 g% N* B4 P; S- s
: b$ ~2 [ s$ V4 H& h% \
select ename, job% ^5 G4 f7 \" S; \/ G
from emp_ning
: f! t* t! F3 q o1 d4 q where deptno in (select deptno8 p! |: N; p, O8 f: l& z, E H$ A
from emp_ning
l& r2 W: V3 B C8 x group by deptno6 M: Y; [7 e% U* K* Z
having avg(nvl(salary,0)) > 5000);3 X/ L$ Z! A1 C l4 T* M$ V( P
) W2 F o9 G, _) ^0 G 7.谁是张无忌的下属?; [2 ^. K; j6 C
select empno from emp_ning8 h9 `2 m: q" B f8 P& m
where ename = '张无忌';
# G" K% e" Y5 {7 U1 a 1001
5 z5 g- m/ S. _: K. a+ v0 s! O 1014
; e- m6 W# O0 g* ~5 b , m C, k, P; c+ W( X7 P# H/ s
select ename from emp_ning
3 \% c0 K/ X; V" U where mgr in (1001, 1014);
: [3 e( e# R& Q, u. u- K 0 G+ ~" j: f& q! R
select ename from emp_ning9 B' i3 i" k( \( t
where mgr in (select empno from emp_ning, j8 G6 N5 w9 P& u6 C
where ename = '张无忌');
% s; P: G) h. {; ^( P3 ^. `; ~, m 6 L. a% \& x0 _; e; G2 N6 O6 |
8.研发部有哪些职位?
) a' A+ P- m" D- S! c0 i/ c select distinct job from emp_ning4 Y: s! p9 j L& ]- W9 I
where deptno = (select deptno
7 i$ A3 {& @7 g from dept_ning
/ {* \! Q4 v" ^* z3 W$ _1 _ where dname = '研发部');
: a z+ a4 s0 w7 o1 ~! Q 5 d2 H# p# U$ `; R4 Z" U
2 k! Y9 q- C/ Y0 s: R1 ~0 A
科帮网 1、本主题所有言论和图片纯属会员个人意见,与本社区立场无关2、本站所有主题由该帖子作者发表,该帖子作者与科帮网 享有帖子相关版权3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和科帮网 的同意4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意7、科帮网 管理员和版主有权不事先通知发贴者而删除本文
JAVA爱好者①群:
JAVA爱好者②群:
JAVA爱好者③ :