Find Employees Who Earn More Than Their Department Average
SQL coding challenge · Difficulty: medium · +100 XP
Problem
A compensation analyst wants to find employees who earn above their own department's average — high performers who are strong candidates for promotion.
Tables
Table: departments
| department_id | department_name | | --- | --- | | 1 | Engineering | | 2 | Marketing | | 3 | HR | | 4 | Finance |
Table: employees
| employee_id | first_name | department_id | salary | hire_date | manager_id | | --- | --- | --- | --- | --- | --- | | 1 | Alice | 1 | 90000 | 2021-03-10 | NULL | | 2 | Bob | 1 | 75000 | 2021-03-20 | 1 | | 3 | Charlie | 1 | 80000 | 2021-03-25 | 1 | | 4 | Victor | 1 | 65000 | 2022-01-15 | 1 | | 5 | Diana | 2 | 70000 | 2022-06-05 | 1 | | 6 | Eve | 2 | 65000 | 2021-11-15 | 5 | | 7 | Frank | 3 | 60000 | 2023-02-28 | 1 |
Expected Output
| employee_id | first_name | salary | department_id | | --- | --- | --- | --- | | 1 | Alice | 90000 | 1 | | 3 | Charlie | 80000 | 1 | | 5 | Diana | 70000 | 2 |
- Return:
employee_id,first_name,salary,department_id - Only employees earning above their own department's average
- Sort by
salarydescending - Hint: Use a correlated subquery:
WHERE salary > (SELECT AVG(salary) FROM employees e2 WHERE e2.department_id = e1.department_id)