LEFT JOIN helps you combine two tables and keep all rows from the first table, even if there is no match in the second table.
LEFT JOIN execution behavior in SQL
Start learning this pattern below
Jump into concepts and practice - no test required
SELECT columns FROM table1 LEFT JOIN table2 ON table1.common_column = table2.common_column;
The LEFT JOIN returns all rows from table1.
If there is no matching row in table2, the result shows NULL for table2 columns.
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;
SELECT products.name, sales.amount FROM products LEFT JOIN sales ON products.id = sales.product_id;
This query lists all authors and their books. Authors without books still appear with NULL for book title.
CREATE TABLE authors (id INT, name VARCHAR(20)); CREATE TABLE books (id INT, title VARCHAR(30), author_id INT); INSERT INTO authors VALUES (1, 'Alice'), (2, 'Bob'), (3, 'Carol'); INSERT INTO books VALUES (1, 'Book A', 1), (2, 'Book B', 1), (3, 'Book C', 2); SELECT authors.name, books.title FROM authors LEFT JOIN books ON authors.id = books.author_id ORDER BY authors.id, books.id;
LEFT JOIN keeps all rows from the left table, even if no match is found on the right.
Columns from the right table show NULL when there is no matching row.
Use LEFT JOIN when you want to keep all data from the first table and add matching info from the second.
LEFT JOIN returns all rows from the first (left) table.
Rows without matches in the second (right) table show NULL for right table columns.
It is useful to keep all data from one table while adding related data from another.
Practice
LEFT JOIN do in SQL?Solution
Step 1: Understand LEFT JOIN behavior
A LEFT JOIN returns all rows from the left table regardless of matches in the right table.Step 2: Check what happens when no match exists
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 BQuick Check:
LEFT JOIN = all left rows + matched right rows [OK]
- Confusing LEFT JOIN with INNER JOIN
- Thinking LEFT JOIN returns only matching rows
- Assuming NULLs never appear in results
Solution
Step 1: Recall correct LEFT JOIN syntax
The correct syntax uses LEFT JOIN followed by ON clause to specify join condition.Step 2: Check each option
SELECT * FROM table1 LEFT JOIN table2 ON table1.id = table2.id; uses correct syntax: LEFT JOIN with ON condition. SELECT * FROM table1 JOIN LEFT table2 ON table1.id = table2.id; has incorrect order. SELECT * FROM table1 LEFT JOIN table2 WHERE table1.id = table2.id; uses WHERE instead of ON. SELECT * FROM table1 LEFT JOIN table2 USING id; uses USING without parentheses, which is invalid syntax.Final Answer:
SELECT * FROM table1 LEFT JOIN table2 ON table1.id = table2.id; -> Option CQuick Check:
LEFT JOIN ... ON condition is standard syntax [OK]
- Using WHERE instead of ON for join condition
- Swapping JOIN and LEFT keywords
- Confusing USING with ON without proper column names
Employees(id, name)Departments(id, dept_name, manager_id)What will this query return?
SELECT e.name, d.dept_name FROM Employees e LEFT JOIN Departments d ON e.id = d.manager_id;
Solution
Step 1: Analyze LEFT JOIN condition
The query LEFT JOINs Employees (left) with Departments (right) on employee id matching department manager_id.Step 2: Understand output rows
All employees appear. If an employee is a manager (id matches manager_id), department name shows; else department columns are NULL.Final Answer:
All employees with their department names if they are managers, NULL otherwise -> Option AQuick Check:
LEFT JOIN keeps all employees, adds department if manager [OK]
- Thinking only managers appear in result
- Confusing which table is left or right
- Expecting departments without managers to appear
SELECT a.id, b.value FROM A a LEFT JOIN B b ON a.id = b.id WHERE b.value > 10;
Solution
Step 1: Understand LEFT JOIN with WHERE filter
The WHERE clause filters rows after join. Filtering on b.value > 10 excludes rows where b.value is NULL.Step 2: Effect on LEFT JOIN
This filtering removes rows without matches in B, making LEFT JOIN behave like INNER JOIN.Final Answer:
The WHERE clause filters out rows where b.value is NULL, negating LEFT JOIN effect -> Option DQuick Check:
Filtering on right table in WHERE breaks LEFT JOIN [OK]
- Filtering right table columns in WHERE after LEFT JOIN
- Confusing ON and WHERE clauses
- Assuming LEFT JOIN always keeps all left rows regardless of WHERE
Customers(id, name)
Orders(id, customer_id, order_date)
Solution
Step 1: Understand requirement for all customers
We want all customers listed, even those without orders, so LEFT JOIN is needed.Step 2: Aggregate last order date per customer
Using MAX(o.order_date) with GROUP BY c.name gives last order date or NULL if no orders.Step 3: Check options
SELECT c.name, MAX(o.order_date) FROM Customers c LEFT JOIN Orders o ON c.id = o.customer_id GROUP BY c.name; uses LEFT JOIN and GROUP BY correctly. SELECT c.name, o.order_date FROM Customers c LEFT JOIN Orders o ON c.id = o.customer_id WHERE o.order_date = (SELECT MAX(order_date) FROM Orders); filters in WHERE, excluding customers without orders. Options A and C use INNER JOIN, excluding customers without orders.Final Answer:
SELECT c.name, MAX(o.order_date) FROM Customers c LEFT JOIN Orders o ON c.id = o.customer_id GROUP BY c.name; -> Option AQuick Check:
LEFT JOIN + GROUP BY + MAX gets last order date including customers without orders [OK]
- Using INNER JOIN excludes customers without orders
- Filtering right table in WHERE removes unmatched rows
- Not grouping when using aggregate functions
