我希望在不使用sql中rank
或row_number
或dense_rank
等任何功能的情况下找到第三高薪部门
我的逻辑
select max(salary),deptno from emp
where salary not in
(select max(salary) from emp where salary not in
(select max(salary) from emp group by deptno)group by deptno)
group by deptno
这是我的查询.syntax是正确的,但它没有给出正确的答案/结果
答案 0 :(得分:0)
这应该做的工作
select salary, deptno from emp e where 2 =
(select count(distinct salary) from emp where
salary > e.salary and deptno = e.depto)
答案 1 :(得分:0)
使用MSSQL,
SELECT MAX(salary) as '3rdHighest', deptno
FROM EMP n WHERE 2 =
(SELECT COUNT(DISTINCT salary) FROM NewTbl
WHERE salary > n. salary and deptno = n.deptno)
GROUP BY deptno