Introduction
LEFT JOIN helps you combine two tables and keep all rows from the first (left) table, even if there is no matching data in the second (right) table.
Jump into concepts and practice - no test required
LEFT JOIN helps you combine two tables and keep all rows from the first (left) table, even if there is no matching data in the second (right) table.
SELECT columns FROM left_table LEFT JOIN right_table ON left_table.key = right_table.key;
SELECT customers.name, orders.id FROM customers LEFT JOIN orders ON customers.id = orders.customer_id;
SELECT employees.name, departments.name FROM employees LEFT JOIN departments ON employees.dept_id = departments.id;
This example creates two tables: customers and orders. It inserts some data, then uses LEFT JOIN to show all customers with their orders. Customers without orders show NULL for order_id.
CREATE TABLE customers (id INT, name VARCHAR(20)); CREATE TABLE orders (id INT, customer_id INT); INSERT INTO customers VALUES (1, 'Alice'), (2, 'Bob'), (3, 'Carol'); INSERT INTO orders VALUES (101, 1), (102, 1), (103, 3); SELECT customers.name, orders.id AS order_id FROM customers LEFT JOIN orders ON customers.id = orders.customer_id ORDER BY customers.id, orders.id;
If no matching row exists in the right table, the result shows NULL for those columns.
LEFT JOIN is useful to find missing or unmatched data while keeping the main list intact.
LEFT JOIN keeps all rows from the left table.
It adds matching rows from the right table or NULL if no match.
Use it to combine data but never lose rows from the main table.
LEFT JOIN do in SQL?EmployeesSalesSELECT Employees.name, Sales.amount FROM Employees LEFT JOIN Sales ON Employees.id = Sales.emp_id;SELECT a.id, b.value FROM A LEFT JOIN B ON a.id = b.a_id WHERE b.value > 10;WHERE b.value > 10 OR b.value IS NULL to preserve unmatched rows.ProductsSales