Highest Salaries Difference - Problem

You're working as a data analyst for a tech company and need to investigate salary disparities between departments. You have access to a database containing employee information including their names, departments, and salaries.

Given a table called Salaries with the following structure:

Column NameType
emp_namevarchar
departmentvarchar
salaryint

Your task is to calculate the absolute difference between the highest salary in the marketing department and the highest salary in the engineering department.

Note: The combination of (emp_name, department) serves as the primary key, and you're guaranteed that both marketing and engineering departments have at least one employee.

Input & Output

example_1.sql โ€” Basic Case
$ Input: Salaries table: +----------+-------------+--------+ | emp_name | department | salary | +----------+-------------+--------+ | Alice | marketing | 95000 | | Bob | engineering | 110000 | | Carol | marketing | 88000 | | Dave | engineering | 105000 | +----------+-------------+--------+
โ€บ Output: +-------------------+ | salary_difference | +-------------------+ | 15000 | +-------------------+
๐Ÿ’ก Note: Marketing highest salary: max(95000, 88000) = 95000. Engineering highest salary: max(110000, 105000) = 110000. Absolute difference: |95000 - 110000| = 15000.
example_2.sql โ€” Marketing Higher
$ Input: Salaries table: +----------+-------------+--------+ | emp_name | department | salary | +----------+-------------+--------+ | John | marketing | 125000 | | Jane | engineering | 95000 | | Mike | marketing | 85000 | | Sarah | engineering | 100000 | +----------+-------------+--------+
โ€บ Output: +-------------------+ | salary_difference | +-------------------+ | 25000 | +-------------------+
๐Ÿ’ก Note: Marketing highest salary: max(125000, 85000) = 125000. Engineering highest salary: max(95000, 100000) = 100000. Absolute difference: |125000 - 100000| = 25000.
example_3.sql โ€” Equal Salaries
$ Input: Salaries table: +----------+-------------+--------+ | emp_name | department | salary | +----------+-------------+--------+ | Tom | marketing | 120000 | | Lisa | engineering | 120000 | +----------+-------------+--------+
โ€บ Output: +-------------------+ | salary_difference | +-------------------+ | 0 | +-------------------+
๐Ÿ’ก Note: Both departments have the same highest salary (120000), so the absolute difference is |120000 - 120000| = 0.

Constraints

  • 1 โ‰ค Number of employees โ‰ค 100
  • Department names are exactly 'marketing' or 'engineering'
  • 103 โ‰ค salary โ‰ค 106
  • At least one employee exists in both marketing and engineering departments
  • Employee names are unique within each department

Visualization

Tap to expand
Department Salary Championship AnalysisMARKETINGTEAMENGINEERINGTEAMAlice: $95kCarol: $88kBob: $110kDave: $105kChampion BattleAlice: $95kVSBob: $110kDifference$15k
Understanding the Visualization
1
Scan Employee Records
Go through each employee record in the company database
2
Track Department Champions
For each department, keep track of who has the highest salary seen so far
3
Update Champions
When we find a higher salary in either department, update our champion
4
Calculate Final Difference
Once all records are processed, calculate the absolute difference between champions
Key Takeaway
๐ŸŽฏ Key Insight: Using conditional aggregation allows us to find both department champions in a single database scan, making it the most efficient solution with O(n) time complexity.
Asked in
Google 45 Meta 38 Amazon 32 Microsoft 28 Apple 22
52.3K Views
High Frequency
~12 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen