SQL Joins Practice for the NSCT

By the NSCT Exam Editorial Team · Updated 2026-11-24 · Facts checked against official sources on 2026-11-24

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

  1. Write down the join condition and the join type. Note which table is left and which is right.
  2. 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).
  3. Remember NULL keys never match. NULL = NULL is UNKNOWN, not TRUE.
  4. Add the preserved unmatched rows. LEFT adds unmatched left rows, RIGHT adds unmatched right rows, FULL adds both. Each is padded with NULLs.
  5. Apply WHERE last. A WHERE condition on the right table's column throws away NULL-padded rows.
  6. 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
Four Venn diagrams for INNER, LEFT, RIGHT and FULL OUTER JOIN on the page's student and department tables, returning 3, 5, 4 and 6 rows.
Which rows each join keeps, using this page's student and department tables. Row counts were run in SQLite.

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

Common mistakes

Practice set

Use a.x = {1, 2, 2, 3, NULL} and b.x = {2, 3, 3, 5, NULL} for questions 1–6.

  1. How many rows does a JOIN b ON a.x = b.x return?
  2. How many rows does a LEFT JOIN b ON a.x = b.x return?
  3. How many rows does a RIGHT JOIN b ON a.x = b.x return?
  4. How many rows does a FULL JOIN b ON a.x = b.x return?
  5. How many rows does a CROSS JOIN b return?
  6. How many rows does the anti-join a LEFT JOIN b ON a.x = b.x WHERE b.x IS NULL return?
  7. 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.name return?
  8. In question 7, how many rows are returned if < becomes <>?
  9. Which join type would you use to list every department, including those with no staff?
Show answers
  1. 4. Key 2: 2 × 1 = 2. Key 3: 1 × 2 = 2. The NULLs do not match.
  2. 6. 4 matched + unmatched 1 and NULL from a.
  3. 6. 4 matched + unmatched 5 and NULL from b.
  4. 8. 4 + 2 + 2.
  5. 25. 5 × 5.
  6. 2. The rows with x = 1 and x = NULL.
  7. 3. The pairs (A, B), (A, D) and (B, D). The < condition removes self-pairs and mirror pairs.
  8. 6. Each of the 3 pairs now appears in both orders.
  9. dept LEFT JOIN emp, or equivalently emp RIGHT JOIN dept.

Practise: 20 questions

Interactive quiz loading. The first 10 questions are shown below.

  1. Which join returns only the rows that have a matching value in both tables?

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

    Answer: A. INNER JOIN

    An INNER JOIN keeps a pair of rows only when the join condition is TRUE for both. Outer joins also keep unmatched rows padded with NULLs, and a CROSS JOIN pairs every row with every row with no condition at all.

  2. In a LEFT JOIN, a left-table row has no matching row in the right table. What appears in the right table's columns for that row?

    1. The row is dropped from the result
    2. Zero or an empty string, depending on the column type
    3. The values from the first row of the right table
    4. NULL in every right-table column
    Show answer

    Answer: D. NULL in every right-table column

    A LEFT (outer) JOIN preserves every row of the left table. When no right row matches, the right side is padded with NULLs, not zeros or empty strings. Dropping the row is what an INNER JOIN would do.

  3. Both tables have a row whose join column is NULL. Why does an INNER JOIN ON a.k = b.k not pair those two rows?

    1. SQL removes all rows containing NULL before any join runs
    2. The two NULLs are treated as different data types
    3. INNER JOIN only works on primary key columns, which cannot hold NULL
    4. NULL = NULL evaluates to UNKNOWN, and a join keeps only pairs where the condition is TRUE
    Show answer

    Answer: D. NULL = NULL evaluates to UNKNOWN, and a join keeps only pairs where the condition is TRUE

    Under SQL's three-valued logic, any comparison with NULL, including NULL = NULL, gives UNKNOWN. A join condition must be TRUE to keep a pair, so NULL keys never match each other. The rows are not removed beforehand; they simply find no partner.

  4. Which query returns the same rows as A RIGHT JOIN B ON A.id = B.id (ignoring column order)?

    1. A LEFT JOIN B ON A.id = B.id
    2. B LEFT JOIN A ON A.id = B.id
    3. A INNER JOIN B ON A.id = B.id
    4. A FULL OUTER JOIN B ON A.id = B.id
    Show answer

    Answer: B. B LEFT JOIN A ON A.id = B.id

    A RIGHT JOIN preserves every row of the right table, B. Swapping the table order and using LEFT JOIN preserves B in the same way, so B LEFT JOIN A gives identical rows. A LEFT JOIN B would preserve A instead.

  5. What must you use to join a table to itself, for example to list each employee beside their manager?

    1. A UNION of the table with itself
    2. A CROSS JOIN with no condition
    3. Two different table aliases, such as emp e and emp m
    4. A separate copy of the table created with CREATE TABLE
    Show answer

    Answer: C. Two different table aliases, such as emp e and emp m

    A self join lists the same table twice in FROM. Aliases (e for the employee, m for the manager) let the ON clause tell the two copies apart, as in e.mgr = m.eid. No physical copy of the table is needed.

  6. What does the pattern LEFT JOIN B ON A.k = B.k WHERE B.k IS NULL return?

    1. Rows of B that have no matching row in A
    2. Rows of A that have no matching row in B
    3. Rows of A whose k value is NULL only
    4. Pairs of rows that match in both tables
    Show answer

    Answer: B. Rows of A that have no matching row in B

    This is an anti-join. The LEFT JOIN pads unmatched A rows with NULLs in B's columns, and WHERE B.k IS NULL keeps exactly those rows. Matched rows have a non-NULL B.k (when B.k is the join column), so they are removed.

  7. How many rows does the INNER JOIN return?

    CREATE TABLE course (cid INT, title TEXT);
    INSERT INTO course VALUES (1,'DB'), (2,'OS'), (3,'AI');
    CREATE TABLE enrol (sid INT, cid INT);
    INSERT INTO enrol VALUES (10,1), (11,1), (12,2), (13,4);
    SELECT e.sid, c.title
    FROM enrol e INNER JOIN course c ON e.cid = c.cid;
    1. 4
    2. 3
    3. 2
    4. 12
    Show answer

    Answer: B. 3

    Enrolments 10 and 11 match course 1 (DB) and enrolment 12 matches course 2 (OS). Enrolment 13 points to course 4, which does not exist, so it is dropped. AI has no enrolments and is also absent. That gives 3 rows.

  8. How many rows does the LEFT JOIN return?

    CREATE TABLE cust (id INT, name TEXT);
    INSERT INTO cust VALUES (1,'Ali'), (2,'Sara'), (3,'Omar');
    CREATE TABLE ord (oid INT, cust_id INT);
    INSERT INTO ord VALUES (100,1), (101,1), (102,1), (103,3), (104,9);
    SELECT c.name, o.oid
    FROM cust c LEFT JOIN ord o ON c.id = o.cust_id;
    1. 3
    2. 4
    3. 6
    4. 5
    Show answer

    Answer: D. 5

    Ali matches three orders (3 rows), Omar matches one (1 row) and Sara matches none but is kept with a NULL oid (1 row): 3 + 1 + 1 = 5. Order 104 belongs to a missing customer and is not preserved, because only the left table is.

  9. How many rows does the FULL OUTER JOIN return? (SQLite supports FULL OUTER JOIN from version 3.39.)

    CREATE TABLE a (k INT); INSERT INTO a VALUES (1), (2), (3), (3);
    CREATE TABLE b (k INT); INSERT INTO b VALUES (3), (4), (5);
    SELECT a.k, b.k FROM a FULL OUTER JOIN b ON a.k = b.k;
    1. 6
    2. 5
    3. 7
    4. 12
    Show answer

    Answer: A. 6

    Matches: the two 3s in a each pair with the single 3 in b, giving 2 rows. Unmatched a rows: 1 and 2 (2 rows). Unmatched b rows: 4 and 5 (2 rows). Total 2 + 2 + 2 = 6.

  10. What does this self join return?

    CREATE TABLE emp (eid INT, name TEXT, mgr INT);
    INSERT INTO emp VALUES (1,'Nadia',NULL), (2,'Faraz',1),
      (3,'Kiran',1), (4,'Imran',3), (5,'Saba',3);
    SELECT m.name, COUNT(*) AS reports
    FROM emp e JOIN emp m ON e.mgr = m.eid
    GROUP BY m.name ORDER BY m.name;
    1. Kiran 2, Nadia 2
    2. Faraz 1, Kiran 2, Nadia 2
    3. Kiran 2, Nadia 2, NULL 1
    4. Nadia 4
    Show answer

    Answer: A. Kiran 2, Nadia 2

    Each row pairs an employee e with their manager m. Faraz and Kiran report to Nadia (2), and Imran and Saba report to Kiran (2). Faraz manages nobody, so he never appears as m. Nadia has no manager, and the inner join drops her as an employee row rather than adding a NULL group.

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.