SQL SELF JOIN Get Certified
Learn how to join a table to itself so you can compare related rows, build hierarchies, and find matching pairs inside a single table.
What is a SELF JOIN?
A SELF JOIN is simply a join where both sides of the join are the same table. SQL treats the two sides as separate tables by giving them different aliases. This technique is essential whenever rows inside one table are related to other rows in the same table.
Typical situations include employee–manager relationships, folder–parent hierarchies, products that share the same price, or people who live in the same city.
Basic SELF JOIN – Employees and Managers
The classic SELF JOIN uses a foreign-key style column that points back into the same table.
What this code does: Anika is the top manager (manager_id is NULL). Rohan and Meera report to Anika. Kabir reports to Rohan. The LEFT JOIN keeps Anika in the result even though she has no manager. The query returns 4 rows.
WITH employees(employee_id, employee_name, manager_id) AS (
VALUES
(1, 'Anika', NULL),
(2, 'Rohan', 1),
(3, 'Meera', 1),
(4, 'Kabir', 2)
)
SELECT
e.employee_name AS employee,
m.employee_name AS manager
FROM employees AS e
LEFT JOIN employees AS m
ON e.manager_id = m.employee_id
ORDER BY e.employee_id;
Another Hierarchy – Staff and Supervisors
The same pattern works for any hierarchical data. Here we use more realistic column names.
What this code does: Isha is the top supervisor. Dev and Tanya report to Isha. Arjun reports to Dev and Naina reports to Tanya. The query returns 5 rows, with Isha showing a NULL supervisor.
WITH staff(staff_id, staff_name, supervisor_id) AS (
VALUES
(101, 'Isha', NULL),
(102, 'Dev', 101),
(103, 'Tanya', 101),
(104, 'Arjun', 102),
(105, 'Naina', 103)
)
SELECT
s.staff_name AS staff_member,
p.staff_name AS supervisor
FROM staff AS s
LEFT JOIN staff AS p
ON s.supervisor_id = p.staff_id
ORDER BY s.staff_id;
SELF JOIN with INNER JOIN (Buddies)
When every row must have a related partner, use INNER JOIN instead of LEFT JOIN.
What this code does: Aarav and Mira are mutual buddies, and Vihaan and Diya are mutual buddies. Kunal has no buddy and is therefore excluded by the INNER JOIN. The query returns 4 rows.
WITH team_members(member_id, member_name, buddy_id) AS (
VALUES
(1, 'Aarav', 2),
(2, 'Mira', 1),
(3, 'Vihaan', 4),
(4, 'Diya', 3),
(5, 'Kunal', NULL)
)
SELECT
a.member_name AS member,
b.member_name AS buddy
FROM team_members AS a
INNER JOIN team_members AS b
ON a.buddy_id = b.member_id
ORDER BY a.member_id;
Finding Products with the Same Price
SELF JOIN is also used to compare different rows that share a common value. The condition p1.product_id < p2.product_id prevents a product from matching itself and removes duplicate pairs.
What this code does: Desk Lamp and Water Bottle both cost 800; Backpack and Travel Pouch both cost 1200. The query returns the two unique pairs.
WITH products(product_id, product_name, price) AS (
VALUES
(1, 'Backpack', 1200),
(2, 'Desk Lamp', 800),
(3, 'Water Bottle', 800),
(4, 'Notebook Set', 500),
(5, 'Travel Pouch', 1200)
)
SELECT
p1.product_name AS product_one,
p2.product_name AS product_two,
p1.price
FROM products AS p1
INNER JOIN products AS p2
ON p1.price = p2.price
AND p1.product_id < p2.product_id
ORDER BY p1.price, p1.product_id;
Products in the Same Category
The same pair-comparison technique works for any grouping column.
What this code does: Alpha Phone and Beta Phone share the Mobile category; Gamma Tablet and Delta Tablet share the Tablet category. Echo Camera has no partner. The query returns 2 rows.
WITH catalog(product_id, product_name, category) AS (
VALUES
(1, 'Alpha Phone', 'Mobile'),
(2, 'Beta Phone', 'Mobile'),
(3, 'Gamma Tablet', 'Tablet'),
(4, 'Delta Tablet', 'Tablet'),
(5, 'Echo Camera', 'Camera')
)
SELECT
p1.product_name AS product_one,
p2.product_name AS product_two,
p1.category
FROM catalog AS p1
INNER JOIN catalog AS p2
ON p1.category = p2.category
AND p1.product_id < p2.product_id
ORDER BY p1.category, p1.product_id;
People from the Same City
Another frequent use is finding pairs that share a location or any other attribute.
What this code does: Riya and Simran both live in Delhi; Karan and Neha both live in Mumbai. Aman (Pune) has no partner. The query returns 2 rows.
WITH travelers(traveler_id, traveler_name, city) AS (
VALUES
(1, 'Riya', 'Delhi'),
(2, 'Karan', 'Mumbai'),
(3, 'Simran', 'Delhi'),
(4, 'Aman', 'Pune'),
(5, 'Neha', 'Mumbai')
)
SELECT
t1.traveler_name AS traveler_one,
t2.traveler_name AS traveler_two,
t1.city
FROM travelers AS t1
INNER JOIN travelers AS t2
ON t1.city = t2.city
AND t1.traveler_id < t2.traveler_id
ORDER BY t1.city, t1.traveler_id;
Salary Comparison
You can also compare numeric values across rows of the same table.
What this code does: Every pair of workers where the first has a lower salary than the second is returned. Because of the worker_id < worker_id condition each unordered pair appears only once. The query returns several ordered pairs.
WITH workers(worker_id, worker_name, salary) AS (
VALUES
(1, 'Nikhil', 45000),
(2, 'Pooja', 62000),
(3, 'Rahul', 52000),
(4, 'Sneha', 70000)
)
SELECT
w1.worker_name AS lower_paid,
w1.salary AS lower_salary,
w2.worker_name AS higher_paid,
w2.salary AS higher_salary
FROM workers AS w1
INNER JOIN workers AS w2
ON w1.salary < w2.salary
AND w1.worker_id < w2.worker_id
ORDER BY w1.worker_id, w2.salary;
Courses in the Same Department
SELF JOIN can list every distinct pair of items that belong to the same group.
What this code does: Algebra and Calculus share the Mathematics department; Physics and Chemistry share the Science department. History has no partner. The query returns 2 rows.
WITH courses(course_id, course_name, department) AS (
VALUES
(1, 'Algebra', 'Mathematics'),
(2, 'Calculus', 'Mathematics'),
(3, 'Physics', 'Science'),
(4, 'Chemistry', 'Science'),
(5, 'History', 'Arts')
)
SELECT
c1.course_name AS course_one,
c2.course_name AS course_two,
c1.department
FROM courses AS c1
INNER JOIN courses AS c2
ON c1.department = c2.department
AND c1.course_id < c2.course_id
ORDER BY c1.department, c1.course_id;
Hierarchical Data – Folders and Parents
SELF JOIN is the natural way to navigate tree-structured data stored in a single table.
What this code does: Documents is the root. Projects and Photos are children of Documents. SQL Files is a child of Projects and Images is a child of Photos. The query returns every folder together with its parent (NULL for the root).
WITH folders(folder_id, folder_name, parent_id) AS (
VALUES
(1, 'Documents', NULL),
(2, 'Projects', 1),
(3, 'Photos', 1),
(4, 'SQL Files', 2),
(5, 'Images', 3)
)
SELECT
child.folder_name AS child_folder,
parent.folder_name AS parent_folder
FROM folders AS child
LEFT JOIN folders AS parent
ON child.parent_id = parent.folder_id
ORDER BY child.folder_id;
SELF JOIN with Multiple Conditions
You can combine several conditions when you need more precise matching.
What this code does: Finds pairs of items that belong to the same category and have the same stock status. Desk Chair and Office Table both belong to Furniture and are Available. The query returns that one matching pair.
WITH inventory_items(item_id, item_name, category, stock_status) AS (
VALUES
(1, 'Desk Chair', 'Furniture', 'Available'),
(2, 'Office Table', 'Furniture', 'Available'),
(3, 'Floor Lamp', 'Lighting', 'Available'),
(4, 'Wall Lamp', 'Lighting', 'Out of Stock'),
(5, 'Book Shelf', 'Furniture', 'Out of Stock')
)
SELECT
a.item_name AS item_one,
b.item_name AS item_two,
a.category,
a.stock_status
FROM inventory_items AS a
INNER JOIN inventory_items AS b
ON a.category = b.category
AND a.stock_status = b.stock_status
AND a.item_id < b.item_id
ORDER BY a.category, a.item_id;
Common Uses of SELF JOIN
Advantages of SELF JOIN
- Lets you compare rows that live inside the same table.
- Natural solution for employee–manager and other hierarchical relationships.
- Useful for finding related or duplicate pairs (same price, same city, same category).
- Works with both LEFT JOIN (keep unmatched rows) and INNER JOIN (only matched pairs).
- No extra tables are required – everything stays in one place.
Best Practices
- Always give the two instances of the table clear, different aliases (e.g.
e/morchild/parent). - When producing unordered pairs, add a condition such as
id < idto avoid self-matches and duplicate pairs. - Use LEFT JOIN when you also want rows that have no related partner (top-level managers, root folders, etc.).
- Use INNER JOIN when every row must have a matching partner.
- Make sure the join condition correctly expresses the intended relationship between the rows.
A SELF JOIN treats one table as two separate tables so you can compare its own rows. It is the standard technique for hierarchical data, pair-wise comparisons, and finding related records stored in a single table. Click Test My Code on any example to run it live, or Code Explanation to walk through the query block by block.
🧪 Test Your SQL Code
Edit the SQL code on the left and click "Run Code" to see the result on the right.
Click "Run Code" to see the result here.