Sobes.tech
Junior — Middle

Explain the principle of the LEFT JOIN operator in SQL and how it combines tables.

sobes.tech AI

Answer from AI

The LEFT JOIN operator in SQL is used to combine two tables such that the result contains all rows from the left (first) table and the matching rows from the right (second) table. If there are no matches for a row from the left table in the right table, the columns from the right table will be NULL.

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 display all customers from the Customers table, as well as their order dates from the Orders table. If a customer has no orders, the order_date field will be NULL.

Thus, LEFT JOIN is useful when you want to keep all data from the main table and add information from a related table, even if there is no related information.