QA Interview Hub · Knowledge base · Quizzes · Українська

SQL › JOIN

Types of JOIN and users without orders

JOIN kinds build pairs by the ON condition and differ only in the fate of rows without a pair. INNER drops them, LEFT keeps the left ones, RIGHT the right ones, FULL OUTER both, and CROSS has no condition and returns all combinations. Users without orders are found with LEFT JOIN and o.id IS NULL or NOT EXISTS; NOT IN is avoided because one NULL gives an empty result.

SQL · JOIN

An interviewer asks: show all kinds of JOIN and find users with no orders. Where do you start?

This is a typical question that is easy to answer with a memorised list and hard to answer in a way that shows understanding. An answer that works starts not with names but with one question: what should happen to a row that has no pair. This lesson gathers the JOIN topic into one shape of answer, with numbers on a concrete data set and with the trap that the interviewer usually looks for.

How it works and why. All kinds of JOIN build pairs of rows by the ON condition and differ only in the fate of rows without a pair. INNER drops them on both sides, LEFT keeps the left ones, RIGHT the right ones, FULL OUTER both, and CROSS has no condition and outputs all combinations. SELF JOIN is not a separate kind but an ordinary JOIN where a table is joined to itself through aliases. So the line of reasoning is the same for any of them: which pair am I looking for, whose rows must survive, how many rows do I expect.

Example: one data set, five results. Three users (Anna, Ivan, Olha) and four orders: 100 and 101 belong to Anna, 102 to Ivan, 103 is a guest order with user_id = NULL.

SELECT u.email, o.id FROM users u JOIN orders o       ON o.user_id = u.id;
SELECT u.email, o.id FROM users u LEFT JOIN orders o  ON o.user_id = u.id;
SELECT u.email, o.id FROM users u RIGHT JOIN orders o ON o.user_id = u.id;
SELECT u.email, o.id FROM users u FULL JOIN orders o  ON o.user_id = u.id;
SELECT u.email, o.id FROM users u CROSS JOIN orders o;

One data set, five kinds of JOIN

INNER: 3 rowspairs only: Anna ×2, IvanLEFT: 4 rows+ Olha with NULL (a user with no orders)RIGHT: 4 rows+ order 103 with NULL (no user)FULL OUTER: 5 rowsboth sides: Olha and order 103CROSS: 12 rowsno condition: 3 × 4 pairs
Figure 1. The difference in row counts is the difference in the fate of rows without a pair: Olha and order 103.

The numbers are useful as a self-check: INNER gives 3, because there are three pairs. LEFT adds Olha, because she is on the left. RIGHT adds order 103, because it is on the right. FULL OUTER adds both, so 3 + 1 + 1 = 5. CROSS ignores the link and gives 3 × 4 = 12. If the result does not match the expectation, the cause is almost always in one of three places: a NULL in the key (row 103), a duplicate of pairs (row multiplication) or a condition in the wrong place (ON or WHERE).

The second part of the question, users without orders, tests whether you know the NULL trap. The data holds order 103 with user_id = NULL, so the naive variant gives 0 rows:

-- users with no orders: Olha
SELECT u.id, u.email
FROM users u
WHERE NOT EXISTS (SELECT 1 FROM orders o WHERE o.user_id = u.id);

-- NOT IN gives 0 rows, because orders.user_id has a NULL (order 103)
SELECT u.id, u.email
FROM users u
WHERE u.id NOT IN (SELECT user_id FROM orders);

The same search, two ways

Typical

NOT IN (subquery)

Because of the NULL in the subquery the result is empty. The conclusion "everyone bought something" is wrong.

Better

NOT EXISTS

Finds Olha. LEFT JOIN … IS NULL on the right table key gives the same.

Figure 2. The answer "NOT EXISTS or LEFT JOIN and IS NULL" shows that you know about NULL.

Limits and alternatives. Not every case is solved by a kind of JOIN. If you only need to check that a related record exists, EXISTS does not multiply rows. If you compare two sets of keys, there are EXCEPT and INTERSECT. Dialects differ: MySQL has no FULL OUTER JOIN, in PostgreSQL a JOIN without ON is an error and in MySQL it works as CROSS. It is also worth mentioning that the optimiser chooses the order and method itself, so the kind of JOIN defines the meaning of a query, not its speed: speed depends on indexes on the link columns.

Common mistakes.

  • Starting with a list of names instead of the question "what to do with a row without a pair".
  • A condition on the right table of a LEFT JOIN in WHERE: the query quietly becomes INNER.
  • A sum or count after a one-to-many JOIN without checking the row count.
  • NOT IN with a subquery where NULL is possible: an empty result that looks like "no orphans".
  • A forgotten link condition: a Cartesian product instead of pairs.
  • Promising FULL OUTER JOIN in MySQL: it does not exist.

Check yourself

Table A has 10 rows and B has 10. 7 pairs match by key, and the key is unique on both sides. How many rows do INNER, LEFT and FULL OUTER return?

Answer

INNER: 7. LEFT: 10 (7 pairs and 3 on the left with no pair). FULL OUTER: 13 (7 pairs, 3 only from A, 3 only from B). CROSS would give 100.

How does a CROSS JOIN with WHERE o.user_id = u.id differ from an INNER JOIN on the same users and orders?

Answer

The result is the same, 3 rows, because WHERE filters out all non-matching combinations. The planner runs both the same way, but INNER JOIN with ON reads unambiguously and a missing condition stands out. For LEFT JOIN such a swap is impossible: WHERE would remove the rows without a pair.

In an interview. Junior: INNER JOIN returns only rows that have a pair in both tables. LEFT JOIN returns all rows of the left table and NULL where there is no pair. RIGHT does the same for the right one, FULL returns everything from both sides, CROSS gives all combinations, and SELF JOIN joins a table to itself. I find users without orders with a LEFT JOIN and WHERE o.id IS NULL. Middle/Senior: I choose the kind of JOIN by what to do with a row without a pair, and I estimate the row count beforehand. I write an anti-join with NOT EXISTS or LEFT JOIN … IS NULL and avoid NOT IN, because a NULL in the subquery gives an empty result. I keep conditions on the right table of a LEFT JOIN in ON and aggregate carefully after a one-to-many JOIN.

More articles in this chapter