DEPLOYEDAI-era interview training
RankBootstrapping
XP 0 / 250
0
0%
Ready
Data Analyst & Analytics Interview · SQL for Analysts · Lesson 1 of 3

Joins & aggregation

7 min

SQL is the single most-tested analyst skill. Two mechanics carry most of it: joins (combine rows across tables) and GROUP BY aggregation (collapse rows into summaries). Get these reflexive and half the screen is won.

Join types

  • INNER JOIN: only rows matching in both tables. The default and most common.
  • LEFT JOIN: every row from the left table, plus matches from the right (NULL where none). Use to keep all customers even those with no orders.
  • RIGHT / FULL OUTER: keep unmatched rows from the right / both sides. Rare but know they exist.
  • CROSS JOIN: every combination (Cartesian product) — usually an accident, occasionally deliberate for date spines.
-- Revenue per customer, including those who never ordered
SELECT c.customer_id,
       c.name,
       COUNT(o.order_id)        AS num_orders,
       COALESCE(SUM(o.amount),0) AS revenue
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
GROUP BY c.customer_id, c.name
ORDER BY revenue DESC;
COUNT(*) vs COUNT(col)COUNT(*) counts rows; COUNT(col) counts non-NULL values; COUNT(DISTINCT col) counts unique values. After a LEFT JOIN, COUNT(*) counts the join output (1 for no-match rows) while COUNT(o.order_id) correctly returns 0 — a classic trap.
Filter placement mattersA condition on the right table in the WHERE clause silently turns a LEFT JOIN into an INNER JOIN (it drops the NULL rows). Put right-table filters in the ON clause to preserve unmatched rows.

◆ Lock it in

  • INNER keeps matches only; LEFT keeps all left rows with NULLs for no-match.
  • GROUP BY + aggregates (SUM/COUNT/AVG) collapse rows into per-group summaries.
  • Watch COUNT semantics and right-table filters that quietly become inner joins.
Feynman drill — say it out loudExplain why a WHERE filter on the right table can turn a LEFT JOIN into an INNER JOIN, and where to put that filter instead.
Step 1 rate your confidence · Step 2 pick your answer
You LEFT JOIN customers to orders and want customers with zero orders to show a count of 0. Which expression is correct?
Step 1 — how sure are you?