LEFT JOIN helps you combine rows from two tables. It keeps all rows from the first table and adds matching rows from the second table. If no match, it shows NULL for the second table.
You want to list all customers and their orders, even if some customers have no orders.
You need to find all employees and their departments, including those not assigned to any department yet.
You want to show all products and their sales, even if some products have never been sold.
You want to compare two lists and see which items in the first list have no match in the second.
Syntax
MySQL
SELECT columns
FROM table1
LEFTJOIN table2 ON table1.common_column = table2.common_column;
LEFT JOIN keeps all rows from the first (left) table.
If there is no matching row in the second (right) table, columns from the right table will be NULL.
Examples
List all customers and their order IDs. Customers without orders will show NULL for order ID.
MySQL
SELECT customers.name, orders.id
FROM customers
LEFTJOIN orders ON customers.id = orders.customer_id;
Show all employees and their department names. Employees without a department will have NULL for department name.
MySQL
SELECT employees.name, departments.name
FROM employees
LEFTJOIN departments ON employees.dept_id = departments.id;
Display all products and their sales amounts. Products with no sales show NULL for amount.
MySQL
SELECT products.name, sales.amount
FROM products
LEFTJOIN sales ON products.id = sales.product_id;
Sample Program
This example creates two tables: customers and orders. It inserts some data, then uses LEFT JOIN to list all customers with their ordered products. Customers without orders show NULL.
MySQL
CREATETABLE customers (id INT, name VARCHAR(20));
CREATETABLE orders (id INT, customer_id INT, product VARCHAR(20));
INSERTINTO customers VALUES (1, 'Alice'), (2, 'Bob'), (3, 'Charlie');
INSERTINTO orders VALUES (101, 1, 'Book'), (102, 1, 'Pen'), (103, 3, 'Notebook');
SELECT customers.name, orders.product
FROM customers
LEFTJOIN orders ON customers.id = orders.customer_id
ORDERBY customers.id, orders.id;
OutputSuccess
Important Notes
LEFT JOIN is useful to keep all data from the first table, even if the second table has no matching rows.
NULL values in the result mean no matching row was found in the right table.
Summary
LEFT JOIN combines two tables, keeping all rows from the first table.
Matching rows from the second table are added; if none, NULLs appear.
Great for showing all items from one list with optional related data from another.
Practice
(1/5)
1. What does a LEFT JOIN do in SQL?
easy
A. Deletes rows from the left table that do not have matches in the right table.
B. Returns only rows that have matching values in both tables.
C. Returns all rows from the right table and matching rows from the left table.
D. Returns all rows from the left table and matching rows from the right table, NULL if no match.
Solution
Step 1: Understand LEFT JOIN behavior
A LEFT JOIN keeps all rows from the left table regardless of matches in the right table.
Step 2: Check what happens with unmatched rows
If no matching row exists in the right table, the result shows NULL for right table columns.
Final Answer:
Returns all rows from the left table and matching rows from the right table, NULL if no match. -> Option D
Quick Check:
LEFT JOIN = all left rows + matched right rows [OK]
Hint: LEFT JOIN keeps all left rows, fills right with NULL if no match [OK]
Common Mistakes:
Confusing LEFT JOIN with INNER JOIN
Thinking it returns all right table rows
Assuming unmatched rows are dropped
2. Which of the following is the correct syntax for a LEFT JOIN in MySQL?
easy
A. SELECT * FROM table1 LEFT JOIN table2 ON table1.id = table2.id;
B. SELECT * FROM table1 JOIN LEFT table2 ON table1.id = table2.id;
C. SELECT * FROM table1 LEFT OUTER JOIN table2 WHERE table1.id = table2.id;
D. SELECT * FROM table1 LEFT JOIN table2 WHERE table1.id = table2.id;
Solution
Step 1: Review standard LEFT JOIN syntax
The correct syntax uses LEFT JOIN followed by ON condition to specify matching columns.
Step 2: Check each option
SELECT * FROM table1 LEFT JOIN table2 ON table1.id = table2.id; uses correct syntax: LEFT JOIN with ON clause. SELECT * FROM table1 JOIN LEFT table2 ON table1.id = table2.id; has JOIN LEFT which is invalid. SELECT * FROM table1 LEFT OUTER JOIN table2 WHERE table1.id = table2.id; uses WHERE instead of ON, which is incorrect for JOIN condition. SELECT * FROM table1 LEFT JOIN table2 WHERE table1.id = table2.id; uses WHERE instead of ON, invalid syntax.
Final Answer:
SELECT * FROM table1 LEFT JOIN table2 ON table1.id = table2.id; -> Option A
Quick Check:
LEFT JOIN syntax = LEFT JOIN ... ON ... [OK]
Hint: Use LEFT JOIN ... ON ... for correct syntax [OK]
Common Mistakes:
Using JOIN LEFT instead of LEFT JOIN
Using WHERE instead of ON for join condition
Putting join condition directly in WHERE after table
A. [('Alice', NULL), ('Bob', NULL), ('Carol', NULL)]
B. [('Alice', 'HR'), ('Bob', 'IT'), ('Carol', NULL)]
C. [('Alice', 'HR'), ('Bob', NULL), ('Carol', 'IT')]
D. [('Alice', 'HR'), ('Carol', 'IT')]
Solution
Step 1: Match Employees with Departments using LEFT JOIN
LEFT JOIN keeps all Employees. For each employee, it tries to find a matching department where e.id = d.emp_id.
Step 2: Check matches and NULLs
'Alice' (id=1) matches department 'HR' (emp_id=1). 'Bob' (id=2) has no matching department, so dept_name is NULL. 'Carol' (id=3) matches 'IT' (emp_id=3).
Final Answer:
[('Alice', 'HR'), ('Bob', NULL), ('Carol', 'IT')] -> Option C
Quick Check:
LEFT JOIN keeps all left rows, unmatched right columns NULL [OK]
Hint: LEFT JOIN shows all left rows; unmatched right columns are NULL [OK]
Common Mistakes:
Assuming unmatched rows are dropped
Mixing up emp_id and id columns
Ignoring ORDER BY effect
4. Identify the error in this LEFT JOIN query:
SELECT a.id, b.value FROM tableA a LEFT JOIN tableB b ON a.id = b.id WHERE b.value > 10;
medium
A. The WHERE clause filters out rows where b.value is NULL, defeating LEFT JOIN purpose.
B. The ON clause is missing a join condition.
C. LEFT JOIN should be replaced with INNER JOIN for this query.
D. The SELECT statement is missing table aliases.
Solution
Step 1: Understand effect of WHERE on LEFT JOIN
LEFT JOIN keeps all rows from tableA, but WHERE b.value > 10 excludes rows where b.value is NULL (no match), effectively turning it into INNER JOIN.
Step 2: Identify the error
The error is using WHERE to filter on right table columns after LEFT JOIN, which removes unmatched rows.
Final Answer:
The WHERE clause filters out rows where b.value is NULL, defeating LEFT JOIN purpose. -> Option A
Quick Check:
WHERE on right table column after LEFT JOIN removes unmatched rows [OK]
Hint: Use ON for filtering right table, not WHERE, to keep unmatched rows [OK]