Table: Employee
+-------------+------+ | Column Name | Type | +-------------+------+ | id | int | | salary | int | +-------------+------+ id is the primary key (column with unique values) for this table. Each row of this table contains information about the salary of an employee.
Write a solution to find the second highest distinct salary from the Employee table. If there is no second highest salary, return null (return None in Pandas).
The result format is in the following example.
Example 1:
Input: Employee table: +----+--------+ | id | salary | +----+--------+ | 1 | 100 | | 2 | 200 | | 3 | 300 | +----+--------+ Output: +---------------------+ | SecondHighestSalary | +---------------------+ | 200 | +---------------------+
Example 2:
Input: Employee table: +----+--------+ | id | salary | +----+--------+ | 1 | 100 | +----+--------+ Output: +---------------------+ | SecondHighestSalary | +---------------------+ | null | +---------------------+
Problem Overview: Given an Employee table containing employee salaries, return the second highest distinct salary. If a second highest value does not exist, the query must return NULL. The key detail is distinct salary; duplicates should not affect the ranking.
Approach 1: Sort with DISTINCT and LIMIT/OFFSET (O(n log n) time, O(n) space)
This approach relies on sorting the salary column in descending order after removing duplicates. Use SELECT DISTINCT salary, sort using ORDER BY salary DESC, and then skip the highest value using LIMIT 1 OFFSET 1. The database engine performs a sort on the unique salary values, which usually costs O(n log n) time depending on indexing. Wrapping the query in a subquery allows returning NULL when fewer than two distinct salaries exist. This method is straightforward and widely supported across SQL databases.
Example pattern:
SELECT (SELECT DISTINCT salary FROM Employee ORDER BY salary DESC LIMIT 1 OFFSET 1) AS SecondHighestSalary;
Approach 2: MAX with Subquery Filtering (O(n) time, O(1) space)
This approach avoids sorting entirely. First compute the maximum salary in the table, then find the largest salary strictly less than that value. The query uses a nested condition like WHERE salary < (SELECT MAX(salary) FROM Employee), followed by another MAX() aggregation. The database scans the table and keeps track of the largest valid value below the top salary. Since no global sorting is required, the logical complexity is O(n) with constant additional space.
Example pattern:
SELECT MAX(salary) AS SecondHighestSalary FROM Employee WHERE salary < (SELECT MAX(salary) FROM Employee);
This pattern works well in relational systems because aggregate functions like MAX() are optimized internally. It also naturally returns NULL when a second distinct salary does not exist.
Both strategies rely on core concepts from SQL such as sorting, aggregation, and nested queries. Understanding how DISTINCT, ORDER BY, and aggregate functions interact with a database execution plan helps you reason about performance.
Recommended for interviews: The MAX() with subquery approach is typically preferred. It demonstrates understanding of SQL aggregation and avoids unnecessary sorting, which signals stronger query optimization instincts. The DISTINCT + ORDER BY solution is still valuable because it shows you understand ranking queries and SQL ordering semantics.
This approach involves directly querying the database to retrieve the second highest distinct salary. The SQL query utilizes the MAX function in a subquery to exclude the highest salary from consideration, thus efficiently targeting the second highest.
This C solution uses sqlite3 to connect to a database and execute a SQL query that selects the second highest salary from the Employee table. The MAX function is used within a sub-query to find and exclude the highest salary, thereby obtaining the second highest.
Python
Time Complexity: O(n) - Iterating through database rows
Space Complexity: O(1) - Constant additional space
This approach does not rely on SQL but instead employs language-specific paradigms, such as sorting or managing unique salary values in a list or array, to identify the second highest distinct salary.
This Java solution uses JDBC to connect to the SQLite database, retrieves all salaries, and uses a TreeSet to store unique salaries in descending order. The second highest salary, if available, is then retrieved by skipping the first element (highest).
JavaScript
Time Complexity: O(n log n) - Due to TreeSet insertion
Space Complexity: O(n) - Storage for unique salaries
| Approach | Complexity |
|---|---|
| Using SQL to Identify Second Highest Salary | Time Complexity: O(n) - Iterating through database rows |
| Using Language-Specific Features to Compute Second Highest Salary | Time Complexity: O(n log n) - Due to TreeSet insertion |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| DISTINCT + ORDER BY + LIMIT/OFFSET | O(n log n) | O(n) | When ranking results is acceptable and sorting operations are simple to express |
| MAX with Subquery Filter | O(n) | O(1) | When you want a more optimal query that avoids sorting and relies on aggregation |
LeetCode 176: Second Highest Salary [SQL] • Frederik Müller • 21,318 views views
Watch 9 more video solutions →Practice Second Highest Salary with our built-in code editor and test cases.
Practice on FleetCode