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

SQL › JOIN

What are RIGHT, FULL OUTER and CROSS JOIN?

JOIN kinds differ in whose rows without a pair are kept: RIGHT keeps the right table, FULL OUTER both. CROSS JOIN has no condition and returns all combinations. RIGHT JOIN is usually replaced by LEFT with the tables swapped. FULL OUTER suits reconciling two sets, and CROSS JOIN only small reference tables, because the result size is the product of the sizes.

SQL · JOIN

Reconciling with a provider statement: which operations exist only on our side and which only on theirs?

At the end of the day the platform's transactions must be reconciled with the payment provider's statement. A LEFT JOIN from our transactions finds what we have and the provider does not. But money that the provider accepted and we did not record is invisible to this query. This lesson is about the three remaining kinds of JOIN, RIGHT, FULL OUTER and CROSS, and which question each answers.

How it works and why. All kinds of JOIN differ in whose rows without a pair are kept. INNER keeps no one. LEFT keeps the left side, RIGHT the right one. FULL OUTER keeps both sides: a row without a pair from either side goes into the result, and the missing half is filled with NULL. CROSS JOIN stands apart: there is no condition, and every row of the left table is combined with every right one.

Two statements, three possible states

oursprovidere1e2e3e2e3e4e1 and e4 have no pair: kinds of JOINtreat them differently.
Figure 1. e2 and e3 match. e1 exists only on our side, e4 only on the provider side. FULL OUTER JOIN returns all four, LEFT JOIN will not show e4.

In practice RIGHT JOIN is almost never written: A RIGHT JOIN B gives the same as B LEFT JOIN A but with a different column order, and a result where the "main" table comes second is awkward to read. FULL OUTER JOIN is the tool for reconciling two sets when differences are possible on both sides. CROSS JOIN gives all combinations and is useful for generating test data.

Example: reconciliation in both directions. Our transactions: e1, e2, e3. In the provider statement: e2, e3, e4. A "what is missing" report with a LEFT JOIN finds only e1 (we have it, they do not). Transaction e4, accepted by the provider and not recorded on our side, goes unnoticed, although it is the worse case: money was debited and there is no order. The right query selects both sides:

-- reconciliation: what exists on one side only
SELECT o.ext_id AS our_id, p.ext_id AS provider_id
FROM transactions o
FULL OUTER JOIN provider_report p ON p.ext_id = o.ext_id
WHERE o.ext_id IS NULL OR p.ext_id IS NULL;

FULL OUTER JOIN and the mismatch filter

after FULL OUTER JOIN
our_idprovider_id
e1NULL
e2e2 filtered
e3e3 filtered
NULLe4
after WHERE IS NULL
our_idprovider_id
e1NULL
NULLe4
Figure 2. The filter keeps only mismatches. The matches e2 and e3 are filtered out; the report shows e1 (missing at the provider) and e4 (missing on our side).

The result contains two categories of defects with different consequences: a row with NULL on the right is our operation that the provider does not see, a row with NULL on the left is an accepted payment with no record. They are told apart by the column that holds NULL. If the keys differ in format (case, spaces, a prefix), the pair is not found and the reconciliation shows mismatches where there are none: normalise the keys first.

Limits and alternatives. MySQL has no FULL OUTER JOIN. It is built from two parts: a LEFT JOIN and a second query from the right side, only rows without a pair, through UNION ALL:

-- MySQL has no FULL JOIN: build it from two parts
SELECT o.ext_id AS our_id, p.ext_id AS provider_id
FROM transactions o
LEFT JOIN provider_report p ON p.ext_id = o.ext_id
WHERE p.ext_id IS NULL
UNION ALL
SELECT o.ext_id, p.ext_id
FROM provider_report p
LEFT JOIN transactions o ON o.ext_id = p.ext_id
WHERE o.ext_id IS NULL;

It is UNION ALL here, not UNION: the parts no longer overlap, and UNION also removes duplicates, which can hide real repeats. In PostgreSQL FULL JOIN is supported only for conditions that can be run with a merge or a hash, in practice mainly equality. For reconciling sets there are other tools too: EXCEPT gives the difference in one direction but does not show values from both sides side by side. CROSS JOIN needs care:

-- a test data matrix: roles × browsers
SELECT r.name AS role, b.name AS browser
FROM roles r
CROSS JOIN browsers b;
-- 4 roles × 3 browsers = 12 rows

The result size equals the product: 4 × 3 gives 12 rows, but for 10,000 × 10,000 it is a hundred million. So CROSS JOIN is applied to small reference tables (roles, browsers, languages), not to data tables.

Common mistakes.

  • Reconciling only with a LEFT JOIN: mismatches on the other side stay unnoticed.
  • Looking for FULL JOIN in MySQL: it does not exist, a union of two parts is needed.
  • Writing UNION instead of UNION ALL in the MySQL variant: real duplicates disappear.
  • Reconciling on keys of different formats: mass "mismatches" because of a space or case.
  • CROSS JOIN on large tables: the product of sizes exhausts memory and time.
  • Writing RIGHT JOIN where it is more readable to swap the tables and take LEFT.

Check yourself

We have 1000 transactions, the provider statement has 990, and 985 match. How many rows does a FULL OUTER JOIN on the key return and how many of them are mismatches?

Answer

1000 + 990 − 985 = 1005 rows in total. There are 20 mismatches: 15 exist only on our side (1000 − 985) and 5 only on the provider side (990 − 985). This assumes the key is unique on both sides.

In a reconciliation the key on the provider side is NULL in a few rows. What happens to those rows in a FULL OUTER JOIN on key equality?

Answer

NULL equals nothing, so these rows find no pair and land in the result as right-only rows with NULL on the left. In the mismatch report they will look like "missing on our side", although the real cause is a defect in the statement, so the column with NULL must be checked separately.

In an interview. Junior: RIGHT JOIN is the mirror of LEFT, and it is usually replaced by LEFT with the tables swapped. FULL OUTER JOIN returns everything from both sides and suits reconciling two sets, and CROSS JOIN gives all combinations. Middle/Senior: I pick the kind by whose rows without a pair must be kept. For reconciliation I take FULL OUTER JOIN with an IS NULL filter on both sides, and in MySQL I build it from two LEFT JOINs through UNION ALL. I normalise the keys, because otherwise pairs are not found. I leave CROSS JOIN to small reference tables.

More articles in this chapter