Highest Salaries Difference - Problem
Given a Salaries table with employee names, departments, and salaries, find the absolute difference between the highest salary in the marketing department and the highest salary in the engineering department.
The table contains:
emp_name(varchar): Employee namedepartment(varchar): Department namesalary(int): Employee salary
Return a single value representing the absolute difference between these two maximum salaries.
Table Schema
Salaries
| Column Name | Type | Description |
|---|---|---|
emp_name
PK
|
varchar | Employee name |
department
PK
|
varchar | Department name (marketing, engineering, etc.) |
salary
|
int | Employee salary |
Primary Key: (emp_name, department)
Note: Each row represents an employee's salary in a specific department. At least one employee exists in both marketing and engineering departments.
Input & Output
Example 1 — Basic Department Comparison
Input Table:
| emp_name | department | salary |
|---|---|---|
| Alice | marketing | 80000 |
| Bob | marketing | 90000 |
| Charlie | engineering | 85000 |
| David | engineering | 92000 |
Output:
| salary_difference |
|---|
| 2000 |
💡 Note:
Marketing department max salary is 90000 (Bob), engineering department max salary is 92000 (David). The absolute difference is |90000 - 92000| = 2000.
Example 2 — Marketing Higher
Input Table:
| emp_name | department | salary |
|---|---|---|
| John | marketing | 95000 |
| Sarah | marketing | 88000 |
| Mike | engineering | 87000 |
| Lisa | engineering | 79000 |
Output:
| salary_difference |
|---|
| 8000 |
💡 Note:
Marketing department max salary is 95000 (John), engineering department max salary is 87000 (Mike). The absolute difference is |95000 - 87000| = 8000.
Constraints
-
1 ≤ emp_name.length ≤ 20 -
1 ≤ department.length ≤ 20 -
1000 ≤ salary ≤ 200000 - At least one employee exists in both marketing and engineering departments
Visualization
Tap to expand
Understanding the Visualization
1
Input
Employee salaries by department
2
MAX by Dept
Find highest salary in each department
3
ABS Difference
Calculate absolute difference
Key Takeaway
🎯 Key Insight: Use MAX() with WHERE clauses to find department maximums, then ABS() for absolute difference
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code