Joins Questions with Answers

8 practice questions on Joins (SQL) with answers and step-by-step solutions. Try 8 samples below, then practise all 8 free in the AptiNest app.

Sample questions

Question 1Easy

How many rows does this query count?

customers
id | name
1  | Asha
2  | Ravi
3  | Meera
orders
id  | customer_id | amount
101 | 1           | 250
102 | 1           | 80
103 | 2           | 120
104 | 5           | 60
SELECT COUNT(*) FROM customers CROSS JOIN orders;
  1. 3
  2. 12
  3. 4
  4. 7

Answer: B. 12

Explanation

A CROSS JOIN pairs every row of one table with every row of the other: 3 x 4 = 12.

Step-by-step solution

  1. A CROSS JOIN pairs every customer with every order.
  2. Customers: 3 rows. Orders: 4 rows.
  3. 3 × 4 = 12 (option B).

Why the other options are wrong

  • A. Three counts only the customers.
  • C. Four counts only the orders.
  • D. Seven adds 3 and 4; a cross join multiplies the row counts.

Tip: Cross join row count = rows in one table × rows in the other.

Question 2Easy

Which join returns all rows from both tables, filling NULLs where there is no match on either side?

  1. LEFT JOIN
  2. INNER JOIN
  3. CROSS JOIN
  4. FULL OUTER JOIN

Answer: D. FULL OUTER JOIN

Explanation

A FULL OUTER JOIN keeps unmatched rows from both the left and the right table.

Step-by-step solution

  1. A FULL OUTER JOIN keeps unmatched rows from both tables.
  2. Unmatched rows from either side get NULLs for the other table's columns.
  3. So option D is correct.

Why the other options are wrong

  • A. A LEFT JOIN keeps only the unmatched left-table rows.
  • B. An INNER JOIN keeps only matching rows.
  • C. A CROSS JOIN pairs every row with every row; it does not fill NULLs for unmatched rows.

Tip: LEFT keeps the left table, RIGHT keeps the right, FULL keeps both.

Question 3Easy

How many rows does this query count?

customers
id | name
1  | Asha
2  | Ravi
3  | Meera
orders
id  | customer_id | amount
101 | 1           | 250
102 | 1           | 80
103 | 2           | 120
104 | 5           | 60
SELECT COUNT(*)
FROM customers c
JOIN orders o ON o.customer_id = c.id;
  1. 4
  2. 7
  3. 2
  4. 3

Answer: D. 3

Explanation

An inner join keeps only matching pairs: Asha has orders 101 and 102, Ravi has 103. Order 104 (customer 5) and Meera have no match.

Step-by-step solution

  1. Inner join keeps matching pairs only.
  2. Asha has orders 101 and 102; Ravi has 103. Order 104 (customer 5) and Meera have no match.
  3. The count is 3 (option D).

Why the other options are wrong

  • A. Four counts all orders, including order 104 with no matching customer.
  • B. Seven adds the two counts together; a join does not add rows like that.
  • C. Two counts customers with orders, not the three joined pairs.

Tip: An inner join count equals the number of matching pairs.

Question 4Medium

What does this query return?

employees
id | name  | dept  | salary | manager_id
1  | Asha  | Sales | 60000  | NULL
2  | Ravi  | Sales | 45000  | 1
3  | Meera | HR    | 50000  | 1
4  | John  | IT    | 70000  | NULL
5  | Kiran | IT    | 45000  | 4
SELECT m.name
FROM employees e
JOIN employees m ON e.manager_id = m.id
WHERE e.name = 'Kiran';
  1. NULL
  2. Asha
  3. John
  4. Kiran

Answer: C. John

Explanation

This is a self-join: the table is used twice, with alias m playing the role of the manager. Kiran's manager_id is 4, which is John.

Step-by-step solution

  1. The query joins Kiran (e) to his manager (m) using e.manager_id = m.id.
  2. Kiran's manager_id is 4.
  3. Employee id 4 is John, so the answer is John (option C).

Why the other options are wrong

  • A. Kiran has a manager_id of 4, so the join finds a row and returns a name.
  • B. Asha is id 1, and she is not Kiran's manager.
  • D. Kiran is the employee; the query returns the manager's name m.name.

Tip: In a self-join, the foreign key on one alias matches the primary key on the other.

Question 5Medium

Which names does this query return?

customers
id | name
1  | Asha
2  | Ravi
3  | Meera
orders
id  | customer_id | amount
101 | 1           | 250
102 | 1           | 80
103 | 2           | 120
104 | 5           | 60
SELECT c.name
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
WHERE o.id IS NULL;
  1. Asha, Ravi
  2. Meera
  3. Asha, Ravi, Meera
  4. (no rows)

Answer: B. Meera

Explanation

This is an anti-join. Customers without orders get NULL order columns from the LEFT JOIN, and the WHERE keeps only those. Only Meera has no orders.

Step-by-step solution

  1. The LEFT JOIN gives NULL order columns to customers with no orders.
  2. WHERE o.id IS NULL keeps only those rows.
  3. Only Meera has no orders, so the answer is Meera (option B).

Why the other options are wrong

  • A. Asha and Ravi both have orders, so they have non-NULL o.id.
  • C. Asha and Ravi have orders, so they are removed.
  • D. Meera has no orders, so she is returned and the result is not empty.

Tip: LEFT JOIN plus WHERE right.id IS NULL finds rows with no match (anti-join).

Question 6Medium

How many rows does this query count?

customers
id | name
1  | Asha
2  | Ravi
3  | Meera
orders
id  | customer_id | amount
101 | 1           | 250
102 | 1           | 80
103 | 2           | 120
104 | 5           | 60
SELECT COUNT(*)
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id;
  1. 4
  2. 12
  3. 3
  4. 5

Answer: A. 4

Explanation

Every customer is kept: Asha matches 2 orders, Ravi 1, and Meera appears once with NULL order columns. 2 + 1 + 1 = 4.

Step-by-step solution

  1. LEFT JOIN keeps every customer, even with no orders.
  2. Asha has 2 matching orders, Ravi has 1, and Meera has no match, so she appears once with NULL order columns.
  3. 2 + 1 + 1 = 4 (option A).

Why the other options are wrong

  • B. 12 is the cross join count, not a left join.
  • C. Three would drop one of the extra Asha order rows.
  • D. Five would add a row that does not exist; there are 4 rows.

Tip: LEFT JOIN count = matched pairs plus one row for each unmatched left-table row.

Question 7Hard

How many rows does this query count?

customers
id | name
1  | Asha
2  | Ravi
3  | Meera
orders
id  | customer_id | amount
101 | 1           | 250
102 | 1           | 80
103 | 2           | 120
104 | 5           | 60
SELECT COUNT(*)
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
WHERE o.amount > 100;
  1. 2
  2. 5
  3. 4
  4. 3

Answer: A. 2

Explanation

The WHERE filter runs after the join and rejects rows where o.amount is NULL, which effectively turns the LEFT JOIN into an inner join. Only 250 (Asha) and 120 (Ravi) remain. Putting the condition in the ON clause would keep all customers.

Step-by-step solution

  1. The LEFT JOIN first gives 4 rows, including Meera with NULL order columns.
  2. WHERE o.amount > 100 is applied after the join; NULL > 100 is not TRUE, so Meera's row is removed.
  3. Only Asha (250) and Ravi (120) remain: the count is 2 (option A).

Why the other options are wrong

  • B. Five is more rows than the join even has; the LEFT JOIN gives 4 rows, and the WHERE removes amounts 80, 60 and NULL.
  • C. Four is the row count before the WHERE filter removes rows.
  • D. Three would keep Meera, whose NULL amount fails the test.

Tip: A condition on the right table in WHERE turns a LEFT JOIN into an inner join; put it in ON to keep all left rows.

Question 8Hard

What does this query return? (columns: name, total)

customers
id | name
1  | Asha
2  | Ravi
3  | Meera
orders
id  | customer_id | amount
101 | 1           | 250
102 | 1           | 80
103 | 2           | 120
104 | 5           | 60
SELECT c.name, SUM(o.amount) AS total
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
GROUP BY c.id, c.name
ORDER BY c.id;
  1. Asha, 250; Ravi, 120; Meera, NULL
  2. Asha, 330; Ravi, 120; Meera, 0
  3. Asha, 330; Ravi, 120
  4. Asha, 330; Ravi, 120; Meera, NULL

Answer: D. Asha, 330; Ravi, 120; Meera, NULL

Explanation

Meera's only joined row has a NULL amount, and SUM over only NULLs returns NULL, not 0. Use COALESCE(SUM(o.amount), 0) to show 0.

Step-by-step solution

  1. Asha has orders 250 and 80, total 330. Ravi has 120.
  2. Meera has no matching order, so her only joined row has a NULL amount.
  3. SUM over only NULLs is NULL, so Meera shows NULL (option D).

Why the other options are wrong

  • A. Asha's total is 330 (250 + 80), not 250.
  • B. Meera's total is NULL, not 0, because SUM of only NULLs is NULL.
  • C. Meera is a customer and must still appear in a LEFT JOIN result.

Tip: Use COALESCE(SUM(o.amount), 0) to show 0 instead of NULL for groups with no matches.

Practise all 8 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all SQL topics · All topics