sql子查询和分组依据
我正在为我的sql类分配一个任务,我似乎无法理解。以下是所需选择的说明: 显示工资高于所在部门同事平均工资的所有员工,仅限至少有4名员工的部门 我已经找到了部分查询,比如sql子查询和分组依据,sql,oracle11g,Sql,Oracle11g,我正在为我的sql类分配一个任务,我似乎无法理解。以下是所需选择的说明: 显示工资高于所在部门同事平均工资的所有员工,仅限至少有4名员工的部门 我已经找到了部分查询,比如 select ename from emp where sal > any (select avg(sal) from emp group by deptno); 获取收入超过平均
select ename
from emp
where sal > any (select avg(sal)
from emp
group by
deptno);
获取收入超过平均工资的员工姓名
或
获取每个部门的员工人数
但不知何故,它无法将它们联系在一起。也许有人能帮我解释一下。你可以将Having子句与groupby连用
只需使用AND子句输入第二个查询:
select ename
from emp
where sal > any (select avg(sal)
from emp
group by
deptno)
and deptno in (select deptno
from emp
group by
deptno having count(deptno) > 4);
一个简单的解决方案。谢谢
select ename
from emp
where sal > any (select avg(sal)
from emp
group by
deptno)
having count(*)>4;
select ename
from emp
where sal > any (select avg(sal)
from emp
group by
deptno)
and deptno in (select deptno
from emp
group by
deptno having count(deptno) > 4);
select ename
from (
select deptno, count(deptno)
from emp
group by deptno
having count(deptno) > 4) valid_depts join emp ON emp.deptno=valid_depts.deptno
where sal > any (select avg(sal)
from emp
group deptno);