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.
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.
We can first calculate the highest salary for each department, and then calculate the difference between the two highest salaries.
MySQL
| Approach | Time | Space | When to Use |
|---|---|---|---|
| GROUP BY with MAX aggregation | O(n) | O(1) | Best general solution when you need the highest salary per department |
| Conditional aggregation with CASE + MAX | O(n) | O(1) | When you want both department values and their difference returned in a single row |
| Subquery for each department | O(n) | O(1) | Readable but slightly more verbose; useful for beginners learning SQL aggregation |
Leetcode 2853 - Highest Salaries Difference - Solved by Everyday Data Science | ABSOLUTE difference • Everyday Data Science • 592 views views
Watch 3 more video solutions →Practice Highest Salaries Difference with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor