select查询例子总结

1、求每个部门的平均薪水
select avg(salary) from emp group by deptno;
2、求薪水最高的人的名字
select ename from emp where sal=
( select max(sal) from emp);

3、查询工资大于1200雇员,按部门编号进行分组,分组后平均薪水大于1500,按工薪倒充排列.

select ename

from emp

where sal>1200

group by deptno having avg(sal)>1500

order by avg(sal) desc;

4、那些人工资,在平均工资之上.

select ename,sal from emp

wheresal>(select avg(sal) from emp)

5、查找每个部门挣钱最多的那个人的名字.

select ename from emp join

(select max(sal) max_sal,deptno from emp group by deptno) t_max //t_max为每个部门最高工资表

on (emp.deptno=t_max.deptno and emp.sal=t_max.max_sal);

6、把某个人的名字以及他的经理人的名字求出来

select e.ename c_ename, d.ename d_ename from emp e,emp d

where e.mgr = d.empno

7.求部门平均薪水的等级。

select deptno, avg_sal, grade from salgrade sjoin

(select deptno,avg(salary) avg_salfrom emp group by deptno) t_avgsal

on t.avg_sal between s.losal and s.hisal;

8、那些人是经理

select ename from emp where empno in

(select distinct mgr from emp);

9、不用组函数,求薪水的最高值

select distinct sal from emp where

sal not in

(selectdistinct e1.sal t_sal from emp e1 join emp e2on e1.sal<e2.sal);

10、平均薪水最高的部门编号

select deptno,avg(sal) avg_sal from emp group by deptno

havingavg_sal =

(select max(avg(sal)) from emp group by deptno);

11、平均薪水最高的部门名称

 

select dname from dept

where deptno=

(select deptno =

(select deptno,avg(sal) avg_sal from emp group by deptno

havingavg_sal =

(select max(avg(sal)) from emp group by deptno)

)

);

12、比普通员工最高工资还要高的经理人(非老大)

select ename from emp where empno in

(select distinct deptno from emp where deptno is not null)

and sal>(

select max(sal) from emp where empno not in

(select distinct deptno from emp where deptno is not null)

);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值