When you need it
You need a join whenever the columns you want live in more than one table: customer names next to orders, plan prices next to subscriptions. Words that signal it: "with their", "along with", "for each customer", "including those with none".
The second half of the skill is knowing what the join does to the row count. Interviewers care more about that than about syntax.
The shape of the query
select a.col, b.col
from a
join b on b.a_id = a.id -- keeps only matching rows
left join c on c.a_id = a.id -- keeps every row of a, NULLs when no match
where <filter on a>;Step by step:
- Pick the table that sets the grain. If the answer is one row per customer, start from
customers. - Join the other tables on their keys. Name the key relationship in your head: one customer to many orders.
- Use
INNER JOINwhen a missing match means the row should disappear. UseLEFT JOINwhen the left row must survive. - Check the row count before and after.
select c.country,
count(distinct c.customer_id) as customers,
count(o.order_id) as orders
from customers c
left join orders o
on o.customer_id = c.customer_id
and o.status = 'completed'
group by c.country
order by c.country;Every customer is kept, even those with no completed orders. The status test sits in the ON clause on purpose. Moved to WHERE, it would delete the unmatched rows and turn the left join back into an inner join.
A join across three tables, summing units per product name:
select p.product_name, sum(oi.quantity) as units
from order_items oi
join orders o on o.order_id = oi.order_id
join products p on p.product_id = oi.product_id
where o.status = 'completed'
group by p.product_name
order by units desc, p.product_name
limit 5;Traps interviewers look for
- Fan-out. Joining a one-to-many table repeats the parent row. Summing a parent column afterwards inflates it. Fix it by aggregating the many side first in a CTE, then joining one row per key.
- Filter in
WHEREafter aLEFT JOIN.where o.status = 'completed'throws away customers with no order, because theirstatusis NULL. Put the condition inON, or useIS NULL OR. - Joining on a non-unique key. If both sides repeat the key, you get a many-to-many join and rows multiply on both sides. Check the key with
count(*)againstcount(distinct key). - NULL keys never match.
NULL = NULLis not true, so rows with a NULL join key drop out of an inner join. Employees with no department disappear unless you use a left join. - Ambiguous columns and wrong aliases. Qualify every column when two tables share a name. Reusing an alias for two tables is a classic copy-paste bug.
- Joining the same fact twice. Joining orders to both returns and items makes a cross product per order. Handle each branch separately and combine at the same grain.
Variations
- Full outer join. Keep unmatched rows from both sides, useful for reconciling two lists.
- Cross join. Every pairing of two small tables, for building a grid such as every category by every month.
- Join on a range. Matching a date to the plan active on that day uses
betweenor two inequalities instead of equality. - Join to an aggregated subquery. Compute totals per key in a derived table, then attach them.
- Semi join. If you only want to test existence,
EXISTSis cleaner than a join and cannot multiply rows.
In other warehouses
- Snowflake and BigQuery support
USING (col), and so do PostgreSQL and MySQL. SQL Server does not, so write the fullON. - MySQL has no
FULL OUTER JOIN. Emulate it with a left joinUNIONa right join. - Databricks has
LEFT SEMI JOINandLEFT ANTI JOIN. In the other warehouses useEXISTSandNOT EXISTS.