Sobes.tech
Junior — Middle

Explain the differences between LEFT JOIN, RIGHT JOIN, and INNER JOIN operations in SQL.

sobes.tech AI

Answer from AI

In SQL, JOIN operations are used to combine rows from two tables based on a related column.

  • INNER JOIN returns only those rows where there are matches in both tables.

  • LEFT JOIN (or LEFT OUTER JOIN) returns all rows from the left table and the matching rows from the right table. If there is no match in the right table, the right table's fields will be NULL.

  • RIGHT JOIN (or RIGHT OUTER JOIN) returns all rows from the right table and the matching rows from the left table. If there is no match in the left table, the left table's fields will be NULL.

Example:

SELECT A.id, B.value
FROM A
LEFT JOIN B ON A.id = B.a_id;

This query will return all records from table A, and only the matching records from B. If there is no match for a record from A in B, the fields from B will be NULL.

Thus:

  • INNER JOIN — intersection of tables based on the condition.
  • LEFT JOIN — all from the left + matches from the right.
  • RIGHT JOIN — all from the right + matches from the left.
Explain the differences between LEFT JOIN, RIGHT… - sobes.tech