Back to Browse

How to find nth highest salary in SQL | SQL Interview Question

174 views
Mar 10, 2022
8:15

How to find nth highest salary in SQL | SQL Interview Question |Query To Find Nth Highest Salary In SQL | SQL Interview Question |Nth highest salary in SQL, nth highest salary in Oracle, nth highest salary, nth highest salary in SQL with explanation, nth highest salary using correlated subquery Create table Employees ( EmpID int primary key identity, EmpName nvarchar(50), EmpSalary int ) select * from Employees Insert into Employees values ('John', 70000) Insert into Employees values ('Satosh', 60000) Insert into Employees values ('Kashish',45000) Insert into Employees values ('Mamta', 70000) Insert into Employees values ('Poonam', 45000) Insert into Employees values ('Tara', 30000) Insert into Employees values ('Vipin',35000) Insert into Employees values ('Shidharth',80000) --To find 2nd highest salary using Sub-Query SELECT TOP 1 EmpSalary FROM ( SELECT DISTINCT TOP 2 EmpSalary FROM EMPLOYEES ORDER BY EmpSalary DESC ) RESULT ORDER BY EmpSalary --To find 2nd highest salary using CTE WITH RESULT AS ( SELECT EmpSalary, DENSE_RANK() OVER (ORDER BY EmpSalary DESC) AS DENSERANK FROM EMPLOYEES ) SELECT TOP 1 EmpSalary FROM RESULT WHERE DENSERANK = 2 #SQL #SQLInterviewQuestion

Download

0 formats

No download links available.

How to find nth highest salary in SQL | SQL Interview Question | NatokHD