0% found this document useful (0 votes)
29 views1 page

SQL Interview Question

The document discusses different SQL queries to find the nth highest salary from a table of employee salaries. It shows methods to find the 2nd and 3rd highest salaries using CTEs with DENSE_RANK and ROW_NUMBER window functions.

Uploaded by

sendhilmca
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views1 page

SQL Interview Question

The document discusses different SQL queries to find the nth highest salary from a table of employee salaries. It shows methods to find the 2nd and 3rd highest salaries using CTEs with DENSE_RANK and ROW_NUMBER window functions.

Uploaded by

sendhilmca
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

Nth highest salary

select TOP 1 salary from


(select DIstinct top 3 salary
from employees
Order by salary Desc)
Result
Order By Salary

-- CTE -- 2ND HIGHEST SALARY

with Result AS
{
Select Salary, DENSE_RANK() OVER (Order by Salary Desc) as Denserank
from Emplyees
}
Select TOP 1 Salary
From Result
Where RESULT.DENSERANK = 2

-- --------------row nuMBER FUNCTION , doesnt work with duplicate values

wITH Result As
{
Select Salary, ROW_NUMBER() over (Order by Salary DESC) as RoweNumber
From Employees
}
select top 1 Salary
From RESULT
wHERE RESULT.ROWNUMBER=3

You might also like