& J/ L& u K7 i3 }组函数:count / avg / sum / max / min 忽略空值 , J$ D% L$ c& o. M# W1 d其中:avg / sum 针对数字的操作。2 z; M. \& `5 G0 _" S& O4 e6 l/ n
max / min 对所有数据类型都可以操作。2 E: U: a4 }0 r. u# c
" c- }& b2 F l. g; c6 Y+ h13.计算最早和最晚的员工入职时间。7 i. b8 M. P0 U. e# R6 }
select max(hiredate) max_hiredate,: G. E" ?, i* b$ K5 B& Y& e
min(hiredate) min_hiredate7 n- e/ L q" C+ a D
from emp_ning; " y7 Q3 j4 l, u! B9 r ( R. n% L8 Y2 e2 Z9 u) u14.按部门计算每个部门的最高和最低薪水分别是多少?# |8 u. K+ E0 _% _ ^4 ?
格式如下: ( Y% Z, U8 Z1 e! ?10 4500 3000 9 i. s* r5 `+ H20 15000 8000 2 S, p( u1 J" g4 W( t' T# S5 v30 10000 5000 ~" S! _+ A& p2 R- [; ^, t
' q" j" o/ ?2 H--group by 列名 : 表示按哪个列分组 + V0 ?- n- D5 ^/ b$ [% q' Cselect deptno, max(salary) max_s, min(salary) min_s : S+ t8 @6 g3 f5 dfrom emp_ning, T/ D3 j2 a* o0 N
group by deptno;$ Z1 \/ i5 n3 @) T6 N
. {" L, G2 @1 |, H% G# X
15.计算每个部门的薪水总和和平均薪水? # @+ j7 c* k9 u! u* ^9 Lselect deptno, sum(salary) sum_s, - O* n/ O# }$ J1 ^7 t( U3 _# f W' R avg(nvl(salary,0)) avg_s 1 B1 D. A3 j) W: U3 @from emp_ning : `; Y4 f# w4 g4 c* h' W5 F J7 wgroup by deptno;' a0 ?) o* |6 f1 t9 J+ e
# d3 v M% G7 d
16.每个部门的统计信息:格式如下: 3 M, l# R3 r. c9 e$ O4 udeptno max_s min_s sum_s avg_s emp_num$ s4 u- V# @* Q* d3 [; s
10 10000 5000 23000 6789 3 / K$ t. m0 d- D.... 1 {( |% o/ [: N' dselect deptno, max(salary) max_s, : I) y# w* a. h( z& R min(salary) min_s, 4 O. F, G9 m# Z" l4 p2 S2 [0 N sum(salary) sum_s, : Y9 o- ~; K, L" O avg(nvl(salary,0)) avg_s, , u [: B: W2 ^5 h count(*) emp_num. @" q5 c9 O- D; r+ j# H
from emp_ning ! C2 h4 ]8 E4 M9 J N- O7 c. qgroup by deptno; ) n0 Y8 Q( Z0 G! D& t& c) P5 d D# \) ^. m
select后出现的列,凡是没有被组函数处理的列,必须 $ j% [# D. ~/ C: D U2 Q% o出现在group by 短语中。 ! l G+ U: J: h) Z5 x7 T3 \: g, }9 Z1 y2 C$ W0 v# k
按职位分组,每个职位的最高、最低薪水和人数?) ]' O) H- U) Q/ f3 |9 V9 m- h, R
select job, max(salary) max_s, , y6 c2 {4 D, c" n min(salary) min_s,' a5 ~# L2 @; z
count(*) emp_num: a; N( a+ b- ]( x* M, f& i& f. ?
from emp_ning 8 ~; }/ p K8 m! {group by job & U7 N$ I% X) |" z. yorder by emp_num; 0 `4 T A/ ^! y3 M" Y6 p1 K; c2 x$ A17.平均薪水大于5000元的部门数据?( B$ l N& r% Y1 J+ W5 d' W* y/ I
select deptno, avg(nvl(salary,0)) avg_s & g$ x$ [$ u; v% J" A% Ifrom emp_ning : g$ U- O: O: G( h% b8 pwhere deptno is not null2 }! o9 s+ G* z4 X0 C/ {# K: _# J
group by deptno1 g, z1 y, H5 ^( N# j% i/ _+ C0 j5 J
having avg(nvl(salary,0)) > 5000; . b& V6 `8 Z- G. ]: f! r 5 i3 _4 p- h3 W$ v; h$ o3 o# i18.薪水总和大于20000元的部门数据? # S, E, v8 z- e; f& {select deptno, sum(salary) sum_s9 B# P! G# N& z% |+ k' j6 F
from emp_ning - C! F9 i+ E) |2 P4 F1 Swhere deptno is not null3 C( X9 Z& T- P$ Q w1 x& W5 Y ]6 K
group by deptno& f/ \( x( R0 c5 B: Z0 S
having sum(salary) > 20000;$ z# e& x: P& Q- s i- M9 m
) t/ p8 H( I- J" A3 c$ O, ?0 G( t
19.哪些职位的人数超过2个人?9 _- r* q) L4 r" S, r9 E
select job, count(*) emp_num " ?9 ?( |6 Z6 ~$ @3 M% V& y Rfrom emp_ning! {. S) j1 Z' m4 {3 l
where job is not null 8 e4 f. ]% d$ F: N' {group by job 9 ]- G. _1 j! |% `3 phaving count(*) > 27 y' R: _7 O; z. C7 ?6 v
order by emp_num; ; ^/ @6 n( R0 n/ }/ F. } 3 y. d1 V+ ], d( @3 a( t: I/ F