在芝加哥工作的人中,谁的工资最高(两种方法,大同小异的)
select * from (select e.ename from emp e join dept d on(e.deptno = d.deptno)where d.loc=‘CHICAGO‘ order by sal desc) where rownum=1;
select * from emp e,dept d where e.deptno=d.deptno and d.loc=‘CHICAGO‘;
查询每个部门下有多少员工
select dname,count(ename) from dept d left outer join emp e on d.deptno =e.deptno group by dname;
查询除去 salesman 所有平均工资超过 1500 的部门(同上)
select d.dname,e.job,avg(e.sal) from emp e join dept d on d.deptno=e.deptno where e.job!=‘SALESMAN‘
group by d.dname,e.job having avg(e.sal)>=0;
select * from(select deptno,avg(sal),job from emp t group by t.deptno,job having avg(sal) > 1500)a where a.job <> ‘SALESMAN‘;
查询在 new york 工作的所有员工的姓名,部门名称和工资信息
select ename,d.dname,sal from emp e join dept d on d.deptno=e.deptno where d.loc=‘NEW YORK‘;
查询姓名为 King 的员工的编号,名称跟部门(同上)
select a.empno,a.ename,a.deptno,d.loc from dept d join(select t.empno,t.ename,t.deptno from emp t where t.ename=‘KING‘)a on a.deptno=d.deptno;
select empno,ename,d.dname from emp e left join dept d on d.deptno = e.deptno where e.ename=‘KING‘;
查询各种工作的最低工资
select job,min(sal) from emp group by jpb;
查询工龄大于10年的所有员工信息
select job,ename,hiredate from emp where (sysdate-hiredate)/365>10;
查询每个部门员工数量,平均工资和平均工作年限(同上)
select count(*),avg(sal),avg((sysdate-hiredate)/365) from emp group by deptno;
select dname,count(ename),avg(sal+nvl(comm,0)),avg(to_char(hiredate,‘yyyy‘)) from dept d left outer join emp e on d.deptno = e.deptno group by dname;
统计各部门每个工种的人数,平均工资。
select dname,count(ename),avg(sal+nvl(comm,0)) from dept d left join emp e on d.deptno = e.deptno group by dname;
查询从事同一种工作但不属于同一部门的员工信息。
select distinct job,deptno from emp;