Solve “Employees earning more than their manager”

Free. Runs in your browser, no install.

SQL · Basics & Joins

What is a self-join?

Easyself-joinaliaseshierarchy

Interview question

What is a self-join?

How to answer

The short answer

A self-join means joining a table to itself.

Example: employees and their managers are stored in the same table.

Output
employees
---------
id
name
manager_id

Query:

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

We use aliases because the same table appears twice.

Common use cases

  • Employee → manager relationships
  • Hierarchies
  • Finding related records
  • Comparing rows within the same table

Lock it in with a real drill

You remember an answer longer once you have used it. Try a hands-on problem on this concept.

Open related drill