Part 1 How to find nth highest salary in sql

preview_player
Показать описание
If you are a foodie like me, I am sure you will enjoy the recipes on my friend's YouTube channel. If you find them useful, please subscribe and share to support her. She's really good at what she does.

Link for all dot net and sql server video tutorial playlists

Link for slides, code samples and text version of the video

This is a very common SQL Server Interview Question. There are several ways of finding the nth highest salary.

By the end of this video, we will be able to answer all the following questions as well.
How to find nth highest salary in SQL Server using a Sub-Query
How to find nth highest salary in SQL Server using a CTE
How to find the 2nd, 3rd or 15th highest salary

Let's use the following Employees table for this demo

Use the following script to create Employees table
Create table Employees
(
ID int primary key identity,
FirstName nvarchar(50),
LastName nvarchar(50),
Gender nvarchar(50),
Salary int
)
GO

Insert into Employees values ('Ben', 'Hoskins', 'Male', 70000)
Insert into Employees values ('Mark', 'Hastings', 'Male', 60000)
Insert into Employees values ('Steve', 'Pound', 'Male', 45000)
Insert into Employees values ('Ben', 'Hoskins', 'Male', 70000)
Insert into Employees values ('Philip', 'Hastings', 'Male', 45000)
Insert into Employees values ('Mary', 'Lambeth', 'Female', 30000)
Insert into Employees values ('Valarie', 'Vikings', 'Female', 35000)
Insert into Employees values ('John', 'Stanmore', 'Male', 80000)
GO

To find the highest salary it is straight forward. We can simply use the Max() function as shown below.
Select Max(Salary) from Employees

To get the second highest salary use a sub query along with Max() function as shown below.
Select Max(Salary) from Employees where Salary [ (Select Max(Salary) from Employees)

To find nth highest salary using Sub-Query
SELECT TOP 1 SALARY
FROM (
SELECT DISTINCT TOP N SALARY
FROM EMPLOYEES
ORDER BY SALARY DESC
) RESULT
ORDER BY SALARY

To find nth highest salary using CTE
WITH RESULT AS
(
SELECT SALARY,
DENSE_RANK() OVER (ORDER BY SALARY DESC) AS DENSERANK
FROM EMPLOYEES
)
SELECT TOP 1 SALARY
FROM RESULT
WHERE DENSERANK = N

To find 2nd highest salary we can use any of the above queries. Simple replace N with 2.

Similarly, to find 3rd highest salary, simple replace N with 3.

Please Note: On many of the websites, you may have seen that, the following query can be used to get the nth highest salary. The below query will only work if there are no duplicates.
WITH RESULT AS
(
SELECT SALARY,
ROW_NUMBER() OVER (ORDER BY SALARY DESC) AS ROWNUMBER
FROM EMPLOYEES
)
SELECT SALARY
FROM RESULT
WHERE ROWNUMBER = 3
Рекомендации по теме
Комментарии
Автор

Thank you for the resources, they are very helpful

AbelMengistuweb
Автор

6 years passed from the video posted in May 2014. And today in May 2020 I still found it very helpful. Great job done for explanation and preparation. It is the best tutorial I have ever met in video, plus it has links to the slides and code. BRAVO! TOP JOB!

mangorez
Автор

When it comes to SQL learning ..Your Channel is the only thing comes to my mind. Really appreciate your hard-work.

rohitpatil
Автор

Hi Venkat, I really appreciate your effort in creating all these tutorials.It helped me to get 2 job offers.Your explanations and examples are very vivid.Thanks for your time and effort.Keep going !! I have been recommending your channel to many friends and will keep doing it.

mirrapriya
Автор

This is just a beauty, I really loved the new Dense_Rank Function, kudvenkat you are the man. Thanks a million.

ga
Автор

Last part where you showed the difference with row_number, it was a great finisher indeed. My concept got clear about the ranking functions. Thanks a lot @kudvenkat

shirshodatta
Автор

Venkat, so grateful to your instructions! Your tutorials are comprehensive and inspiring, and get me away from fears coding the queries. I have followed 6 parts and will be keeping on learning. Thank you again!

sarahlee
Автор

the way you explain any concept is simply amazing! thank you so much!

dimple
Автор

I got first job by following your videos for learning.... and its been 2 years experience and still following your videos so that can get better opportunity. I am sure its gonna work again...XD

shantilal_suthar
Автор

Your explanation is very precise, I love the way u first write sub query first and then the outer query..it just fits in mind quickly.
One more thing others uses rownumber function, coz they first make it distinct rows and accordingly top number is given for req. Salary.

shanksa
Автор

it's 2024 and this video is still helpful for me. Thank you so much kudvenkat

codewithcreativecoder
Автор

I revisited this video to refresh my mind. I enjoyed watching this video and recommend others to watch. Topic has been explained in crisp and clear. Thanks Guru Venkat

krismaly
Автор

Hello Venkat, I am thank full to you that you create a great tutorial and yes this is the only tutorial which lead me to a great job. The efforts you made are very helpful for many peoples. You Rocks Man.

AbdulWahab-liri
Автор

Hi Venkat, I have recently come across your videos. these videos are actually gold mines. Thank you very much for explaining is such a simple way.

himanshurustagi
Автор

Iam Raja Sekhar recently i watched your vedios about SQL for beginner's it's very useful to me. Full explanation it's amazing, it's easy to understand .thanks lot for ur helping the viewer by giving free knowledge to us .once thanks for alot.

rajasekharreddy
Автор

Your way of explanation is amazing and unique Thank you so much because you made my skills much polished from zero to hero. I was unable to prepare anything but you made me the most confident one. Thanks once again

humanitylover
Автор

Watching this vedio after 9 years, still this question is asked in every interview ... Thank you for explaining in such a giod way 😊

poojagore
Автор

Thanks Venkat,  
Excellent You are awesome dear, not only this one but all the other videos specially the SQL are fabulous and very much informative and very easy to understand. Your way of teaching is very much understandable and easy.
We have learnt a lot from your video series on all the programming fields C#, SQL Server etc.
Be blessed and thanks a lot.

KhalidAfridi
Автор

You're a genius. I've been searching for this. Your explanation is everything. Thanks for sharing

nonoobott
Автор

Thanks Kudvenkat for your clear description.

shekharmishra