Junior
What is the difference between LEFT JOIN and RIGHT JOIN?
sobes.tech AI
Answer from AI
LEFT JOIN selects all rows from the left table and the matching rows from the right table. If there is no match in the right table, the columns from the right table will have NULL values.
RIGHT JOIN selects all rows from the right table and the matching rows from the left table. If there is no match in the left table, the columns from the left table will have NULL values.
In fact, RIGHT JOIN is a mirror reflection of LEFT JOIN. The result of RIGHT JOIN (table A JOIN table B) is identical to the result of LEFT JOIN (table B JOIN table A) with columns rearranged.
SQL example:
-- LEFT JOIN
SELECT *
FROM TableA
LEFT JOIN TableB ON TableA.id = TableB.id;
-- RIGHT JOIN
SELECT *
FROM TableA
RIGHT JOIN TableB ON TableA.id = TableTableB.id;
Visualization:
| Type of JOIN | Rows included from the left table | Rows included from the right table |
|---|---|---|
| LEFT JOIN | All | Matching (or NULL) |
| RIGHT JOIN | Matching (or NULL) | All |