Suppose I have a table named EMPLOYEE containing the following attributes
(EMPLOYEE_ID, LAST_NAME, FIRST_NAME, MIDDLE_NAME, JOB_ID, MANAGER_ID, Salary)
Can I Display the Nth highest salary drawing employee details Please help
Suppose I have a table named EMPLOYEE containing the following attributes
(EMPLOYEE_ID, LAST_NAME, FIRST_NAME, MIDDLE_NAME, JOB_ID, MANAGER_ID, Salary)
Can I Display the Nth highest salary drawing employee details Please help
ORDER BY
and LIMIT
where 10
is n + 1
:
SELECT
*
FROM
employees
ORDER BY
Salary DESC
LIMIT
10, 1
(If you want the first record, use LIMIT 0, 1
. For the tenth, use LIMIT 9, 1
etc.)
try this
put n > 1 to get corresponding results
n=3 must give you second 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)
Test Table
CREATE TABLE Test
(ID INT IDENTITY(1,1),
Salary INT)
INSERT INTO Test
VALUES (100), (200), (300), (400), (500)
SELECT * FROM Test
Query
SELECT TOP 1 Salary
FROM
(SELECT TOP 3 Salary FROM Test ORDER BY Salary DESC)q
ORDER BY Salary ASC
In your Sub-query SELECT TOP Nth the rest remains the same and it will get you the desired results