假设我有一个名为 EMPLOYEE 的表,其中包含以下属性
(EMPLOYEE_ID、LAST_NAME、FIRST_NAME、MIDDLE_NAME、JOB_ID、MANAGER_ID、工资)
我可以显示第N个最高工资绘图的员工详细信息吗?请帮忙
ORDER BY
LIMIT
哪里10
是n + 1
:_
SELECT
*
FROM
employees
ORDER BY
Salary DESC
LIMIT
10, 1
(如果你想要第一条记录,使用LIMIT 0, 1
。第十条,使用LIMIT 9, 1
etc.)
try this
put n > 1 to get corresponding results
n=3 必须给你第二个highest salary
SELECT * --This is the outer query part
FROM Employee Emp1
WHERE (N-1) = ( /* Subquery starts here */
SELECT COUNT(DISTINCT(Emp2.Salary))
FROM Employee Emp2
WHERE Emp2.Salary > Emp1.Salary)
测试台
CREATE TABLE Test
(ID INT IDENTITY(1,1),
Salary INT)
INSERT INTO Test
VALUES (100), (200), (300), (400), (500)
SELECT * FROM Test
询问
SELECT TOP 1 Salary
FROM
(SELECT TOP 3 Salary FROM Test ORDER BY Salary DESC)q
ORDER BY Salary ASC
在您的子查询 SELECT TOP Nth 中,其余部分保持不变,它将为您提供所需的结果