Sobes.tech
Junior — Middle

Can you explain how LEFT JOIN works in SQL and how it differs from other joins?

sobes.tech AI

Answer from AI

LEFT JOIN in SQL is used to combine two tables in such a way that the result contains all rows from the left table and the matching rows from the right table. If there are no matching records in the right table, the result set will have NULL values for those columns.

Differences from other types of JOIN:

  • INNER JOIN returns only the rows where there are matches in both tables.
  • RIGHT JOIN returns all rows from the right table and the matching rows from the left.
  • FULL JOIN returns all rows from both tables, combining matches and filling NULL where there are no matches.

Example:

SELECT a.id, a.name, b.order_date
FROM customers a
LEFT JOIN orders b ON a.id = b.customer_id;

This query will return all customers with their orders if they exist, or NULL in the order_date column if there are no orders.