What is the difference between inner join, left join, right join, self join and full join?
Published at:
Category:other
In SQL (Structured Query Language), joins are used to combine rows from two or more tables based on a related column between them. Here's an explanation of the different types of joins you mentioned:
1. Inner Join
- Definition: An
INNER JOIN
returns only the rows that have matching values in both tables. - Behavior: It combines rows from two tables where there is a match between specified columns (usually a key column like a primary or foreign key).
- Result: Only records that exist in both tables will be returned.
- Example:
SELECT * FROM table1 INNER JOIN table2 ON table1.id = table2.id;
2. Left Join (or Left Outer Join)
- Definition: A
LEFT JOIN
returns all the rows from the left table (the first table in the query) and the matched rows from the right table. If there is no match, the result isNULL
on the side of the right table. - Behavior: It ensures all rows from the left table are included, even if there is no corresponding row in the right table.
- Result: All records from the left table and matching records from the right table; unmatched records from the right table will show as
NULL
. - Example:
SELECT * FROM table1 LEFT JOIN table2 ON table1.id = table2.id;
3. Right Join (or Right Outer Join)
- Definition: A
RIGHT JOIN
is similar to aLEFT JOIN
, but it returns all the rows from the right table and the matched rows from the left table. If there is no match, the result isNULL
on the side of the left table. - Behavior: It ensures all rows from the right table are included, even if there is no corresponding row in the left table.
- Result: All records from the right table and matching records from the left table; unmatched records from the left table will show as
NULL
. - Example:
SELECT * FROM table1 RIGHT JOIN table2 ON table1.id = table2.id;
4. Self Join
- Definition: A
SELF JOIN
is a regular join, but the table is joined with itself. This is useful when each row in the table has a relationship with other rows within the same table. - Behavior: You use aliases to distinguish between the two instances of the same table being joined.
- Result: Rows from the same table are combined based on some condition.
- Example:
In this example, theSELECT a.column_name, b.column_name FROM employees a JOIN employees b ON a.supervisor_id = b.employee_id;
employees
table is joined with itself to find the supervisor-employee relationships.
5. Full Join (or Full Outer Join)
- Definition: A
FULL JOIN
(orFULL OUTER JOIN
) returns all the rows when there is a match in either the left table or the right table. If there is no match, the result isNULL
on the side where there is no match. - Behavior: It combines all rows from both tables, filling in
NULL
for missing matches. - Result: All records from both tables, with
NULL
values for non-matching rows. - Example:
Note: Not all databases supportSELECT * FROM table1 FULL OUTER JOIN table2 ON table1.id = table2.id;
FULL OUTER JOIN
. For example, MySQL does not natively support it, but workarounds can be implemented usingUNION
.
Summary of Differences:
- INNER JOIN: Only matching rows from both tables.
- LEFT JOIN: All rows from the left table and matching rows from the right table.
- RIGHT JOIN: All rows from the right table and matching rows from the left table.
- FULL JOIN: All rows from both tables, with NULLs where there are no matches.
- SELF JOIN: Joins a table with itself.
- CROSS JOIN: Cartesian product of both tables.
Each type of join serves a different purpose depending on the data you need to retrieve.