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
Смотрите видео How to find nth highest salary in SQL | SQL Interview Question онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь ALLCPL 10 Март 2022, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 163 раз и оно понравилось 6 людям.