Skip to main content

Highest Salaries Difference - Solution & Explanation

EasyPremiumFree on FleetCodeDatabase3 min read
Practice this problem

Problem Statement

Table: Salaries

+-------------+---------+ 
| Column Name | Type    | 
+-------------+---------+ 
| emp_name    | varchar | 
| department  | varchar | 
| salary      | int     |
+-------------+---------+
(emp_name, department) is the primary key (combination of unique values) for this table.
Each row of this table contains emp_name, department and salary. There will be at least one entry for the engineering and marketing departments.

Write a solution to calculate the difference between the highest salaries in the marketing and engineering department. Output the absolute difference in salaries.

Return the result table.

The result format is in the following example.

 

Example 1:

Input: 
Salaries table:
+----------+-------------+--------+
| emp_name | department  | salary |
+----------+-------------+--------+
| Kathy    | Engineering | 50000  |
| Roy      | Marketing   | 30000  |
| Charles  | Engineering | 45000  |
| Jack     | Engineering | 85000  | 
| Benjamin | Marketing   | 34000  |
| Anthony  | Marketing   | 42000  |
| Edward   | Engineering | 102000 |
| Terry    | Engineering | 44000  |
| Evelyn   | Marketing   | 53000  |
| Arthur   | Engineering | 32000  |
+----------+-------------+--------+
Output: 
+-------------------+
| salary_difference | 
+-------------------+
| 49000             | 
+-------------------+
Explanation: 
- The Engineering and Marketing departments have the highest salaries of 102,000 and 53,000, respectively. Resulting in an absolute difference of 49,000.

Approach Overview

Problem Overview: The task asks you to compute the difference between the highest salaries from specific departments in a company table. Instead of comparing every employee manually, the goal is to use SQL aggregation to extract the maximum salary for each relevant department and subtract the values.

Approach 1: GROUP BY with MAX Aggregation (O(n) time, O(1) space)

The cleanest solution uses SQL aggregation. Scan the table once and compute the maximum salary for each department using MAX(). A GROUP BY groups rows by department, allowing the database engine to track the highest salary per group. Once the maximum salary for each department is known, calculate the difference between the departments directly in the query. Since the database processes the table in a single pass, the time complexity is O(n) where n is the number of rows. Only a few aggregated values are stored, so the space complexity is O(1).

Many implementations use conditional aggregation instead of returning multiple rows. For example, MAX(CASE WHEN department = 'Engineering' THEN salary END) extracts the highest engineering salary while another conditional expression captures the marketing maximum. Subtracting these two values returns the required difference in a single result row. This avoids extra joins or nested queries.

This technique relies heavily on SQL aggregation patterns such as SQL, GROUP BY, and conditional expressions commonly used in database interview questions. The database engine handles grouping and maximum tracking efficiently, making the query both readable and performant.

Recommended for interviews: Interviewers expect a simple aggregation-based query. Demonstrating GROUP BY and MAX() shows you understand how to summarize grouped data in SQL. More complicated subqueries technically work, but the aggregation approach is shorter, clearer, and typically what reviewers look for in database interview questions.

Solution

We can first calculate the highest salary for each department, and then calculate the difference between the two highest salaries.

Code

MySQL

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
GROUP BY with MAX aggregationO(n)O(1)Best general solution when you need the highest salary per department
Conditional aggregation with CASE + MAXO(n)O(1)When you want both department values and their difference returned in a single row
Subquery for each departmentO(n)O(1)Readable but slightly more verbose; useful for beginners learning SQL aggregation

Video Solution

Leetcode 2853 - Highest Salaries Difference - Solved by Everyday Data Science | ABSOLUTE difference • Everyday Data Science • 633 views views

Watch 2 more video solutions →

Frequently Asked Questions

Is Highest Salaries Difference easy or hard?
Highest Salaries Difference is classified as an Easy database problem. It mainly tests familiarity with SQL aggregation functions like MAX() and how to compute grouped values efficiently.
Highest Salaries Difference Python/Java solution
This problem is designed for SQL databases rather than general-purpose languages. The expected solution is a SQL query using MAX() and GROUP BY or conditional aggregation in MySQL.
How to solve Highest Salaries Difference in O(n)?
Use aggregation instead of comparing rows manually. Apply MAX() with conditional filtering for each department to capture the highest salary values during a single table scan. Subtract the aggregated values to get the final difference, which keeps the complexity at O(n).
What is the best approach for Highest Salaries Difference?
The best approach uses SQL aggregation with MAX() and conditional expressions. Compute the highest salary for each department using MAX(CASE WHEN department = ... THEN salary END) and subtract the results. The query scans the table once, giving O(n) time complexity and O(1) space usage.
Is Highest Salaries Difference asked at Google/Amazon/Meta?
Database aggregation questions similar to this frequently appear in SQL interview rounds at companies like Amazon, Meta, and Google. They test understanding of GROUP BY, aggregate functions, and conditional expressions rather than complex algorithms.
What data structure is used in Highest Salaries Difference?
The problem relies on SQL aggregation rather than traditional data structures. The database engine internally maintains grouped aggregates such as MAX() values while scanning rows, similar to maintaining counters per group.
What is the time complexity of Highest Salaries Difference?
The query runs in O(n) time because the database scans the table once to compute the maximum salary values. Aggregation functions such as MAX() track the highest value during the scan, so no additional passes are required.

Ready to solve this problem?

Practice Highest Salaries Difference with our built-in code editor and test cases.

Practice on FleetCode