SQL Joins Practice for the NSCT
SQL join questions give you two small tables and a query, then ask which rows come back or how many. To answer them you need to know exactly which rows each join keeps, how NULLs behave and how duplicate keys multiply rows. Databases carries 10% of the NSCT, according to HEC's Areas of Competencies, and candidates averaged 42.3% accuracy in it in 2026, per the HEC NSCT report.
This page goes deeper than the official Advanced SQL topic, which introduces joins alongside subqueries and views. Here every join type gets a full output table, and every result below was run in SQLite 3.50.
The method, step by step
- Write down the join condition and the join type. Note which table is left and which is right.
- Count matches per key value. For each key value k, the inner join produces (rows with k in the left table) × (rows with k in the right table).
- Remember NULL keys never match.
NULL = NULLis UNKNOWN, not TRUE. - Add the preserved unmatched rows. LEFT adds unmatched left rows, RIGHT adds unmatched right rows, FULL adds both. Each is padded with NULLs.
- Apply WHERE last. A WHERE condition on the right table's column throws away NULL-padded rows.
- Only then apply GROUP BY, aggregates and ORDER BY.
All the examples use these two tables.
| sid | name | dept_id |
|---|---|---|
| 1 | Ali | 10 |
| 2 | Sara | 20 |
| 3 | Omar | NULL |
| 4 | Hina | 10 |
| 5 | Zain | 40 |
| dept_id | dname |
|---|---|
| 10 | CS |
| 20 | EE |
| 30 | ME |

Inner join
An inner join keeps a pair of rows only when the ON condition is TRUE.
SELECT s.name, d.dname
FROM student s INNER JOIN dept d ON s.dept_id = d.dept_id;
| name | dname |
|---|---|
| Ali | CS |
| Sara | EE |
| Hina | CS |
Omar (NULL) and Zain (40) find no partner, and ME has no student. Result: 3 rows. Writing just JOIN means the same thing.
Left outer join
A left join keeps every row of the left table. Where nothing matches, the right table's columns become NULL.
SELECT s.name, d.dname
FROM student s LEFT JOIN dept d ON s.dept_id = d.dept_id;
| name | dname |
|---|---|
| Ali | CS |
| Sara | EE |
| Omar | NULL |
| Hina | CS |
| Zain | NULL |
Result: 3 matched + 2 unmatched = 5 rows. Add WHERE d.dept_id IS NULL and you get only Omar and Zain. This anti-join pattern finds rows with no match.
Right outer join
A right join keeps every row of the right table. student RIGHT JOIN dept gives the same rows as dept LEFT JOIN student.
| name | dname |
|---|---|
| Ali | CS |
| Hina | CS |
| Sara | EE |
| NULL | ME |
Result: 3 matched + 1 unmatched department = 4 rows. Zain and Omar disappear because the left table is not preserved.
Full outer join
A full outer join keeps unmatched rows from both sides: 3 matched + 2 unmatched students + 1 unmatched department = 6 rows.
| name | dname |
|---|---|
| Ali | CS |
| Sara | EE |
| Omar | NULL |
| Hina | CS |
| Zain | NULL |
| NULL | ME |
MySQL has no FULL OUTER JOIN; you emulate it with a LEFT JOIN UNION a RIGHT JOIN. SQLite added it in version 3.39, and PostgreSQL, SQL Server and Oracle support it.
Cross join
A cross join is the Cartesian product: every row paired with every row, with no condition. 5 students × 3 departments = 15 rows. A cross join filtered by WHERE behaves like a theta join, and a join with a forgotten ON condition behaves like a cross join, which is why some wrong answers are suspiciously large.
Self join
A self join joins a table to itself through two aliases. Take emp(eid, name, mgr) with rows (1, Amna, NULL), (2, Bilal, 1), (3, Sana, 1) and (4, Tariq, 2).
SELECT e.name AS employee, m.name AS manager
FROM emp e LEFT JOIN emp m ON e.mgr = m.eid;
| employee | manager |
|---|---|
| Amna | NULL |
| Bilal | Amna |
| Sana | Amna |
| Tariq | Bilal |
With an inner join, Amna disappears and you get 3 rows. Grouping the inner join by m.name counts direct reports: Amna 2 and Bilal 1.
Duplicate keys and the ON vs WHERE trap
When keys repeat, multiply per key. Let r.k = {1, 1, 2, 4} and s.k = {1, 1, 1, 2, 2, 3}.
| Key | In r | In s | Inner rows |
|---|---|---|---|
| 1 | 2 | 3 | 6 |
| 2 | 1 | 2 | 2 |
| 3 | 0 | 1 | 0 |
| 4 | 1 | 0 | 0 |
The inner join gives 8 rows, LEFT gives 8 + 1 = 9, RIGHT gives 8 + 1 = 9 and FULL gives 8 + 1 + 1 = 10.
Where you put a filter changes an outer join completely. On the student tables:
| Query | Rows |
|---|---|
LEFT JOIN dept d ON s.dept_id = d.dept_id AND d.dname = 'CS' |
5 (Ali and Hina get CS, the other three get NULL) |
LEFT JOIN dept d ON s.dept_id = d.dept_id WHERE d.dname = 'CS' |
2 (Ali and Hina only) |
A filter in ON only decides what matches. A filter in WHERE runs afterwards and removes the NULL-padded rows, which silently turns the left join into an inner join.
Speed tricks
- Inner count = Σ (left count × right count) per key. Tally keys first and never draw the whole product.
- Outer counts build on the inner count: LEFT = inner + unmatched left, RIGHT = inner + unmatched right, FULL = inner + both.
- Cross count = rows × rows. It is a product, never a sum.
- A NULL key is always unmatched, even when the other table also has a NULL.
- A LEFT JOIN never returns fewer rows than the left table. It returns more only when a left row matches several right rows.
Common mistakes
- Expecting NULL to match NULL in an ON condition.
- Using
COUNT(*)after a LEFT JOIN to count child rows. The NULL-padded row counts as 1; useCOUNT(child.id)to get 0. - Filtering the right table in WHERE and still expecting unmatched left rows.
- Adding instead of multiplying for duplicate keys or cross joins.
- Using
NATURAL JOINwhen the tables share an extra column name such asname. It joins on every common column. - Following a LEFT JOIN with an INNER JOIN on the right table's column. The inner join drops the rows the left join preserved.
Practice set
Use a.x = {1, 2, 2, 3, NULL} and b.x = {2, 3, 3, 5, NULL} for questions 1–6.
- How many rows does
a JOIN b ON a.x = b.xreturn? - How many rows does
a LEFT JOIN b ON a.x = b.xreturn? - How many rows does
a RIGHT JOIN b ON a.x = b.xreturn? - How many rows does
a FULL JOIN b ON a.x = b.xreturn? - How many rows does
a CROSS JOIN breturn? - How many rows does the anti-join
a LEFT JOIN b ON a.x = b.x WHERE b.x IS NULLreturn? - Table c(name, city) holds A-LHR, B-LHR, C-KHI and D-LHR. How many rows does
c x JOIN c y ON x.city = y.city AND x.name < y.namereturn? - In question 7, how many rows are returned if
<becomes<>? - Which join type would you use to list every department, including those with no staff?
Show answers
- 4. Key 2: 2 × 1 = 2. Key 3: 1 × 2 = 2. The NULLs do not match.
- 6. 4 matched + unmatched 1 and NULL from a.
- 6. 4 matched + unmatched 5 and NULL from b.
- 8. 4 + 2 + 2.
- 25. 5 × 5.
- 2. The rows with x = 1 and x = NULL.
- 3. The pairs (A, B), (A, D) and (B, D). The
<condition removes self-pairs and mirror pairs. - 6. Each of the 3 pairs now appears in both orders.
- dept LEFT JOIN emp, or equivalently emp RIGHT JOIN dept.
Practise: 20 questions
FAQs
What is the difference between JOIN and INNER JOIN?
There is none. JOIN on its own is shorthand for INNER JOIN in standard SQL and in MySQL, PostgreSQL, SQL Server, Oracle and SQLite. Likewise, LEFT JOIN is shorthand for LEFT OUTER JOIN. The word OUTER is optional and changes nothing.
Can a LEFT JOIN return more rows than the left table has?
Yes. If one left row matches three right rows, it appears three times. A LEFT JOIN guarantees that every left row appears at least once, not exactly once. Only when each left row matches at most one right row does the result equal the left table's row count.
Is a join with the condition in WHERE the same as one in ON?
For an inner join, yes: FROM a, b WHERE a.k = b.k gives the same rows as a JOIN b ON a.k = b.k. For outer joins, no. A condition on the preserved side's partner in WHERE removes NULL-padded rows after the join, so the outer join behaves like an inner join.
How do I write a full outer join in MySQL?
MySQL has no FULL OUTER JOIN. Combine a LEFT JOIN b ... UNION a RIGHT JOIN b .... UNION removes duplicate matched rows, though it would also merge genuinely duplicate rows. A safer pattern is UNION ALL with the right-join half restricted to rows WHERE a.key IS NULL.
Next step
Revisit the official Advanced SQL topic, then move on to SQL query output questions for GROUP BY and subquery tracing. Browse the Databases MCQs hub or take a timed NSCT mock test.