This guide is specifically for PostgreSQL syntax.
Learn SQL LEFT JOIN with hands-on practice exercises. Each exercise includes the table schema, solution query, expected output, and detailed explanation.
What You'll Practice
These exercises cover LEFT JOIN from basic to advanced concepts. Work through each problem to build your SQL skills.
Exercise 1
Question: List all employees and their departments, including employees without a department.
Table Schema
CREATE TABLE employees ( id INT PRIMARY KEY, name VARCHAR(100), department_id INT ); CREATE TABLE departments ( id INT PRIMARY KEY, name VARCHAR(100) );
Solution
SELECT e.name AS employee, d.name AS department FROM employees e LEFT JOIN departments d ON e.department_id = d.id;
Expected Output
| employee | department | |---|---| | John | Sales | | Jane | NULL |
Explanation
LEFT JOIN returns all rows from the left table and matching rows from the right. Unmatched right-side rows show as NULL.
Exercise 2
Question: Find customers who have never placed an order.
Table Schema
CREATE TABLE customers (id INT, name VARCHAR(100)); CREATE TABLE orders (id INT, customer_id INT, total DECIMAL);
Solution
SELECT c.name FROM customers c LEFT JOIN orders o ON c.id = o.customer_id WHERE o.id IS NULL;
Expected Output
| name | |---| | Inactive Customer |
Explanation
This pattern (LEFT JOIN + WHERE IS NULL) finds rows in the left table that have no match in the right table.
Common Mistakes to Avoid
- Forgetting to use proper syntax for LEFT JOIN
- Not considering NULL values in comparisons
- Confusing similar operators or clauses
Related SQL Exercises
Continue practicing with these related topics:
PostgreSQL-Specific Notes
This page covers PostgreSQL syntax. Other databases may have different syntax for similar operations.