0
0
SQLquery~10 mins

Self join concept in SQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Self join concept
Start with one table
Create two aliases of the same table
Join the table aliases on a related column
Filter or select matching rows
Return combined rows showing relationships within the same table
A self join uses one table twice by giving it two names (aliases) and joining it to itself to find related rows.
Execution Sample
SQL
SELECT e1.name AS Employee, e2.name AS Manager
FROM employees e1
JOIN employees e2 ON e1.manager_id = e2.id;
This query finds each employee and their manager by joining the employees table to itself.
Execution Table
StepActione1.namee1.manager_ide2.ide2.nameJoin Condition (e1.manager_id = e2.id)Output Row
1Start with first employee rowAlice22Bob2 = 2 (True)Alice, Bob
2Next employee rowCharlie32Bob3 = 2 (False)
3Check next manager rowCharlie33David3 = 3 (True)Charlie, David
4Next employee rowEve22Bob2 = 2 (True)Eve, Bob
5Next employee rowFrank42Bob4 = 2 (False)
6Check next manager rowFrank43David4 = 3 (False)
7Check next manager rowFrank44Grace4 = 4 (True)Frank, Grace
8No more rowsEnd of join
💡 All employee rows checked against all manager rows; join condition false or true determines output.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6After 7Final
e1.nameAliceCharlieCharlieEveFrankFrankFrankFrank
e1.manager_id23324444
e2.id22322344
e2.nameBobBobDavidBobBobDavidGraceGrace
Key Moments - 3 Insights
Why do we need to use aliases like e1 and e2 in a self join?
Because we use the same table twice in the query, aliases help SQL know which copy we mean. See execution_table rows where e1 and e2 columns differ.
How does the join condition work when joining a table to itself?
The join condition compares columns from the two aliases. For example, e1.manager_id = e2.id matches employees to their managers, as shown in the join condition column in execution_table.
What happens if the join condition is false for some rows?
Those rows do not appear in the output. For example, in execution_table rows 2, 5, and 6 the condition is false, so no output row is produced.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output row at step 3?
ACharlie, David
BAlice, Bob
CEve, Bob
DFrank, Grace
💡 Hint
Check the Output Row column in execution_table at step 3.
At which step does the join condition first become false?
AStep 5
BStep 2
CStep 4
DStep 7
💡 Hint
Look at the Join Condition column in execution_table for the first 'False' value.
If we changed the join condition to e1.id = e2.manager_id, what would happen?
AIt would find employees with no managers.
BIt would cause a syntax error.
CIt would find managers and their employees instead.
DIt would return the same results.
💡 Hint
Think about reversing the join condition and how it changes the relationship direction.
Concept Snapshot
Self join uses one table twice by giving it two aliases.
Join the aliases on related columns within the same table.
Useful to find relationships like employee-manager in one table.
Syntax: FROM table AS t1 JOIN table AS t2 ON t1.col = t2.col
Output combines rows from the same table based on the join condition.
Full Transcript
A self join means joining a table to itself. We do this by giving the table two different names called aliases. Then we join these aliases on columns that relate rows inside the same table. For example, to find each employee's manager, we join the employees table to itself where the employee's manager_id matches the manager's id. The execution table shows step-by-step how each employee row is matched with manager rows. When the join condition is true, the pair appears in the output. When false, it does not. Aliases are important to tell SQL which copy of the table we mean. Changing the join condition changes the relationship direction we find. This concept helps find relationships inside one table using SQL.