2nd Max Salary in SQL with Example: Query the Second Highest Wage Efficiently

When working with large datasets in SQL, it's not uncommon to need information beyond the highest salary. Perhaps you're curious about the second highest salary in your employees table, or you're looking to identify a specific salary bracket for recruitment purposes. Whatever your reason, SQL provides several ways to retrieve the second maximum salary.

SQL in 60 days
SQL in 60 days

Before we dive into the examples, let's ensure you understand our dataset. We'll be working with a table named 'employees' that has the following structure:

Monthly Salary Overview Dashboard
Monthly Salary Overview Dashboard

```sql CREATE TABLE employees ( id INT PRIMARY KEY, name VARCHAR(50), salary DECIMAL(10, 2) ); ```

Using Subquery with LIMIT and OFFSET

The most straightforward method to find the second highest salary is by using a subquery with the LIMIT and OFFSET clauses.

I will create professional salary sheet in your company
I will create professional salary sheet in your company

Here's an example:

```sql SELECT salary FROM ( SELECT DISTINCT salary FROM employees ORDER BY salary DESC LIMIT 2 OFFSET 1 ) as subquery; ```

Explanation of the Subquery Approach

SQL Query Challenge #5 | Find Customers Who Spent More Than Customer 104
SQL Query Challenge #5 | Find Customers Who Spent More Than Customer 104

The subquery orders the salaries in descending order and fetches the first two distinct values using the LIMIT clause. The OFFSET clause then skips the first row, ensuring we get the second highest salary.

Do note that this method might not work as expected if there are multiple employees with the second highest salary, as it orders by salary and may skip some distinct salaries due to the LIMIT clause.

Using Self-Join

a table that has some words on it
a table that has some words on it

Another approach is to perform a self-join on the employees table. This method ensures that if there are multiple employees with the same second highest salary, they will all be returned.

Here's an example:

```sql SELECT e1.salary FROM employees e1 JOIN employees e2 ON e1.salary < e2.salary GROUP BY e1.salary HAVING COUNT(*) = ( SELECT COUNT(*) FROM employees WHERE salary > e1.salary ); ```

Explanation of the Self-Join Approach

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

The self-join compares each employee's salary with every other employee's salary. The join condition ensures that we only keep the salaries that are less than another salary. The having clause then excludes the highest salary and returns the second highest.

This method can be resource-intensive because it compares each row with every other row. However, it's the most reliable way to find the second highest salary when there could be multiple employees with the same salary.

Learn SQL Fast ๐Ÿš€ | Beginner SQL Commands Cheat Sheet for Data Analysts
Learn SQL Fast ๐Ÿš€ | Beginner SQL Commands Cheat Sheet for Data Analysts
a blue and white resume with circles on it
a blue and white resume with circles on it
Download Salary Sheet Excel Template - ExcelDataPro
Download Salary Sheet Excel Template - ExcelDataPro
Learn SQL just by investing 30 minutes a day within 30 Days
Learn SQL just by investing 30 minutes a day within 30 Days
a table that has some type of data on it, including numbers and symbols for each subject
a table that has some type of data on it, including numbers and symbols for each subject
SQL GETDATE() Explained with Examples | Current Date & Time in SQL Server
SQL GETDATE() Explained with Examples | Current Date & Time in SQL Server
2.4K views ยท 102 reactions | ๐ŸŽฏ SQL Cheatsheet โ€“ Everything You Need at a Glance Master SQL fundamentals with this clean and powerful SQL Cheatsheet ๐Ÿš€ Perfect for beginners, developers, data analysts, and inte | Codewithprashantt
2.4K views ยท 102 reactions | ๐ŸŽฏ SQL Cheatsheet โ€“ Everything You Need at a Glance Master SQL fundamentals with this clean and powerful SQL Cheatsheet ๐Ÿš€ Perfect for beginners, developers, data analysts, and inte | Codewithprashantt
SQL Basics Made Easy ๐Ÿ’ป | SELECT, WHERE & ORDER BY with Examples
SQL Basics Made Easy ๐Ÿ’ป | SELECT, WHERE & ORDER BY with Examples
๐Ÿ’ท Want to know how much salary you need to take home a specific amount?
๐Ÿ’ท Want to know how much salary you need to take home a specific amount?
a blue and white poster with text on it
a blue and white poster with text on it
SQL Career- Explore the Job Opportunities
SQL Career- Explore the Job Opportunities
What Is a Database in SQL? | SQL Database for Beginners
What Is a Database in SQL? | SQL Database for Beginners
an image of a table with numbers and tables for each piece in the game, which is
an image of a table with numbers and tables for each piece in the game, which is
SQL Concepts Every Analyst Should Understand
SQL Concepts Every Analyst Should Understand
167K views ยท 467 reactions | Brought to you by programmingvalley  #SQL #database | Programming Valley
167K views ยท 467 reactions | Brought to you by programmingvalley #SQL #database | Programming Valley
Ready-To-Use Simple Salary Sheet Excel Template - MSOfficeGeek
Ready-To-Use Simple Salary Sheet Excel Template - MSOfficeGeek
SQL Transactions Explained ๐Ÿ’พ
SQL Transactions Explained ๐Ÿ’พ
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
Complete SQL Cheat Sheet for Beginners | SQL Commands, Joins, Queries & Database Guide
Complete SQL Cheat Sheet for Beginners | SQL Commands, Joins, Queries & Database Guide
SQL queries don
SQL queries don

In practice, the choice between these methods depends on the specific requirements of your task and the size of your dataset. The subquery approach is simpler and often sufficient, while the self-join can be more reliable but may require more resources.

To explore further, consider playing with some sample data and testing both methods to see which one works best for your needs.

Related Articles

Xslt When Example Ms Sql Xpath Query Example 2 Highest Salary In Sql Sql Primary Key 3 Columns Oracle Sql Xml Query Examples W3schools Sql User Sql Query Key Words Join 3 Tables Sql With Where Year Sql Example Max Salary In Sql