Tier 2 · Core SQL

Self-Joins in SQL

By Owen Middleton · Updated September 2026 · Examples run on PostgreSQL 17

What are Self-Joins in SQL?

A self-join joins a table to itself, using two separate aliases to treat it as two distinct copies.

You're looking at an org chart. The employees table has a manager_id column that points to another row in the same table — each employee's row stores the ID of their manager, who is also an employee. To get an employee's name alongside their manager's name, both pieces of data live in the same table. You can't pull from two different tables, because there's only one table. You pull from the same table twice, with different aliases.

How do you write a self-join in SQL?

The syntax looks exactly like a regular join. The only difference is that FROM and JOIN reference the same table name, and you must give each copy a distinct alias:

SELECT e.name AS employee_name, m.name AS manager_name
FROM employees e
JOIN employees m ON e.manager_id = m.id

e is the employee copy. m is the manager copy. SQL scans e for each employee row, then looks up the matching row in m where m.id equals that employee's manager_id. The result: employee name next to manager name on the same row.

Why does a self-join drop the top of an org chart?

Because this is an INNER JOIN, employees with no manager_id — typically executives at the top of the hierarchy — are excluded. They have no matching row in the manager copy. To include them, use LEFT JOIN:

SELECT e.name AS employee_name, m.name AS manager_name
FROM employees e
LEFT JOIN employees m ON e.manager_id = m.id

Employees with no manager get NULL in manager_name. Executives included.

Self-joins work with any table that has rows relating to other rows in the same table. Categories with a parent_id, products that reference a base product, org charts, threaded comments:

SELECT e.name AS employee_name, m.name AS manager_name
FROM employees e
JOIN employees m ON e.manager_id = m.id
WHERE e.department_id = 1

The same filtering and ordering rules apply as any other join.

Why does a self-join need two table aliases?

The one thing that trips people up: forgetting that each copy of the table needs a distinct alias.

If you write FROM employees JOIN employees ON ... without aliases, SQL raises an error because it can't tell which copy you're referencing in the SELECT list or ON clause. Pick two short, meaningful aliases — e and m for employee and manager, c and p for child and parent — and use them consistently throughout the query.

Practice Self-Joins in SQL

Practice · easy hr · Helix Systems

Helix Systems' HR team is building an employee directory and needs each person listed alongside their direct manager.

Write a query to return the employee name and manager name for every employee who has a manager on record.

Assumptions:

  • The employees table contains every active and former employee at Helix Systems.
  • The manager_id column points to another row in employees — the employee's direct manager.
  • Top-of-hierarchy executives have a missing manager_id and will not appear in the result.

Output:

  • One row per employee with a manager, with columns employee_name and manager_name.
Schema · hr4 tables? = nullable
departments
idinteger
nametext
locationtext
budgetnumeric
salaries
idinteger
employee_id?integer
amountnumeric
effective_datedate
end_date?date
employees
idinteger
nametext
emailtext
department_id?integer
manager_id?integer
hire_datedate
titletext
is_activeboolean
job_history
idinteger
employee_id?integer
titletext
department_id?integer
start_datedate
end_date?date

Run previews · Check grades

Write a query, then run it to see results here.

Worked solution

The full breakdown walks through the shape, each clause, why this approach beats the alternatives, and the trap to avoid.

See the full worked solution

9 Self-Joins practice problems

Start learning to practice all 9 Self-Joins problems, with instant grading and mastery tracking.

Common questions about Self-Joins

What happens if you forget the aliases in a self-join?

The query stops, saying the table name was specified more than once. Both halves of the join are the same table, so without two distinct aliases there is no way to say which copy a column belongs to, and PostgreSQL refuses rather than guessing.

Can you join a table to itself more than once?

Yes. Each copy needs its own alias, and then a third can be chained onto the second, which is how you reach an employee, their manager and their manager in turn. Every extra copy narrows the result to rows where the whole chain exists.

Does a self-join have to be an INNER JOIN?

No, and often it should not be. An inner self-join silently drops the row at the top of the hierarchy, because nobody is above it to match. Switch to a LEFT JOIN when the root belongs in the answer.

How you actually get good at SQL

Reading explains SQL. Writing it, over and over with instant feedback, is what makes you fluent.

That's the whole SQLMaxx loop: 600+ real problems, instant AI feedback, mastery you can actually see, and spaced review that won't let you forget.

A stack of SQL practice problem cards, the top card showing an employees table.
615 problems · 66 concepts

Real problems. Not toy examples.

615 hand-built problems spanning all 66 concepts, from basic SELECTs to window functions, built on real schemas and real business questions, the kind you'll actually get asked on the job. Enough reps to make SQL automatic.

A retro computer showing a SQL query marked correct with a green checkmark.
Instant AI feedback

Write a query. Know if it's right in one second.

No copying an answer and hoping it clicked. The AI grader checks your real query against real data, catches exactly what's wrong, and explains the fix in plain English, like a senior analyst reading over your shoulder on every problem.

A circular mastery progress dial filling from blue to green, the SQLMaxx diamond at its center.
Mastery tracking

Stop guessing whether you actually know it.

SQLMaxx tracks every concept and shows you what you've mastered and what's still shaky. Your skills fill in one concept at a time, so 'I think I get joins' becomes something you can prove.

A SQL query editor circled by a blue return arrow with a clock, scheduled to come back for review.
Spaced review

Learn it once. Keep it for good.

Most of what you learn this week fades by next week. So when a concept comes due for review, SQLMaxx hands you a fresh problem to solve from a blank editor, not a flashcard to re-read. A research-backed spaced-repetition algorithm (FSRS) times each return for right before you'd forget, so your SQL is still there months later, when the interview or the job actually needs it.