Self JOIN
A self JOIN is a regular join, but the table is joined with itself.
- self join
- table alias
- reports to
- employees
As mentioned in Lesson 5.21 Text: Recap & Looking Ahead:
There are a few more advanced JOINs that we did not cover here, and they are used in very specific use cases. UNION and UNION ALL, CROSS JOIN, and the tricky SELF JOIN. These are more advanced than this course will cover, but it is useful to be aware that they exist, as they are useful in special cases.
A self JOIN is a regular join, but the table is joined with itself.
To construct a self join, you select from the same table twice by using the SELECT statement with an inner join or outer join clause. Because you refer to the same table twice in the same statement, you have to use table aliases. The following illustrates the syntax of a self join:
SELECT
column1, column2,...
FROM
table AS A
(LEFT | INNER) JOIN
table AS B ON join_conditionExample
The following illustrates the data in the employees table including employee id, last name, first name and reports to:
SELECT
employeeid, lastname, firstname, reportsto
FROM
employees;Outputs:
SQL self join with inner join example
SQL self join is very useful when you want to select related data stored in one table such as organizational structure. For example, to display who reports to whom, you can join the employees table to itself as the following query:
SELECT
concat(e.firstname, e.lastname) employee,
concat(m.firstname, m.lastname) manager
FROM
employees e
INNER JOIN
employees m ON m.employeeid = e.reportsto;SQL self join with left join example
The top manager i.e., the CEO, does not report to anyone in the company, therefore, the reportTo column contains the NULL value. To query the whole organization structure including the CEO, you need to use the LEFT JOIN clause rather than the INNER JOIN clause as the following query:
SELECT
concat(e.firstname, e.lastname) employee,
concat(m.firstname, m.lastname) manager
FROM
employees e
LEFT JOIN
employees m ON m.employeeid = e.reportsto
ORDER BY manager;Is that the only purpose?
Joining a table to itself can be useful when you want to compare values in a column to other values in the same column.
To write the query, select from the same table listed twice with different aliases, set up the comparison, and eliminate cases where a particular value would be equal to itself.
Example
Which customers are located in the same state (column name is Region)? Type this statement in the SQL window:
SELECT DISTINCT c1.ContactName, c1.Address, c1.City, c1.Region
FROM Customers AS c1, Customers AS c2
WHERE c1.Region = c2.Region
AND c1.ContactName <> c2.ContactName
ORDER BY c1.Region, c1.ContactName;The result should look like this:
References:
- http://www.zentut.com/sql-tutorial/sql-self-join/
- http://www1.udel.edu/evelyn/SQL-Class3/SQL3_self.html
Further Resources:
- Visual Examples of SQL Joins
- Use cases of Full Outer JOIN
- Cross JOIN
- https://macdiscussions.udacity.com/t/guide-alternative-way-for-joining-tables/51794/1
- Tips to Draw ERD
- Understand Joins
- SQL Self Joins | SQL Tutorial
- [Video] Database Design 50 - Self Join
- What is a Self Join? Explain with Example – Interview Question of the Week #064
- What is SELF JOIN and when would you use it?