Junior — Middle
What is the difference between INNER and LEFT JOIN types when working with tables in SQL?
sobes.tech AI
Answer from AI
INNER JOIN returns only those rows that have matches in both tables based on the join condition. If there is no corresponding record in one of the tables, such a row will not be included in the result.
LEFT JOIN (or LEFT OUTER JOIN) returns all rows from the left table and the matching rows from the right table. If there are no matches for a row from the left table in the right table, the result will include the values from the left table and NULL for the right table.
Example:
SELECT a.id, a.name, b.order_date
FROM customers a
INNER JOIN orders b ON a.id = b.customer_id;
-- returns only customers with orders
SELECT a.id, a.name, b.order_date
FROM customers a
LEFT JOIN orders b ON a.id = b.customer_id;
-- returns all customers, including those without orders (order_date will be NULL)