You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
SQL>select deptno,to_char(avg(sal),'L99999.99') avg_sal,to_char(sum(sal),'L99999.99') sum_sal from emp group by deptno;
DEPTNO AVG_SAL SUM_SAL
---------- ------------------- -------------------30 $1566.67 $9400.0020 $2175.00 $10875.0010 $2916.67 $8750.00
3. 部门工资总和的最大值为多少
SQL>selectmax(sum(sal)) from emp group by deptno;
MAX(SUM(SAL))
-------------10875
4. 部门工资总和最多的部门名称和工资总和
SQL>select deptno,sum_sal from (select deptno,sum(sal) sum_sal from emp group by deptno order by sum_sal desc ) where rownum <2;
DEPTNO SUM_SAL
---------- ----------2010875
5. 工资总和超过9000的部门
SQL>select deptno,sum(sal) sum_sal from emp group by deptno having sum_sal >9000;
select deptno,sum(sal) sum_sal from emp group by deptno having sum_sal >9000*
ERROR at line1:
ORA-00904: "SUM_SAL": invalid identifier
SQL>select deptno,sum(sal) sum_sal from emp group by deptno havingsum(sal) >9000;
DEPTNO SUM_SAL
---------- ----------3094002010875
注意having字句后面不可以使用别名。
where字句不可以过滤组函数运算后的结果。
6. 雇员中工资相同的员工分别是谁,工资为所少?
SQL>selecte1.ename,e2.ename,e1.salfrom emp e1,emp e2 wheree1.sal=e2.salande1.ename!=e2.ename;
ENAME ENAME SAL
---------- ---------- ----------
MARTIN WARD 1250
WARD MARTIN 1250
FORD SCOTT 3000
SCOTT FORD 3000
7. 相同的工资有几个?
SQL>select sal,count(sal) from emp group by sal;
SAL COUNT(SAL)
---------- ----------245015000113001125022850129751110013000280011600115001
SAL COUNT(SAL)
---------- ----------950112 rows selected.
SQL>select sal,count(sal) from emp group by sal havingcount(sal)>1;
SAL COUNT(SAL)
---------- ----------1250230002
SQL>select sal from emp group by sal havingcount(sal)>1;
SAL
----------12503000
SQL>select ename,sal from emp where sal in (select sal from emp group by sal havingcount(sal)>1);
ENAME SAL
---------- ----------
WARD 1250
MARTIN 1250
SCOTT 3000
FORD 3000
8. 每一年参加工作的雇员的数量
SQL>selectcount(to_char(hiredate,'yyyy')) enum, to_char(hiredate,'yyyy') year from emp group by to_char(hiredate,'yyyy');
ENUM YEAR
---------- ----219871198011982101981
SQL>selectsum(case when to_char(hiredate,'yy')='81' then 1 else 0 end) "81",sum(decode(to_char(hiredate,'yy'),82,1,0)) "82",sum(decode(to_char(hiredate,'yy'),80,1,0)) "80",sum(decode(to_char(hiredate,'yy'),87,1,0)) "87"from emp;
81828087---------- ---------- ---------- ----------10112
课后练习
selectmax(sal),min(sal),sum(sal),avg(sal) from emp;
selectcount(*) from emp;
selectcount(*) from emp where deptno=30;
selectcount(deptno) from emp;
selectcount(distinct deptno) from emp;
selectcount(comm) from emp;
selectavg(comm) from emp;
selectavg(nvl(comm,0)) from emp;
select deptno,sum(sal) from emp group by deptno;
select deptno,job,sum(sal) from emp group by deptno,job;
select deptno,sum(sal) from emp havingsum(sal)>9000group by deptno;
--------------------------------10部门的最大工资
--------------------------------
查找重复的工资
--------------------------------80年 81年 82年 87年都有多少新员工
1980198119821987---- ---- ---- ----11012--------------------------------