SQL Exercises

SQL INNER JOIN Exercises (With Answers)

12 Exercises
~60 min
8 Intermediate4 Advanced

Exercise 1

intermediate

Question

Join employees with their departments to show employee name and department name.

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_name, d.name AS department_name
2FROM employees e
3INNER JOIN departments d ON e.department_id = d.id;

Expected Output

| employee_name | department_name |
|---|---|
| John | Sales |
| Jane | Engineering |

Explanation

INNER JOIN returns only rows that have matching values in both tables. Use table aliases (e, d) for cleaner code.

Exercise 1

intermediate

Question

Join employees with their departments to show employee name and department name.

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_name, d.name AS department_name
2FROM employees e
3INNER JOIN departments d ON e.department_id = d.id;

Expected Output

| employee_name | department_name |
|---|---|
| John | Sales |
| Jane | Engineering |

Explanation

INNER JOIN returns only rows that have matching values in both tables. Use table aliases (e, d) for cleaner code.

Exercise 1

intermediate

Question

Join employees with their departments to show employee name and department name.

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_name, d.name AS department_name
2FROM employees e
3INNER JOIN departments d ON e.department_id = d.id;

Expected Output

| employee_name | department_name |
|---|---|
| John | Sales |
| Jane | Engineering |

Explanation

INNER JOIN returns only rows that have matching values in both tables. Use table aliases (e, d) for cleaner code.

Exercise 1

intermediate

Question

Join employees with their departments to show employee name and department name.

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_name, d.name AS department_name
2FROM employees e
3INNER JOIN departments d ON e.department_id = d.id;

Expected Output

| employee_name | department_name |
|---|---|
| John | Sales |
| Jane | Engineering |

Explanation

INNER JOIN returns only rows that have matching values in both tables. Use table aliases (e, d) for cleaner code.

Exercise 2

intermediate

Question

Find all orders with their customer names and product names.

Table Schema

SQL
CREATE TABLE customers (id INT, name VARCHAR(100));
CREATE TABLE products (id INT, name VARCHAR(100), price DECIMAL);
CREATE TABLE orders (id INT, customer_id INT, product_id INT, quantity INT);
Show Solution

Solution

SQL
1SELECT c.name AS customer, p.name AS product, o.quantity
2FROM orders o
3INNER JOIN customers c ON o.customer_id = c.id
4INNER JOIN products p ON o.product_id = p.id;

Expected Output

| customer | product | quantity |
|---|---|---|
| John | Laptop | 2 |
| Jane | Phone | 1 |

Explanation

You can chain multiple JOINs to connect several tables. Each JOIN adds one more table to the result.

Exercise 2

intermediate

Question

Find all orders with their customer names and product names.

Table Schema

SQL
CREATE TABLE customers (id INT, name VARCHAR(100));
CREATE TABLE products (id INT, name VARCHAR(100), price DECIMAL);
CREATE TABLE orders (id INT, customer_id INT, product_id INT, quantity INT);
Show Solution

Solution

SQL
1SELECT c.name AS customer, p.name AS product, o.quantity
2FROM orders o
3INNER JOIN customers c ON o.customer_id = c.id
4INNER JOIN products p ON o.product_id = p.id;

Expected Output

| customer | product | quantity |
|---|---|---|
| John | Laptop | 2 |
| Jane | Phone | 1 |

Explanation

You can chain multiple JOINs to connect several tables. Each JOIN adds one more table to the result.

Exercise 2

intermediate

Question

Find all orders with their customer names and product names.

Table Schema

SQL
CREATE TABLE customers (id INT, name VARCHAR(100));
CREATE TABLE products (id INT, name VARCHAR(100), price DECIMAL);
CREATE TABLE orders (id INT, customer_id INT, product_id INT, quantity INT);
Show Solution

Solution

SQL
1SELECT c.name AS customer, p.name AS product, o.quantity
2FROM orders o
3INNER JOIN customers c ON o.customer_id = c.id
4INNER JOIN products p ON o.product_id = p.id;

Expected Output

| customer | product | quantity |
|---|---|---|
| John | Laptop | 2 |
| Jane | Phone | 1 |

Explanation

You can chain multiple JOINs to connect several tables. Each JOIN adds one more table to the result.

Exercise 2

intermediate

Question

Find all orders with their customer names and product names.

Table Schema

SQL
CREATE TABLE customers (id INT, name VARCHAR(100));
CREATE TABLE products (id INT, name VARCHAR(100), price DECIMAL);
CREATE TABLE orders (id INT, customer_id INT, product_id INT, quantity INT);
Show Solution

Solution

SQL
1SELECT c.name AS customer, p.name AS product, o.quantity
2FROM orders o
3INNER JOIN customers c ON o.customer_id = c.id
4INNER JOIN products p ON o.product_id = p.id;

Expected Output

| customer | product | quantity |
|---|---|---|
| John | Laptop | 2 |
| Jane | Phone | 1 |

Explanation

You can chain multiple JOINs to connect several tables. Each JOIN adds one more table to the result.

Exercise 3

advanced

Question

List employees with their manager's name using a self-join.

Table Schema

SQL
CREATE TABLE employees (
  id INT PRIMARY KEY,
  name VARCHAR(100),
  manager_id INT
);
Show Solution

Solution

SQL
1SELECT e.name AS employee, m.name AS manager
2FROM employees e
3INNER JOIN employees m ON e.manager_id = m.id;

Expected Output

| employee | manager |
|---|---|
| Jane | John |
| Bob | Jane |

Explanation

A self-join joins a table to itself. Use different aliases to distinguish between the two instances of the same table.

Exercise 3

advanced

Question

List employees with their manager's name using a self-join.

Table Schema

SQL
CREATE TABLE employees (
  id INT PRIMARY KEY,
  name VARCHAR(100),
  manager_id INT
);
Show Solution

Solution

SQL
1SELECT e.name AS employee, m.name AS manager
2FROM employees e
3INNER JOIN employees m ON e.manager_id = m.id;

Expected Output

| employee | manager |
|---|---|
| Jane | John |
| Bob | Jane |

Explanation

A self-join joins a table to itself. Use different aliases to distinguish between the two instances of the same table.

Exercise 3

advanced

Question

List employees with their manager's name using a self-join.

Table Schema

SQL
CREATE TABLE employees (
  id INT PRIMARY KEY,
  name VARCHAR(100),
  manager_id INT
);
Show Solution

Solution

SQL
1SELECT e.name AS employee, m.name AS manager
2FROM employees e
3INNER JOIN employees m ON e.manager_id = m.id;

Expected Output

| employee | manager |
|---|---|
| Jane | John |
| Bob | Jane |

Explanation

A self-join joins a table to itself. Use different aliases to distinguish between the two instances of the same table.

Exercise 3

advanced

Question

List employees with their manager's name using a self-join.

Table Schema

SQL
CREATE TABLE employees (
  id INT PRIMARY KEY,
  name VARCHAR(100),
  manager_id INT
);
Show Solution

Solution

SQL
1SELECT e.name AS employee, m.name AS manager
2FROM employees e
3INNER JOIN employees m ON e.manager_id = m.id;

Expected Output

| employee | manager |
|---|---|
| Jane | John |
| Bob | Jane |

Explanation

A self-join joins a table to itself. Use different aliases to distinguish between the two instances of the same table.

Related Content

From Our Blog

Ready for more practice?

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