
SQL practice
SQL self join practice questions
8 problems, ordered from easiest. They run in your browser, and 1 is free.
Start with Employees earning more than their managerThe idea in a minute
A self join joins a table to itself. You give the table two aliases and treat them as two copies, for example one copy for the employee and one for the manager.
Use it when a row points at another row in the same table, or when you want to compare rows with each other.
Example: Employee and manager names
CREATE TABLE staff AS SELECT * FROM (VALUES (1,'Priya',NULL),(2,'Karan',1),(3,'Isha',1),(4,'Dev',2)) t(id, name, manager_id);
SELECT e.name AS employee, m.name AS manager
FROM staff e
LEFT JOIN staff m ON e.manager_id = m.id
ORDER BY e.idWhat it prints
employee manager
Priya NULL
Karan Priya
Isha Priya
Dev KaranWhat to watch for
- Always give the two copies different aliases, or SQL cannot tell the columns apart.
- Use a LEFT JOIN if people without a match, such as the top manager, must still appear.
- To compare pairs, use a condition like a.id < b.id so each pair appears once.
Self Joins problems
- Employees earning more than their managerbeginner · Free
- Employee and manager namesintermediate · Locked
- Products most often bought togetherintermediate · Locked
- Near-salary pairs in a departmentadvanced · Locked
- Overlapping meetings in a roomadvanced · Locked
- Slow request-to-accept tripsadvanced · Locked
- Pages liked by people you followadvanced · Locked
- Strong mutual friendsadvanced · Locked
Want a guided order across every topic? Open the practice sheets or the SQL hub.