Skip to main content
Intermediatehow it works Verified ~6 min

Write SQL to list each employee with their manager's name (including people without a manager), then find employees who earn more than their manager.

Source: PostgreSQL: SELECT (clause processing)

Quick answer

Join the employees table to itself with aliases e and m on m.id = e.manager_id; LEFT JOIN keeps top-level employees with a NULL manager. For "earns more than the manager" use an inner self-join with WHERE e.salary > m.salary.

Self-join idea

A self-join joins a table to itself using two aliases, one for the employee and one for the manager. The table has employees(id, name, salary, manager_id) where manager_id points to another row in the same table.

Query 1: every employee with the manager's name

query.sql
SELECT e.name AS employee, m.name AS manager
FROM employees e
LEFT JOIN employees m ON m.id = e.manager_id;

The CEO has manager_id = NULL. An inner join would drop that row; the LEFT JOIN keeps it with manager = NULL. Use COALESCE(m.name, 'No manager') for a friendly label.

Sample data and result (I ran this logic in SQLite):

employeemanager
CEONULL
AshaCEO
RaviCEO
MeenaAsha

Query 2: employees who earn more than their manager

query.sql
SELECT e.name AS employee, e.salary, m.name AS manager, m.salary AS manager_salary
FROM employees e
JOIN employees m ON m.id = e.manager_id
WHERE e.salary > m.salary;

An inner join is right here: someone with no manager has nothing to compare with, and NULL comparisons would be UNKNOWN anyway, so those rows would not be returned.

Common traps

  • Forgetting aliases: both sides are the same table, so every column needs e. or m..
  • Joining on the wrong column: m.id = e.manager_id (the manager's id equals the employee's manager_id), not e.id = m.id.
  • Putting a filter on m in the WHERE clause of a LEFT JOIN, which turns it into an inner join. Put such conditions in the ON clause.

Performance and limits

Index manager_id so the join can look up managers quickly. A self-join shows only one level (direct manager). For the whole chain of command (manager of manager) use a recursive CTE.

Animated explanation
  1. 1employees eeach employee
  2. 2LEFT JOIN employees mON m.id = e.manager_id
  3. 3Top levelmanager columns NULL, still listed
  4. 4Inner join + WHEREe.salary > m.salary
One table, two roles: the alias decides which role a row plays.
See the concept: Join fan-outWhy a join to a one-to-many table multiplies sums
Join fan-out
order_lines: one row per line
O101line 1₹300
payments: one row per attempt
O101P101FAILED
O101P102SUCCESS
lines JOIN payments ON order_id
line 1P101₹300
line 1P102₹300
SUM(line_revenue)₹600

Wrong: the single ₹300 line is counted once per payment attempt.

Fix: aggregate payments to one row per order first

line 1, paid, ₹300, so SUM = ₹300

One order line, two payment attempts: a direct join doubles the revenue. Aggregating payments to order grain first keeps one row per line.

Key points

  • A self-join uses two aliases of the same table
  • LEFT JOIN keeps employees whose manager_id is NULL
  • Join condition is m.id = e.manager_id
  • Use an inner join with WHERE e.salary > m.salary for the comparison
  • Only one level of hierarchy; use a recursive CTE for more

Common mistakes

  • Using an inner join for query 1 and losing the top-level employee
  • Joining on the wrong columns so each employee is its own manager
  • Filtering on the manager table in WHERE after a LEFT JOIN
  • Forgetting to qualify column names with the alias

Prerequisites

  • INNER JOIN and LEFT JOIN
  • Table aliases

Best practices

  • Use descriptive aliases such as emp and mgr
  • Index the manager_id column
  • Use COALESCE to show a label for missing managers

Likely follow-ups

  • How would you find the employees with no one reporting to them?
  • How would you list the full chain of managers for an employee?
  • How would you find the highest-paid employee in each department using a window function?
  • What is the difference between LEFT JOIN and INNER JOIN in a self-join?

References

Discussion