Day 5/7 - SQL Challenge 🎯
TODAY’S QUESTION:
Find all employees who earn more than their manager.
TABLE:
Employees (id, name, salary, manager_id)
SAMPLE DATA:
id | name | salary | manager_id
—|———|———|————
1 | Alice | 90000 | NULL (CEO)
2 | Bob | 80000 | 1
3 | Charlie | 95000 | 1 ← Earns more than Alice!
4 | Diana | 70000 | 2
THE SOLUTION:
SELECT
e.name as employee_name
FROM Employees e
JOIN Employees m ON e.manager_id =
m.id
WHERE e.salary > m.salary;
BREAKDOWN:
Employees e - Think of this as the “employee table”
Employees m - Think of this as the “manager table”
e.manager_id =
m.id - Connect employee to their manager
e.salary > m.salary - Find where employee earns more
VISUAL:
Employee Table (e) Manager Table (m)
name | manager_id id | name | salary
———|———— —|-——|-——
Bob | 1 → 1 | Alice | 90000
Charlie | 1 → 1 | Alice | 90000 ✓ (95k > 90k)
Diana | 2 → 2 | Bob | 80000
WHY SELF JOINS ARE HARD:
Same table appears twice. You need to:
Alias differently (e vs m)
Think of them as sepa...