0
0
SQLquery~20 mins

Self join for hierarchical data in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Self Join Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Find all employees and their managers

Given a table Employees with columns EmployeeID, Name, and ManagerID (which references EmployeeID), 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.ManagerID = m.EmployeeID
ORDER BY e.EmployeeID;
SQL
SELECT e.Name AS Employee, m.Name AS Manager
FROM Employees e
LEFT JOIN Employees m ON e.ManagerID = m.EmployeeID
ORDER BY e.EmployeeID;
A[{"Employee": "Alice", "Manager": null}, {"Employee": "Bob", "Manager": "Alice"}, {"Employee": "Charlie", "Manager": "Bob"}]
B[{"Employee": "Alice", "Manager": "Alice"}, {"Employee": "Bob", "Manager": "Bob"}, {"Employee": "Charlie", "Manager": "Charlie"}]
C[{"Employee": "Alice", "Manager": "Bob"}, {"Employee": "Bob", "Manager": "Charlie"}, {"Employee": "Charlie", "Manager": null}]
D[{"Employee": "Alice", "Manager": null}, {"Employee": "Bob", "Manager": null}, {"Employee": "Charlie", "Manager": null}]
Attempts:
2 left
💡 Hint

Think about how the ManagerID links to EmployeeID in the same table.

📝 Syntax
intermediate
1:30remaining
Identify the syntax error in self join query

Which option contains a syntax error in this self join query to find employees and their managers?

SELECT e.Name, m.Name
FROM Employees e
JOIN Employees m ON e.ManagerID = m.EmployeeID;
ASELECT e.Name, m.Name FROM Employees e JOIN Employees m ON e.ManagerID = m.EmployeeID;
BSELECT e.Name, m.Name FROM Employees e JOIN Employees m ON e.ManagerID = m.EmployeeID WHERE;
CSELECT e.Name, m.Name FROM Employees e JOIN Employees m ON e.ManagerID = m.EmployeeID ORDER BY e.Name;
DSELECT e.Name, m.Name FROM Employees e JOIN Employees m ON e.ManagerID = m.EmployeeID LIMIT 5;
Attempts:
2 left
💡 Hint

Look for incomplete clauses or misplaced keywords.

🧠 Conceptual
advanced
1:00remaining
Understanding recursive hierarchy with self join

In a self join used to represent hierarchical data, what is the main purpose of joining the table to itself?

ATo combine rows from two different tables into one result set.
BTo create a backup of the table before updating data.
CTo compare rows within the same table to find relationships like manager and employee.
DTo filter rows based on a condition in a single table.
Attempts:
2 left
💡 Hint

Think about how hierarchical relationships are stored in one table.

query_result
advanced
2:00remaining
Output of recursive self join for two-level hierarchy

Given the table Categories with columns CategoryID, Name, and ParentID, what is the output of this query?

SELECT c.Name AS Child, p.Name AS Parent
FROM Categories c
LEFT JOIN Categories p ON c.ParentID = p.CategoryID
WHERE p.Name IS NOT NULL
ORDER BY c.Name;
SQL
SELECT c.Name AS Child, p.Name AS Parent
FROM Categories c
LEFT JOIN Categories p ON c.ParentID = p.CategoryID
WHERE p.Name IS NOT NULL
ORDER BY c.Name;
A[{"Child": "Computers", "Parent": null}, {"Child": "Electronics", "Parent": null}]
B[{"Child": "Computers", "Parent": "Laptops"}, {"Child": "Electronics", "Parent": "Smartphones"}]
C[{"Child": "Laptops", "Parent": null}, {"Child": "Smartphones", "Parent": null}]
D[{"Child": "Laptops", "Parent": "Computers"}, {"Child": "Smartphones", "Parent": "Electronics"}]
Attempts:
2 left
💡 Hint

Remember that ParentID links to CategoryID of the parent category.

🔧 Debug
expert
2:30remaining
Why does this self join query return no rows?

Consider this query on Employees table:

SELECT e.Name, m.Name
FROM Employees e
INNER JOIN Employees m ON e.ManagerID = m.EmployeeID
WHERE e.ManagerID IS NULL;

Why does it return no rows?

ABecause <code>e.ManagerID IS NULL</code> filters out all rows that have a manager, but the join requires matching managers, so no rows match.
BBecause the join condition is incorrect; it should be <code>e.EmployeeID = m.ManagerID</code>.
CBecause the table has no employees with <code>ManagerID</code> set to <code>null</code>.
DBecause <code>INNER JOIN</code> always returns all rows regardless of the <code>WHERE</code> clause.
Attempts:
2 left
💡 Hint

Think about how INNER JOIN and WHERE conditions interact.