Inner & Outer Joins Questions with Answers

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

Sample questions

Question 1Easy

How many rows does this query return?

learners
id | name   | course_id
1  | Asmita | 10
2  | Bharat | 20
3  | Charu  | NULL
4  | Dev    | 10
5  | Esha   | 40
courses
id | title  | fee
10 | Java   | 5000
20 | Python | 4000
30 | SQL    | 3000
SELECT l.name, c.title
FROM learners l
LEFT JOIN courses c ON l.course_id = c.id;
  1. 4
  2. 5
  3. 6
  4. 3

Answer: B. 5

Explanation

A LEFT JOIN keeps every learner; Charu and Esha get NULL for title.

Step-by-step solution

  1. A LEFT JOIN keeps every learner, even when no course matches.
  2. There are 5 learners, so 5 rows; Charu and Esha get NULL course titles.
  3. So the answer is option B, 5 rows.

Why the other options are wrong

  • A. 4 would drop one learner, but LEFT JOIN keeps all of them.
  • C. 6 would need extra rows, but no learner matches more than one course.
  • D. 3 is the inner join count; LEFT JOIN keeps the unmatched learners too.

Tip: A LEFT JOIN returns at least one row for every left-side row.

Question 2Easy

What is the difference between INNER JOIN and LEFT OUTER JOIN?

  1. INNER JOIN also keeps unmatched rows from both tables
  2. INNER JOIN returns only matching pairs; LEFT JOIN also returns unmatched left rows with NULLs for the right columns
  3. LEFT JOIN returns only rows from the left table
  4. They differ only in performance

Answer: B. INNER JOIN returns only matching pairs; LEFT JOIN also returns unmatched left rows with NULLs for the right columns

Explanation

An outer join preserves the rows of one side even when the join condition finds no partner.

Step-by-step solution

  1. An inner join returns only the pairs that match the condition.
  2. A left outer join also keeps each unmatched left row, with NULLs for the right columns.
  3. So the difference is option B.

Why the other options are wrong

  • A. An inner join does not keep unmatched rows from either table.
  • C. A left join returns the matched rows as well, not only left rows.
  • D. The difference is in the rows returned, not only in speed.

Tip: Inner join returns matches only; left join returns all left rows plus the matches.

Question 3Easy

Which join returns all rows from the left table and the matching rows from the right table, with NULL where there is no match?

  1. INNER JOIN
  2. LEFT JOIN
  3. CROSS JOIN
  4. SELF JOIN

Answer: B. LEFT JOIN

Explanation

LEFT (OUTER) JOIN keeps every left row and fills missing right columns with NULL.

Step-by-step solution

  1. The question asks for all left rows plus matching right rows, with NULL where nothing matches.
  2. That is the rule of a LEFT (OUTER) JOIN.
  3. So the answer is LEFT JOIN (option B).

Why the other options are wrong

  • A. INNER JOIN keeps only rows that match on both sides.
  • C. CROSS JOIN pairs every left row with every right row, with no match rule.
  • D. SELF JOIN means a table joined to itself, not a rule about keeping left rows.

Tip: LEFT JOIN keeps every row of the left table; INNER JOIN keeps only matches.

Question 4Easy

How many rows does this query return?

learners
id | name   | course_id
1  | Asmita | 10
2  | Bharat | 20
3  | Charu  | NULL
4  | Dev    | 10
5  | Esha   | 40
courses
id | title  | fee
10 | Java   | 5000
20 | Python | 4000
30 | SQL    | 3000
SELECT l.name, c.title
FROM learners l
INNER JOIN courses c ON l.course_id = c.id;
  1. 3
  2. 2
  3. 5
  4. 4

Answer: A. 3

Explanation

Only Asmita, Bharat and Dev have a course_id that exists in courses. Charu (NULL) and Esha (40) have no match.

Step-by-step solution

  1. An inner join keeps only learners whose course_id matches a course id.
  2. Asmita (10), Bharat (20) and Dev (10) match; Charu has NULL and Esha has 40, which has no match.
  3. So 3 rows are returned, option A.

Why the other options are wrong

  • B. 2 leaves out Dev, who matches the Java course.
  • C. 5 includes all learners, but an inner join drops the unmatched ones.
  • D. 4 is wrong; only three learners have a matching course.

Tip: An inner join keeps only rows that match on both sides.

Question 5Medium

Which query returns learners who are NOT enrolled in any existing course (anti-join)?

  1. SELECT l.name FROM learners l RIGHT JOIN courses c ON c.id = l.course_id WHERE l.id IS NULL;
  2. SELECT l.name FROM learners l JOIN courses c ON c.id <> l.course_id;
  3. SELECT l.name FROM learners l LEFT JOIN courses c ON c.id = l.course_id WHERE l.course_id IS NOT NULL;
  4. SELECT l.name FROM learners l LEFT JOIN courses c ON c.id = l.course_id WHERE c.id IS NULL;

Answer: D. SELECT l.name FROM learners l LEFT JOIN courses c ON c.id = l.course_id WHERE c.id IS NULL;

Explanation

After a LEFT JOIN, a NULL in the right table’s key column marks learners with no matching course (here Charu and Esha).

Step-by-step solution

  1. An anti-join needs the learners with no matching course.
  2. A LEFT JOIN from learners to courses keeps them, with NULL course columns when unmatched.
  3. WHERE c.id IS NULL keeps only those rows, so the answer is option D.

Why the other options are wrong

  • A. A RIGHT JOIN keeps courses, so this returns courses with no learner, not learners.
  • B. The inner join with <> pairs learners with other courses, so it does not find learners with no course.
  • C. This keeps learners who do have a course, the opposite of what is needed.

Tip: Anti-join: LEFT JOIN from the wanted side, then WHERE the right key IS NULL.

Question 6Medium

What does this query return?

learners
id | name   | course_id
1  | Asmita | 10
2  | Bharat | 20
3  | Charu  | NULL
4  | Dev    | 10
5  | Esha   | 40
courses
id | title  | fee
10 | Java   | 5000
20 | Python | 4000
30 | SQL    | 3000
SELECT l.name
FROM learners l
JOIN courses c ON c.id = l.course_id AND c.title <> 'Java'
ORDER BY l.name;
  1. Bharat, Charu, Esha
  2. Asmita, Dev
  3. Bharat
  4. Bharat, Esha

Answer: C. Bharat

Explanation

For an inner join, a condition in ON behaves like WHERE: only Bharat’s match (Python) survives.

Step-by-step solution

  1. The ON clause has the key match and the title test, so for an inner join it works like WHERE.
  2. Asmita and Dev match Java, which is excluded; Bharat matches Python and passes.
  3. Charu has NULL and Esha has course 40 with no match, so only Bharat remains: option C.

Why the other options are wrong

  • A. Charu has a NULL course_id and Esha has course 40, so neither matches.
  • B. Asmita and Dev are Java students, and the title test excludes Java.
  • D. Esha has course 40, which has no match, so she is not returned.

Tip: In an inner join, a test in ON behaves like WHERE; in a LEFT JOIN it does not.

Question 7Medium

How many rows does this query return?

a
k
1
1
2
NULL
b
k
1
1
1
NULL
SELECT *
FROM a JOIN b ON a.k = b.k;
  1. 8
  2. 2
  3. 6
  4. 3

Answer: C. 6

Explanation

Each of the two a-rows with k = 1 pairs with all three b-rows with k = 1:
2 × 3 = 6.
NULL keys never match each other.

Step-by-step solution

  1. Each of the two a-rows with k = 1 matches each of the three b-rows with k = 1.
  2. That gives 2 × 3 = 6 pairs.
  3. NULL never equals NULL in a join, so the NULL rows add nothing; the answer is option C, 6 rows.

Why the other options are wrong

  • A. 8 would need the NULL keys to match, and they do not.
  • B. 2 counts only the a-rows, not the matched pairs.
  • D. 3 counts only the b-rows, not the pairs.

Tip: Multiply the matching counts for each key; NULL does not match NULL in a join condition.

Question 8Medium

What does this query return?

departments
id | dname
1  | IT
2  | Finance
3  | Legal
staff
id | name  | dept_id | salary | mgr_id
1  | Om    | 1       | 90000  | NULL
2  | Pia   | 1       | 60000  | 1
3  | Qadir | 2       | 75000  | 1
4  | Riya  | 2       | 50000  | 3
5  | Sam   | NULL    | 40000  | 3
6  | Tara  | 1       | 60000  | 2
SELECT d.dname, MAX(s.salary)
FROM departments d
JOIN staff s ON s.dept_id = d.id
GROUP BY d.dname
ORDER BY d.dname;
  1. Finance 50000, IT 60000
  2. Finance 75000, IT 90000
  3. Finance 75000, IT 90000, Legal NULL
  4. IT 90000

Answer: B. Finance 75000, IT 90000

Explanation

The inner join drops Legal (no staff) and Sam (no department); MAX is taken per remaining department.

Step-by-step solution

  1. The inner JOIN keeps only departments with staff, so Legal is dropped.
  2. Sam (dept NULL) matches no department and is dropped too.
  3. IT's maximum salary is 90000 (Om) and Finance's is 75000 (Qadir), so the answer is option B.

Why the other options are wrong

  • A. The maximums are 90000 for IT and 75000 for Finance; 50000 and 60000 are not maximums.
  • C. Legal is dropped by the inner join, so no Legal row is returned.
  • D. Finance has staff, so it is returned too.

Tip: An inner join drops departments with no staff; MAX is taken for each remaining group.

Question 9Hard

How many rows does this query return?

learners
id | name   | course_id
1  | Asmita | 10
2  | Bharat | 20
3  | Charu  | NULL
4  | Dev    | 10
5  | Esha   | 40
courses
id | title  | fee
10 | Java   | 5000
20 | Python | 4000
30 | SQL    | 3000
SELECT l.name, c.title
FROM learners l
LEFT JOIN courses c ON l.course_id = c.id
WHERE c.fee > 4500;
  1. 2
  2. 3
  3. 4
  4. 5

Answer: A. 2

Explanation

Filtering the right table in WHERE removes the NULL-extended rows (NULL > 4500 is UNKNOWN), turning the LEFT JOIN into an inner join: only Asmita and Dev.

Step-by-step solution

  1. The LEFT JOIN first keeps all learners, with NULL course fields for unmatched ones.
  2. WHERE c.fee > 4500 removes rows where the fee is NULL, because NULL > 4500 is UNKNOWN.
  3. Only Asmita and Dev have the Java course (fee 5000) left, so the answer is option A, 2 rows.

Why the other options are wrong

  • B. 3 would keep Bharat, whose Python fee of 4000 fails the test.
  • C. 4 would keep unmatched rows, which the WHERE removes.
  • D. 5 is the count before the WHERE removes the NULL-fee rows.

Tip: A WHERE test on the right table turns a LEFT JOIN into an inner join; move the test into ON to keep the rows.

Question 10Hard

How many rows does this query return?

a
k
1
1
2
NULL
b
k
1
1
1
NULL
SELECT *
FROM a LEFT JOIN b ON a.k = b.k;
  1. 6
  2. 4
  3. 7
  4. 8

Answer: D. 8

Explanation

6 matched pairs for k = 1, plus the unmatched a-rows 2 and NULL, each once with NULLs: 8.

Step-by-step solution

  1. A LEFT JOIN keeps all 4 rows of a.
  2. The two k = 1 rows each match 3 b-rows, giving 6 rows.
  3. The k = 2 row and the NULL row have no match, so each stays once: 6 + 1 + 1 = 8, option D.

Why the other options are wrong

  • A. 6 counts only the matched pairs and drops the unmatched a-rows.
  • B. 4 counts the a-rows but not the matched pairs.
  • C. 7 is one short; the NULL row must be kept too.

Tip: LEFT JOIN rows are the matched pairs plus one row for each unmatched left row.

Practise all 27 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