Find Second and Third Highest Salary in SQL Query Techniques

If you're interested in knowing the highest salary in SQL, you might have already learned how to use the MAX function. But what if you want to find the second or third highest salary? The MAX function alone won't suffice. You'll need another approach to handle this scenario. Let's explore how to find the second and third highest salaries in SQL using a combination of subqueries and common table expressions (CTEs).

Find the Second Highest Salary in SQL (Interview Favorite)
Find the Second Highest Salary in SQL (Interview Favorite)

To demonstrate, let's assume we have an 'Employees' table with the following structure:

the top 20 highest paying freelance skills by the hour infographical poster
the top 20 highest paying freelance skills by the hour infographical poster
ID Name Salary
1 John Doe 50000
2 Jane Smith 60000
3 Bob Johnson 55000

Finding the Second Highest Salary

SQL in 60 days
SQL in 60 days

The goal here is to find the employee with the second highest salary. To do this, we'll first find the maximum salary, then exclude it from the result set and find the highest remaining salary.

Here's how you can achieve this using a CTE:

7 SQL Projects for Beginners | Build a Real Data Portfolio
7 SQL Projects for Beginners | Build a Real Data Portfolio

Using Common Table Expressions (CTEs)

CTEs allow you to define temporary results sets within a SELECT, INSERT, UPDATE, or DELETE statement. They can make your queries more readable and efficient. Here's how you can use a CTE to find the second highest salary:

WITH SalaryCTE AS ( SELECT Salary, RANK() OVER (ORDER BY Salary DESC) as Rank FROM Employees ) SELECT Salary FROM SalaryCTE WHERE Rank = 2;

7 SQL QUERIES EVERY STUDENT SHOULD MASTER
7 SQL QUERIES EVERY STUDENT SHOULD MASTER

Using a Subquery

Alternatively, you can use a subquery to achieve the same result. A subquery is a query nested inside another query. Here's how you can use a subquery to find the second highest salary:

SELECT Salary FROM Employees WHERE Salary < (SELECT MAX(Salary) FROM Employees) ORDER BY Salary DESC LIMIT 1;

the top 10 sol queries to master windows functions for each employee in their department
the top 10 sol queries to master windows functions for each employee in their department

Finding the Third Highest Salary

To find the third highest salary, you can apply a similar approach. After excluding the highest and second highest salaries, you'll be left with the third highest. Let's see how to do this using both methods.

SQL Subqueries Explained with Examples | Beginner SQL Tutorial
SQL Subqueries Explained with Examples | Beginner SQL Tutorial
a notepad with some writing on it that says, what is row - number?
a notepad with some writing on it that says, what is row - number?
the top 10 important sol queries for data analists info sheet, with text below
the top 10 important sol queries for data analists info sheet, with text below
SQL Query Challenge #6 | Find the Highest-Paid Employee(s) in Each Department
SQL Query Challenge #6 | Find the Highest-Paid Employee(s) in Each Department
SQL Career- Explore the Job Opportunities
SQL Career- Explore the Job Opportunities
the top 50 project ideas list is shown in orange and black, with text on it
the top 50 project ideas list is shown in orange and black, with text on it
Learn SQL Fast 🚀 | Beginner SQL Commands Cheat Sheet for Data Analysts
Learn SQL Fast 🚀 | Beginner SQL Commands Cheat Sheet for Data Analysts
a table that has some words on it
a table that has some words on it
Information Security Analyst Salary
Information Security Analyst Salary
Proven Ways to Double Your Salary in Two Years
Proven Ways to Double Your Salary in Two Years
SQL Query Challenge #5 | Find Customers Who Spent More Than Customer 104
SQL Query Challenge #5 | Find Customers Who Spent More Than Customer 104
Top Skills That Increase CA Salary in India (2026)
Top Skills That Increase CA Salary in India (2026)
four different types of soljoins with the names and numbers in each section on them
four different types of soljoins with the names and numbers in each section on them
the data table for each company's products and their price ranges are shown in blue
the data table for each company's products and their price ranges are shown in blue
SQL Joins Explained with Examples | Complete JOIN Cheat Sheet for Beginners
SQL Joins Explained with Examples | Complete JOIN Cheat Sheet for Beginners
What jobs can you get if you learn SQL?
What jobs can you get if you learn SQL?
Learn SQL just by investing 30 minutes a day within 30 Days
Learn SQL just by investing 30 minutes a day within 30 Days
SQL RIGHT() Explained with Examples | Extract Characters from the Right in SQL Server
SQL RIGHT() Explained with Examples | Extract Characters from the Right in SQL Server
SQL Aggregate Functions Cheat Sheet for Beginners
SQL Aggregate Functions Cheat Sheet for Beginners
SQL Query Challenge #4 | Employees Earning Above Department Average
SQL Query Challenge #4 | Employees Earning Above Department Average

Using CTE

You can extend the previous CTE to find the third highest salary like this:

WITH SalaryCTE AS ( SELECT Salary, RANK() OVER (ORDER BY Salary DESC) as Rank FROM Employees ) SELECT Salary FROM SalaryCTE WHERE Rank = 3;

Using a Subquery

Similarly, you can adjust the previous subquery to find the third highest salary:

SELECT Salary FROM Employees WHERE Salary < (SELECT MAX(Salary) FROM Employees) AND Salary > (SELECT MAX(Salary) FROM Employees WHERE Salary < (SELECT MAX(Salary) FROM Employees)) ORDER BY Salary DESC LIMIT 1;

SQL is a powerful tool with numerous functions and methods that allow you to analyze and manipulate data in numerous ways. Knowing how to use these techniques can help you to extract meaningful insights from datasets. Now that you've learned how to find the second and third highest salaries in SQL, you can apply these techniques to other scenarios and continue to expand your SQL skills.

Related Articles

Sql Code Example W3schools Grant Select W3schools K3 Database K3s Migrate From Sqlite To Etcd W3 Query Params Enum Sql Server Sql Where With 3 Conditions Ms Sql Xpath Query Example W3schools Variables Sql W3schools Xslt