skip to content

Join Types

The join constructs themselves — inner, outer, cross, self, USING/NATURAL, and LATERAL — and exactly which rows each one keeps, drops, or NULL-extends. Interviewers ask you to predict result-set contents and row counts for each construct, often on tiny sample tables.

part ofSQLoverview, primer and where to startread it →
on this pageshow

questions

page 2 of 2

Why would you CROSS JOIN a table to a one-row subquery such as (SELECT SUM(amount) AS total FROM sales)?

level: middleimportance: nice to knowfreq 30%

basics

~20 s

Pairing N rows with a one-row subquery yields N rows, attaching that subquery's value — a grand total, say — to every row, so each row can be compared against it without repeating the aggregate query in the select list.

open as a page

With FULL OUTER JOIN ... USING (product_id), what does the single product_id column hold for unmatched rows?

level: middleimportance: nice to knowfreq 22%

basics

~20 s

The merged column is the coalesce of both sides, so a row present on only one side shows that side's product_id rather than NULL. With an ON join you would have to write COALESCE(a.product_id, b.product_id) yourself.

open as a page

When would you standardise on LATERAL for per-row lookups across a multi-engine codebase?

level: principalimportance: nice to knowfreq 18%

basics

~20 s

Standardise on it when every target engine supports it or its APPLY spelling and the queries genuinely need per-row subqueries returning several rows or columns. Otherwise prefer formulations that are portable everywhere and keep the LATERAL variants isolated behind engine-specific code.

open as a page

showing 31–33 of 33