Introduction
LEFT JOIN helps you combine two tables and keep all rows from the first table, even if there is no matching data in the second table. This shows missing matches as NULL.
Jump into concepts and practice - no test required
LEFT JOIN helps you combine two tables and keep all rows from the first table, even if there is no matching data in the second table. This shows missing matches as NULL.
SELECT columns FROM table1 LEFT JOIN table2 ON table1.common_column = table2.common_column;
The LEFT JOIN keeps all rows from table1.
If there is no match in table2, columns from table2 will be NULL.
SELECT customers.name, orders.id FROM customers LEFT JOIN orders ON customers.id = orders.customer_id;
SELECT employees.name, projects.name FROM employees LEFT JOIN projects ON employees.project_id = projects.id;
This query lists all customers and their order IDs. Customers without orders show NULL in 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;
LEFT JOIN is useful to find unmatched rows by checking for NULLs in the joined table's columns.
Ordering results helps see NULL rows clearly at the right place.
LEFT JOIN keeps all rows from the first table.
Rows without matches in the second table show NULL values.
This helps find missing or unmatched data easily.
LEFT JOIN do in SQL?Employees and Departments with data:Employees:Departments:SELECT e.name, d.dept_name FROM Employees e LEFT JOIN Departments d ON e.dept_id = d.dept_id;
SELECT a.id, b.value FROM A a LEFT JOIN B b ON a.id = b.a_id WHERE b.value > 10;
Orders:Customers: