Challenge - 5 Problems
Self Join Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of a simple self join query
Consider a table Employees with columns
id, name, and manager_id (which refers to the id of the employee's manager). What is the output of the following SQL query?SELECT e.name AS Employee, m.name AS Manager FROM Employees e JOIN Employees m ON e.manager_id = m.id ORDER BY e.id;
SQL
CREATE TABLE Employees (id INT, name VARCHAR(20), manager_id INT); INSERT INTO Employees VALUES (1, 'Alice', NULL), (2, 'Bob', 1), (3, 'Charlie', 1), (4, 'David', 2);
Attempts:
2 left
💡 Hint
Think about how the join matches employees to their managers using the manager_id and id columns.
✗ Incorrect
The query joins the Employees table to itself to find each employee's manager by matching e.manager_id to m.id. Alice has no manager (NULL), so she is excluded from the join results. Bob and Charlie have Alice as manager, David has Bob.
🧠 Conceptual
intermediate1:30remaining
Understanding self join purpose
Why do we use a self join in SQL?
Attempts:
2 left
💡 Hint
Think about when you want to relate rows inside the same table.
✗ Incorrect
A self join is used to join a table to itself to compare or relate rows within that same table, such as finding managers of employees in the same Employees table.
📝 Syntax
advanced1:30remaining
Identify the syntax error in self join query
Which option contains a syntax error in this self join SQL query?
SELECT e.name, m.name FROM Employees e JOIN Employees m ON e.manager_id = m.id;
Attempts:
2 left
💡 Hint
Check the JOIN clause syntax carefully.
✗ Incorrect
Option B uses WHERE instead of ON for the join condition, which is a syntax error in JOIN syntax.
❓ query_result
advanced2:00remaining
Output of self join with NULL manager_id
Given the Employees table below, what will this query return?
Table data:
SELECT e.name AS Employee, COALESCE(m.name, 'No Manager') AS Manager FROM Employees e LEFT JOIN Employees m ON e.manager_id = m.id ORDER BY e.id;
Table data:
id | name | manager_id 1 | Alice | NULL 2 | Bob | 1 3 | Charlie | 1 4 | David | 2
Attempts:
2 left
💡 Hint
LEFT JOIN keeps all employees even if they have no manager.
✗ Incorrect
LEFT JOIN returns all employees. For Alice, manager_id is NULL, so m.name is NULL and COALESCE replaces it with 'No Manager'. Others show their managers.
❓ optimization
expert2:30remaining
Optimizing a self join query for large tables
You have a large Employees table with millions of rows. You want to find each employee's manager name using a self join. Which option is the best way to optimize this query?
SELECT e.name AS Employee, m.name AS Manager FROM Employees e JOIN Employees m ON e.manager_id = m.id;
Attempts:
2 left
💡 Hint
Indexes help speed up join operations on large tables.
✗ Incorrect
Creating indexes on the join columns (id and manager_id) allows the database to quickly find matching rows, greatly improving performance on large tables.