Outer joins help us find all related data, even when some parts are missing. They show us everything from one table and matching data from another, including unmatched rows.
Why outer joins are needed in SQL
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
SQL
SELECT columns FROM table1 LEFT OUTER JOIN table2 ON table1.key = table2.key;
LEFT OUTER JOIN returns all rows from the left table and matched rows from the right table.
If there is no match, columns from the right table will be NULL.
Examples
SQL
SELECT customers.name, orders.id FROM customers LEFT OUTER JOIN orders ON customers.id = orders.customer_id;
SQL
SELECT employees.name, departments.name FROM employees RIGHT OUTER JOIN departments ON employees.department_id = departments.id;
SQL
SELECT products.name, sales.amount FROM products FULL OUTER JOIN sales ON products.id = sales.product_id;
Sample Program
This query lists all customers and their orders. Customers without orders show NULL for order_id.
SQL
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 OUTER JOIN orders ON customers.id = orders.customer_id ORDER BY customers.id, orders.id;
Important Notes
Outer joins help avoid losing data when one table has missing matches.
LEFT, RIGHT, and FULL outer joins differ by which table's rows are all kept.
Summary
Outer joins show all rows from one or both tables, even if no match exists.
They are useful to find missing or unmatched data.
LEFT OUTER JOIN is the most common, showing all rows from the left table.
Practice
1. Why do we need
LEFT OUTER JOIN in SQL?easy
Solution
Step 1: Understand the purpose of LEFT OUTER JOIN
LEFT OUTER JOIN returns all rows from the left table and matched rows from the right table. If no match, NULLs appear for right table columns.Step 2: Compare with INNER JOIN behavior
INNER JOIN returns only rows with matching keys in both tables, excluding unmatched rows.Final Answer:
To include all rows from the left table even if there is no matching row in the right table -> Option AQuick Check:
LEFT OUTER JOIN shows all left rows [OK]
Hint: LEFT OUTER JOIN keeps all left rows, unmatched get NULLs [OK]
Common Mistakes:
- Confusing LEFT OUTER JOIN with INNER JOIN
- Thinking it deletes or updates rows
- Assuming it only shows matched rows
2. Which of the following is the correct syntax for a LEFT OUTER JOIN?
easy
Solution
Step 1: Recall correct LEFT OUTER JOIN syntax
The correct syntax is: SELECT ... FROM table1 LEFT JOIN table2 ON condition; LEFT JOIN is shorthand for LEFT OUTER JOIN.Step 2: Identify syntax errors in other options
SELECT * FROM table1 JOIN LEFT OUTER table2 ON table1.id = table2.id; has JOIN LEFT OUTER which is invalid order. SELECT * FROM table1 OUTER LEFT JOIN table2 ON table1.id = table2.id; uses OUTER LEFT JOIN which is incorrect. SELECT * FROM table1 LEFT OUTER JOIN table2 WHERE table1.id = table2.id; uses WHERE instead of ON for join condition.Final Answer:
SELECT * FROM table1 LEFT JOIN table2 ON table1.id = table2.id; -> Option BQuick Check:
LEFT JOIN ... ON ... is correct syntax [OK]
Hint: Use LEFT JOIN ... ON ... for correct outer join syntax [OK]
Common Mistakes:
- Swapping JOIN and LEFT keywords
- Using WHERE instead of ON for join condition
- Writing OUTER LEFT JOIN instead of LEFT OUTER JOIN
3. Given tables
Employees and Departments where some employees have no department, what will this query return?SELECT e.name, d.name FROM Employees e LEFT JOIN Departments d ON e.dept_id = d.id;
medium
Solution
Step 1: Analyze LEFT JOIN behavior on Employees and Departments
LEFT JOIN keeps all rows from Employees (left table). For employees without matching department, department columns show NULL.Step 2: Understand output columns
Query selects employee name and department name. Employees without department show NULL in department name.Final Answer:
All employees with their department names; NULL for employees without a department -> Option CQuick Check:
LEFT JOIN keeps all left rows, unmatched right columns NULL [OK]
Hint: LEFT JOIN shows all left rows, unmatched right side NULL [OK]
Common Mistakes:
- Thinking only matched rows appear
- Confusing left and right tables
- Expecting departments without employees to appear
4. What is wrong with this query if we want to list all customers and their orders, including customers with no orders?
SELECT c.name, o.order_id FROM Customers c INNER JOIN Orders o ON c.id = o.customer_id;
medium
Solution
Step 1: Understand INNER JOIN behavior
INNER JOIN returns only rows with matching keys in both tables, so customers without orders are excluded.Step 2: Identify correct join for including all customers
LEFT OUTER JOIN keeps all customers even if no matching orders exist, showing NULL for order columns.Final Answer:
INNER JOIN excludes customers without orders; should use LEFT OUTER JOIN -> Option AQuick Check:
Use LEFT OUTER JOIN to include all left table rows [OK]
Hint: Use LEFT OUTER JOIN to include unmatched left table rows [OK]
Common Mistakes:
- Using INNER JOIN when outer join is needed
- Forgetting ON clause (though present here)
- Thinking table order in FROM matters for join type
5. You have two tables:
Students and Enrollments. Some students are not enrolled in any course. Which query correctly lists all students and their courses, showing NULL for students without enrollments?hard
Solution
Step 1: Identify which table has all students
Students table contains all students, including those without enrollments.Step 2: Choose join to keep all students
LEFT OUTER JOIN with Students as left table keeps all students, adding course info or NULL if no enrollment.Step 3: Evaluate other options
INNER JOIN excludes students without enrollments. RIGHT OUTER JOIN with Enrollments left excludes students without enrollments. LEFT OUTER JOIN with Enrollments left table excludes students without enrollments.Final Answer:
SELECT s.name, e.course FROM Students s LEFT OUTER JOIN Enrollments e ON s.id = e.student_id; -> Option DQuick Check:
LEFT OUTER JOIN keeps all left table rows [OK]
Hint: LEFT OUTER JOIN with Students on left keeps all students [OK]
Common Mistakes:
- Using INNER JOIN excludes students without courses
- Swapping left and right tables in join
- Using RIGHT OUTER JOIN incorrectly
