Sobes.tech
Junior — Middle

Explain the differences between SQL join types: LEFT JOIN, RIGHT JOIN, and INNER JOIN.

sobes.tech AI

Answer from AI

INNER JOIN returns only the rows that match the join condition in both tables.

LEFT JOIN returns all rows from the left table and the matching rows from the right table. If there are no matches in the right table, the right table's fields will be filled with NULL.

RIGHT JOIN — conversely, returns all rows from the right table and the matching rows from the left, with NULLs where there are no matches.

Example:

SELECT * FROM A
INNER JOIN B ON A.id = B.a_id; -- only matches

SELECT * FROM A
LEFT JOIN B ON A.id = B.a_id; -- all from A + matches from B

SELECT * FROM A
RIGHT JOIN B ON A.id = B.a_id; -- all from B + matches from A
Explain the differences between SQL join types: LEFT… - sobes.tech