0
0
PostgreSQLquery~20 mins

Self join patterns in PostgreSQL - 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
Find employees and their managers using self join

Given the employees table 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;
PostgreSQL
CREATE TABLE employees (id INT PRIMARY KEY, name TEXT, manager_id INT);
INSERT INTO employees VALUES
(1, 'Alice', NULL),
(2, 'Bob', 1),
(3, 'Charlie', 1),
(4, 'Diana', 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": "Alice"}, {"employee": "Bob", "manager": "Bob"}, {"employee": "Charlie", "manager": "Charlie"}, {"employee": "Diana", "manager": "Diana"}]
B[{"employee": "Alice", "manager": null}, {"employee": "Bob", "manager": "Alice"}, {"employee": "Charlie", "manager": "Alice"}, {"employee": "Diana", "manager": "Bob"}]
C[{"employee": "Alice", "manager": "Bob"}, {"employee": "Bob", "manager": "Charlie"}, {"employee": "Charlie", "manager": "Diana"}, {"employee": "Diana", "manager": null}]
D[{"employee": "Alice", "manager": null}, {"employee": "Bob", "manager": null}, {"employee": "Charlie", "manager": null}, {"employee": "Diana", "manager": null}]
Attempts:
2 left
💡 Hint

Think about how the manager_id links to the id of the manager in the same table.

📝 Syntax
intermediate
2:00remaining
Identify the syntax error in self join query

Which option contains a syntax error in this self join query to find pairs of employees who share the same manager?

SELECT e1.name, e2.name
FROM employees e1
JOIN employees e2 ON e1.manager_id = e2.manager_id
WHERE e1.id != e2.id;
ASELECT e1.name, e2.name FROM employees e1 JOIN employees e2 WHERE e1.manager_id = e2.manager_id AND e1.id != e2.id;
BSELECT e1.name, e2.name FROM employees e1 JOIN employees e2 ON e1.manager_id = e2.manager_id AND e1.id != e2.id;
CSELECT e1.name, e2.name FROM employees e1, employees e2 WHERE e1.manager_id = e2.manager_id AND e1.id != e2.id;
DSELECT e1.name, e2.name FROM employees e1 JOIN employees e2 ON e1.manager_id = e2.manager_id WHERE e1.id != e2.id;
Attempts:
2 left
💡 Hint

Check if the JOIN clause has a proper ON condition.

optimization
advanced
2:00remaining
Optimize self join query for hierarchical data

You want to find all employees and their direct managers from a large employees table. Which query is more efficient?

ASELECT e.name, m.name FROM employees e JOIN employees m ON e.manager_id = m.id;
BSELECT e.name, (SELECT name FROM employees WHERE id = e.manager_id) AS manager FROM employees e;
CSELECT e.name, m.name FROM employees e CROSS JOIN employees m WHERE e.manager_id = m.id;
DSELECT e.name, m.name FROM employees e LEFT JOIN employees m ON e.manager_id = m.id;
Attempts:
2 left
💡 Hint

Consider the difference between LEFT JOIN and JOIN and the impact on rows returned.

🧠 Conceptual
advanced
2:00remaining
Understanding recursive self join for hierarchy

Which statement best describes how recursive self joins help in querying hierarchical data?

ARecursive self joins repeatedly join a table to itself to traverse multiple levels of hierarchy until a condition is met.
BRecursive self joins join two different tables repeatedly to combine unrelated data.
CRecursive self joins use a single join to fetch all hierarchy levels at once without iteration.
DRecursive self joins are not used for hierarchical data but for combining unrelated datasets.
Attempts:
2 left
💡 Hint

Think about how you can find all subordinates under a manager at any depth.

🔧 Debug
expert
2:00remaining
Debug incorrect self join query output

Given the employees table with id, name, and manager_id, a query attempts to list employees and their managers but returns duplicate rows for some employees. Which option explains the cause?

SELECT e.name AS employee, m.name AS manager
FROM employees e
JOIN employees m ON e.manager_id = m.id
JOIN employees d ON d.manager_id = m.id
WHERE d.name = 'Diana';
AThe query is missing GROUP BY, which is required to remove duplicates.
BThe WHERE clause filters employees incorrectly, causing duplicates in the output.
CThe extra join to 'd' causes multiple rows per employee if a manager has multiple subordinates, leading to duplicates.
DThe join condition is incorrect; it should use LEFT JOIN instead of JOIN to avoid duplicates.
Attempts:
2 left
💡 Hint

Consider how joining the same table multiple times can multiply rows.