没有使用等级或其他功能的第三高薪部门

时间:2013-09-05 16:24:06

标签: sql-server database

我希望在不使用sql中rankrow_numberdense_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是正确的,但它没有给出正确的答案/结果

2 个答案:

答案 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