SQL Exercises

SQL LEFT JOIN Exercises (With Answers)

8 Exercises
~40 min
8 Intermediate

Exercise 1

intermediate

Question

List all employees and their departments, including employees without a department.

Table Schema

SQL
CREATE TABLE employees (
  id INT PRIMARY KEY,
  name VARCHAR(100),
  department_id INT
);

CREATE TABLE departments (
  id INT PRIMARY KEY,
  name VARCHAR(100)
);
Show Solution

Solution

SQL
1SELECT e.name AS employee, d.name AS department
2FROM employees e
3LEFT 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 1

intermediate

Question

List all employees and their departments, including employees without a department.

Table Schema

SQL
CREATE TABLE employees (
  id INT PRIMARY KEY,
  name VARCHAR(100),
  department_id INT
);

CREATE TABLE departments (
  id INT PRIMARY KEY,
  name VARCHAR(100)
);
Show Solution

Solution

SQL
1SELECT e.name AS employee, d.name AS department
2FROM employees e
3LEFT 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 1

intermediate

Question

List all employees and their departments, including employees without a department.

Table Schema

SQL
CREATE TABLE employees (
  id INT PRIMARY KEY,
  name VARCHAR(100),
  department_id INT
);

CREATE TABLE departments (
  id INT PRIMARY KEY,
  name VARCHAR(100)
);
Show Solution

Solution

SQL
1SELECT e.name AS employee, d.name AS department
2FROM employees e
3LEFT 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 1

intermediate

Question

List all employees and their departments, including employees without a department.

Table Schema

SQL
CREATE TABLE employees (
  id INT PRIMARY KEY,
  name VARCHAR(100),
  department_id INT
);

CREATE TABLE departments (
  id INT PRIMARY KEY,
  name VARCHAR(100)
);
Show Solution

Solution

SQL
1SELECT e.name AS employee, d.name AS department
2FROM employees e
3LEFT 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

intermediate

Question

Find customers who have never placed an order.

Table Schema

SQL
CREATE TABLE customers (id INT, name VARCHAR(100));
CREATE TABLE orders (id INT, customer_id INT, total DECIMAL);
Show Solution

Solution

SQL
1SELECT c.name
2FROM customers c
3LEFT JOIN orders o ON c.id = o.customer_id
4WHERE 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.

Exercise 2

intermediate

Question

Find customers who have never placed an order.

Table Schema

SQL
CREATE TABLE customers (id INT, name VARCHAR(100));
CREATE TABLE orders (id INT, customer_id INT, total DECIMAL);
Show Solution

Solution

SQL
1SELECT c.name
2FROM customers c
3LEFT JOIN orders o ON c.id = o.customer_id
4WHERE 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.

Exercise 2

intermediate

Question

Find customers who have never placed an order.

Table Schema

SQL
CREATE TABLE customers (id INT, name VARCHAR(100));
CREATE TABLE orders (id INT, customer_id INT, total DECIMAL);
Show Solution

Solution

SQL
1SELECT c.name
2FROM customers c
3LEFT JOIN orders o ON c.id = o.customer_id
4WHERE 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.

Exercise 2

intermediate

Question

Find customers who have never placed an order.

Table Schema

SQL
CREATE TABLE customers (id INT, name VARCHAR(100));
CREATE TABLE orders (id INT, customer_id INT, total DECIMAL);
Show Solution

Solution

SQL
1SELECT c.name
2FROM customers c
3LEFT JOIN orders o ON c.id = o.customer_id
4WHERE 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.

Related Content

From Our Blog

Ready for more practice?

Join SQL Mastery and get access to interactive exercises, quizzes, and more.