Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is a self join in SQL?
A self join is when a table is joined with itself to compare rows within the same table.
Click to reveal answer
beginner
Why do we use table aliases in a self join?
Table aliases give different names to the same table so we can refer to it twice in the query clearly.
Click to reveal answer
intermediate
Example: How to find employees who have the same manager using self join?
Use a self join on the employee table matching employee.manager_id with manager.employee_id using aliases.
Click to reveal answer
intermediate
Can a self join be used with INNER JOIN and LEFT JOIN?
Yes, self joins can use INNER JOIN, LEFT JOIN, or other join types depending on the data needed.
Click to reveal answer
beginner
What is the main difference between a self join and a regular join?
A self join joins a table to itself, while a regular join combines two different tables.
Click to reveal answer
What does a self join do?
AJoins a table to itself
BJoins two different tables
CJoins two databases
DJoins two columns in the same table
✗ Incorrect
A self join is when a table is joined with itself to compare rows within the same table.
Why are aliases important in a self join?
ATo rename columns
BTo speed up the query
CTo create new tables
DTo refer to the same table twice clearly
✗ Incorrect
Aliases let us give different names to the same table so we can use it twice in the query.
Which join type can be used in a self join?
AAny join type like INNER JOIN or LEFT JOIN
BOnly LEFT JOIN
COnly INNER JOIN
DNo join type is needed
✗ Incorrect
Self joins can use any join type depending on the data needed.
In a self join, what does the ON clause usually compare?
ATwo different tables' columns
BTwo unrelated columns
CTwo columns from the same table using aliases
DTwo databases
✗ Incorrect
The ON clause compares columns from the same table but using different aliases.
What is a common use case for a self join?
ACombining sales data from two tables
BFinding employees with the same manager
CJoining customer and order tables
DCreating a new table
✗ Incorrect
Self joins are often used to find relationships within the same table, like employees sharing a manager.
Explain what a self join is and why we use table aliases in it.
Think about how you can compare rows in the same table.
You got /3 concepts.
Describe a real-life example where a self join would be useful.
Consider relationships within one group of people.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of a self join in SQL?
easy
A. To combine rows from two tables without any condition
B. To join two different tables based on a common column
C. To join a table to itself to compare rows within the same table
D. To delete duplicate rows from a table
Solution
Step 1: Understand the concept of self join
A self join is used when you want to compare rows within the same table by treating it as two separate tables using aliases.
Step 2: Identify the correct purpose
Joining a table to itself allows you to find relationships or comparisons between rows in the same table, such as hierarchical data or pairs.
Final Answer:
To join a table to itself to compare rows within the same table -> Option C
Quick Check:
Self join = join table to itself [OK]
Hint: Self join means joining table to itself using aliases [OK]
Common Mistakes:
Confusing self join with joining two different tables
Thinking self join deletes duplicates
Assuming self join combines rows without condition
2. Which of the following is the correct syntax to perform a self join on a table named employees with alias e1 and e2?
easy
A. SELECT * FROM employees e1 JOIN employees e2 ON e1.id = e2.manager_id;
B. SELECT * FROM employees JOIN employees ON id = manager_id;
C. SELECT * FROM employees e1, employees e2 ON e1.id = e2.manager_id;
D. SELECT * FROM employees e1 INNER JOIN employees e2 ON e1.id = e2.id;
Solution
Step 1: Use table aliases for self join
To join a table to itself, you must use aliases like e1 and e2 to distinguish the two instances.
Step 2: Write the join condition correctly
The join condition should relate columns from the two aliases, for example e1.id = e2.manager_id to find employees and their managers.
Final Answer:
SELECT * FROM employees e1 JOIN employees e2 ON e1.id = e2.manager_id; -> Option A
Quick Check:
Self join syntax = table alias + join condition [OK]
Hint: Always use aliases to distinguish the same table twice [OK]
Common Mistakes:
Not using aliases causes syntax errors
Joining on wrong columns returns wrong results
Using comma join without aliases is confusing
3. Given the table employees with columns id, name, and manager_id, what will this query return?
SELECT e1.name AS Employee, e2.name AS Manager FROM employees e1 LEFT JOIN employees e2 ON e1.manager_id = e2.id;
medium
A. Syntax error due to missing WHERE clause
B. List of employees with their managers' names, NULL if no manager
C. List of employees without managers only
D. List of managers with their employees' names
Solution
Step 1: Understand the LEFT JOIN on self join
The query joins the employees table to itself using aliases e1 and e2, matching e1.manager_id to e2.id to find each employee's manager.
Step 2: Interpret the SELECT columns and join type
Using LEFT JOIN means all employees (e1) appear, even if they have no manager (e2.name will be NULL). The SELECT shows employee and manager names.
Final Answer:
List of employees with their managers' names, NULL if no manager -> Option B
Quick Check:
LEFT JOIN self join shows all employees with managers [OK]
Hint: LEFT JOIN keeps all employees, shows NULL for missing managers [OK]
Common Mistakes:
Confusing employee and manager columns
Thinking it lists only managers or only employees without managers
Assuming syntax error without WHERE clause
4. Identify the error in this self join query:
SELECT e1.name, e2.name FROM employees e1 JOIN employees e2 ON e1.id = e2.id;
medium
A. Missing table aliases for employees
B. Using JOIN instead of LEFT JOIN causes error
C. Syntax error due to missing WHERE clause
D. The join condition compares the same column, causing incorrect results
Solution
Step 1: Analyze the join condition
The query joins employees to itself on e1.id = e2.id, which matches each row to itself only, not to related rows.
Step 2: Understand the effect of the condition
This join condition does not find relationships like manager or pairs; it just duplicates rows, which is likely incorrect for self join use.
Final Answer:
The join condition compares the same column, causing incorrect results -> Option D
Quick Check:
Self join needs meaningful join condition, not same column equals [OK]
Hint: Join condition must relate different columns for meaningful self join [OK]
Common Mistakes:
Joining on identical columns returns only same rows
Forgetting to use aliases
Assuming JOIN requires WHERE clause
5. You have a table employees with columns id, name, and manager_id. Write a query using self join to find all employees who share the same manager. Which query correctly achieves this?
hard
A. SELECT e1.name, e2.name FROM employees e1 JOIN employees e2 ON e1.manager_id = e2.manager_id WHERE e1.id <> e2.id;
B. SELECT e1.name, e2.name FROM employees e1 JOIN employees e2 ON e1.id = e2.manager_id;
C. SELECT e1.name, e2.name FROM employees e1 JOIN employees e2 ON e1.manager_id = e2.id WHERE e1.id = e2.id;
D. SELECT e1.name, e2.name FROM employees e1 JOIN employees e2 ON e1.manager_id = e2.manager_id WHERE e1.id = e2.id;
Solution
Step 1: Understand the goal
We want pairs of employees who share the same manager, so their manager_id values must be equal but employees must be different.
Step 2: Write the self join condition
Joining on e1.manager_id = e2.manager_id finds employees with the same manager. Adding WHERE e1.id <> e2.id excludes pairing an employee with themselves.
Final Answer:
SELECT e1.name, e2.name FROM employees e1 JOIN employees e2 ON e1.manager_id = e2.manager_id WHERE e1.id <> e2.id; -> Option A
Quick Check:
Same manager and different employees = SELECT e1.name, e2.name FROM employees e1 JOIN employees e2 ON e1.manager_id = e2.manager_id WHERE e1.id <> e2.id; [OK]
Hint: Join on manager_id and exclude same employee IDs [OK]