SQL LEARNING PLATFORM
Joining a table to itself — useful for hierarchical relationships.
Learning Blocks
Interactive Queries
Concepts you'll master
SELF JOIN is used to join a table with itself
Useful for hierarchical data (like employees and managers)
We use aliases to differentiate the same table
Each alias acts like a separate table
Common in real-world scenarios like org charts and reporting structure
🌟 Think of it this way: Imagine an employees table where each employee has a manager_id. To find who manages whom, we join the employees table with itself — one side is the employee, the other is the manager.
Shows each employee along with their manager name.
SELECT e.name AS employee, m.name AS manager FROM employees e LEFT JOIN employees m ON e.manager_id = m.emp_id;
A table joined to itself. Our employees table has manager_id referencing emp_id in the same table:
SELECT e.name AS employee, m.name AS manager FROM employees e LEFT JOIN employees m ON e.manager_id = m.emp_id ORDER BY e.name;
✅ Engineering Insight: INNER JOIN vs LEFT JOIN is a decision, not a preference. Ask yourself: 'Is it valid for a row to have no match?' If yes, use LEFT JOIN. If a row must have a corresponding entry in the other table (by business rules), use INNER JOIN — it will surface data integrity issues.
Practice your SQL skills