0
0
MySQLquery~20 mins

Self JOIN in MySQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Self JOIN Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of a Self JOIN Query
Given the table Employees with columns id, name, and manager_id (which references id of the manager), what is the output of the following query?

SELECT e.name AS Employee, m.name AS Manager
FROM Employees e
LEFT JOIN Employees m ON e.manager_id = m.id
ORDER BY e.id;
MySQL
CREATE TABLE Employees (id INT, name VARCHAR(50), manager_id INT);
INSERT INTO Employees VALUES
(1, 'Alice', NULL),
(2, 'Bob', 1),
(3, 'Charlie', 1),
(4, 'David', 2);

SELECT e.name AS Employee, m.name AS Manager
FROM Employees e
LEFT JOIN Employees m ON e.manager_id = m.id
ORDER BY e.id;
A[{"Employee": "Alice", "Manager": null}, {"Employee": "Bob", "Manager": null}, {"Employee": "Charlie", "Manager": null}, {"Employee": "David", "Manager": null}]
B[{"Employee": "Alice", "Manager": "Alice"}, {"Employee": "Bob", "Manager": "Bob"}, {"Employee": "Charlie", "Manager": "Charlie"}, {"Employee": "David", "Manager": "David"}]
C[{"Employee": "Alice", "Manager": "Bob"}, {"Employee": "Bob", "Manager": "Charlie"}, {"Employee": "Charlie", "Manager": "David"}, {"Employee": "David", "Manager": null}]
D[{"Employee": "Alice", "Manager": null}, {"Employee": "Bob", "Manager": "Alice"}, {"Employee": "Charlie", "Manager": "Alice"}, {"Employee": "David", "Manager": "Bob"}]
Attempts:
2 left
💡 Hint
Think about how the manager_id links to the id in the same table.
📝 Syntax
intermediate
1:30remaining
Identify the Syntax Error in Self JOIN
Which option contains a syntax error in this self JOIN query?

SELECT e.name, m.name
FROM Employees e
JOIN Employees m ON e.manager_id = m.id;
ASELECT e.name, m.name FROM Employees e JOIN Employees m ON e.manager_id = m.id;
BSELECT e.name, m.name FROM Employees e JOIN Employees m ON e.manager_id = m.id ORDER BY;
CSELECT e.name, m.name FROM Employees e JOIN Employees m ON e.manager_id = m.id WHERE e.id = m.id;
DSELECT e.name, m.name FROM Employees e JOIN Employees m ON e.manager_id = m.id LIMIT 5;
Attempts:
2 left
💡 Hint
Check the ORDER BY clause syntax carefully.
optimization
advanced
2:30remaining
Optimizing a Self JOIN Query for Performance
You have a large Employees table with millions of rows. Which option will improve the performance of this self JOIN query the most?

SELECT e.name, m.name
FROM Employees e
JOIN Employees m ON e.manager_id = m.id
WHERE e.department = 'Sales';
AAdd an index on Employees(manager_id) and Employees(department).
BAdd an index only on Employees(name).
CAdd a foreign key constraint on Employees(manager_id) referencing Employees(id).
DRewrite the query using UNION instead of JOIN.
Attempts:
2 left
💡 Hint
Think about which columns are used in JOIN and WHERE clauses.
🔧 Debug
advanced
2:00remaining
Debugging Unexpected NULLs in Self JOIN Result
A query returns NULL for all manager names even though managers exist. Which option explains the most likely cause?

SELECT e.name, m.name AS Manager
FROM Employees e
LEFT JOIN Employees m ON e.manager_id = m.id;
AThe JOIN should be INNER JOIN instead of LEFT JOIN.
BThe query is missing a GROUP BY clause.
CThe <code>manager_id</code> values in Employees do not match any <code>id</code> values due to data inconsistency.
DThe table alias <code>m</code> is not allowed in self JOIN.
Attempts:
2 left
💡 Hint
Check if the join condition matches any rows.
🧠 Conceptual
expert
3:00remaining
Understanding Recursive Relationships with Self JOIN
Consider an Employees table where each employee can have a manager, and managers can also have managers, forming a hierarchy. Which SQL approach best retrieves the full management chain for a given employee in MySQL 8+?

AUse a recursive Common Table Expression (CTE) with a self JOIN to traverse the hierarchy.
BUse multiple nested JOINs manually for each level of management.
CUse a simple SELECT with GROUP BY on manager_id.
DUse a UNION ALL without recursion to combine employee and manager rows.
Attempts:
2 left
💡 Hint
Think about how to handle an unknown number of management levels.