Skip to main content

How To Find The Second Highest Salary In SQL Server

 

Introduction

 
In this article, I am going to explain to you how to find the second highest salary in various ways. This is one of the most common questions asked in an SQL interview.
 
Below are the several ways of finding the Nth highest salary:
  1. How to find the second highest salary in SQL Server using a Sub-Query
  2. How to find the second highest salary in SQL Server using a CTE
  3. How to find the 3rd, 5th highest salary
Here we will be using SQL Server 2017, or you can use SQL Server 2008 or above.
 
Read my previous Joins in SQL Server 2017 part of this article using the below links before reading this article:
Prerequisites
 
SQL Server 2017 or you can use SQL server 2008 or above version.
 
First, we will create a Database and a table.
 

Creating a Database and One Table

 
Step 1 - Create a Database
 
Open your SQL Server and use the following script to create the “EmployeeDB” Database.

  1. Create database EmployeeDB  

Now, select the script query then press F5 or click on the Execute button to execute the above script.
 
You should see a message, “Command(s) completed successfully.” This means your new database has been created.
 
Step 2 - Create a table
 
Open your SQL Server and use the following script to create a table “tbl_Employees”.
  1. Create table tbl_Employees  
  2. (  
  3.      Id int primary key not null identity(1,1),  
  4.      FirstName varchar(50),  
  5.      LastName varchar(20),  
  6.      Location varchar(20),  
  7.      Gender varchar(50),  
  8.      Salary int  
  9. )  
Execute the above query to create “tbl_Employees “.
 
You should see a message, “Command(s) completed successfully.”
 
Now, the data has been inserted into the table. 
  1. Insert into tbl_Employees values ('Chittaranjan''Swain','Odisha''Male', 80000)  
  2. Insert into tbl_Employees values ('Chandin''Swain''Pune','Female', 76000)  
  3. Insert into tbl_Employees values ('Mitu''Pradhan','Delhi''Male', 55000)  
  4. Insert into tbl_Employees values ('Jeni''Swain','Chennai''Female', 76000)  
  5. Insert into tbl_Employees values ('Adyashree''Swain','UK''Female', 49000)  
  6. Insert into tbl_Employees values ('Ram''Kumar','US''Male', 39000)  
  7. Insert into tbl_Employees values ('Jitendra''Gouad','Hydrabad''Male', 35000)  
  8. Insert into tbl_Employees values ('Dibas''Hembram','Bangalore''Male', 55000)  
Execute the above query, you should see a message, “Command(s) completed successfully.”
 
Now retrieve all data from the “tbl_Employees” table.
  1. select * from tbl_Employees  
output
 


 
How To Find Second Highest Salary

select distinct top 2 salary from tbl_Employees order by Salary desc 

Output
 

 
 
How To Find Second Highest Salary Using a Sub-Query
 
To Get the Second Highest Salary use a Subquery along with the Max() function

  1. Select Max(Salary) as Salary from tbl_Employees where Salary <(select MAX(Salary) from tbl_Employees)  

Output
 


 
How To Find Second Highest Salary Using a Sub-Query

  1. SELECT TOP 1 SALARY  
  2. FROM (  
  3.       SELECT DISTINCT TOP 2 SALARY  
  4.       FROM tbl_Employees  
  5.       ORDER BY SALARY DESC  
  6.       ) RESULT  
  7. ORDER BY SALARY  

OutPut
 



How To Find Second Highest Salary Using CTE

  1. WITH RESULT AS  
  2. (  
  3.     SELECT SALARY,  
  4.            DENSE_RANK() OVER (ORDER BY SALARY DESCAS DENSERANK  
  5.     FROM tbl_Employees  
  6. )  
  7. SELECT TOP 1 SALARY  
  8. FROM RESULT  
  9. WHERE DENSERANK = 2  

Output
 


 
How To Find the Third Highest Salary

  1. select distinct top 3 salary from tbl_Employees order by Salary desc  

Output
 


 
How To Find the Third Highest Salary Using Sub-Query

  1. SELECT TOP 1 SALARY  
  2. FROM (  
  3.       SELECT DISTINCT TOP 3 SALARY  
  4.       FROM tbl_Employees  
  5.       ORDER BY SALARY DESC  
  6.       ) RESULT  
  7. ORDER BY SALARY  

Output
 



How To Find the Third Highest Salary Using CTE

  1. WITH RESULT AS  
  2. (  
  3.     SELECT SALARY,  
  4.            DENSE_RANK() OVER (ORDER BY SALARY DESCAS DENSERANK  
  5.     FROM tbl_Employees  
  6. )  
  7. SELECT TOP 1 SALARY  
  8. FROM RESULT  
  9. WHERE DENSERANK = 3  

Output
 


 
Similarly, to find the 5th highest salary simply replace N with 5. 
 

Conclusion

 
In this article, I explained how to find the second highest salary with several ways in SQL Server with examples. I hope this article has helped you to understand this topic.
 
Post your valuable feedback in the comments section.

Comments

Popular posts from this blog

.NET Core API With Dapper, Repository And UnitOfWork

  Introduction In this tutorial, you will use ASP.NET Core Web API to create a web API that returns a list of brands. This article focuses on creating web API using dapper, repository pattern, and UnitOfWork pattern in .NET 8. I will guide you through the steps by step to create web API. HTTP is not just for serving up web pages. HTTP is also a powerful platform for building APIs that expose services and data. HTTP is simple, flexible, and ubiquitous. Almost any platform that you can think of has an HTTP library, so HTTP services can reach a broad range of clients, including browsers, mobile devices, and traditional desktop applications. ASP.NET Core Web API is a framework for building web APIs on top of the .NET Framework. Creating a New Project in Visual Studio Launch Visual Studio and select Create a New Project. Creating a Core Web API Start Visual Studio and choose Create a new project. In the Create a new project dialog, select ASP.NET Core Web API Click Next. Configuring You...

.Net framework interview questions

What is the .NET Framework? The .NET Framework is a set of technologies that form an integral part of the .NET Platform. It is Microsoft’s managed code programming model for building applications. The .NET Framework has two main components: Common Language Runtime (CLR): The CLR is one of the foundation in the .NET framework and provides a common set of services for applications developed on Microsoft .Net Technologies. .NET Framework class library: The .NET framework class library is a collection of reusable types and exposes features of the runtime. It contains of a set of classes that is used to access common functionality. What is CTS (Common Type System)? The common type system (CTS) defines how types are declared, used, and managed in the runtime, and is also an important part of the runtime’s support for cross-language integration. The common type system performs the following functions: ·          Establishes a framework that...

Move Selected Gridview Rows to Another Gridview in Asp.net

Introduction :  Here I will explain how to move or transfer selected checkbox gridview rows to another  grid v iew in asp.net using c#, vb.net or copy one gridview row to another gridview in asp.net using c#, vb.net. Description :  In previous posts I explained Display images in gridview from database in asp.net, Enable/Disable checkbox in gridview based on condition,  asp.net gridview examples  and  bind data to textbox control in gridview ,  Bind data to dropdownlist  in gridview  and many articles relating to  asp.net,  c#, vb.net, c#,vb.net. Now I will explain how to move selected rows from one gridview to another gridview in asp.netusing c#, vb.net. To implement this first we need to write the code in aspx page like this  < html   xmlns ="http://www.w3.org/1999/xhtml"> < head > ...