Junior
What is the difference between Left Join, Right Join, and Inner Join?
sobes.tech AI
Answer from AI
- INNER JOIN: Returns rows where there are matches in both tables.
- LEFT JOIN: Returns all rows from the "left" table and matching rows from the "right" table. If there are no matches in the "right" table, NULLs are returned for columns from the "right" table.
- RIGHT JOIN: Returns all rows from the "right" table and matching rows from the "left" table. If there are no matches in the "left" table, NULLs are returned for columns from the "left" table.
Visualization:
| Join Type | Included rows |
|---|---|
| INNER JOIN | Only matching rows from both tables. |
| LEFT JOIN | All rows from the left table + matching rows from the right (if any). |
| RIGHT JOIN | All rows from the right table + matching rows from the left (if any). |
SQL example:
-- INNER JOIN
SELECT *
FROM TableA
INNER JOIN TableB ON TableA.ID = TableB.ID;
-- LEFT JOIN
SELECT *
FROM TableA
LEFT JOIN TableB ON TableA.ID = TableB.ID;
-- RIGHT JOIN
SELECT *
FROM TableA
RIGHT JOIN TableB ON TableA.ID = TableB.ID;