SQL Dojo Beta

Library

The patterns, pitfalls and keywords of SQL and analytics engineering, each with the exercises that practise it.

Pattern15

Pattern

A histogram in SQL

Count values per bin of a fixed width, keeping the empty bins.
Pattern

Allocation with a remainder

Spread an amount over rows by weight, rounded to the cent, with the remainder on one chosen row so the parts add up exactly.
Pattern

Anti-join

The rows of one table that have no match in another: customers who never ordered.
Pattern

Apply a change feed

Reduce a batch of changes to the last one per key, deletes included, then apply it with one MERGE.
Pattern

Counting business days

Build the working days once (no weekend, no holiday), then count them between two dates.
Pattern

Effective-dated lookup

Find the rate, price or version valid on the date of each fact, from a table that stores changes.
Pattern

Invariants as tests

A rule that must hold on every row, or between a model and its source, written as "the rows that break it".
Pattern

Merging overlapping periods

Merge periods that overlap or touch into continuous ones, with a running maximum of the end dates.
Pattern

Peak concurrency

How many things were active at the same time, at most: +1 at each start, −1 at each end, then a running total.
Pattern

Quarantine table

Bad rows go to a separate table with the rule they break, instead of being filtered out and forgotten.
Pattern

Running total

Add up a measure from the first row to the current one.
Pattern

Semi-join

The rows that have at least one match, each kept once.

Pitfall9

Pitfall

Convert, then truncate

Cutting a UTC time into days, hours or months gives UTC days: convert to the reader's time zone first.
Pitfall

Dirty data

A messy file raises no error: every query runs, and the answers are wrong. Check a new table before trusting it.
Pitfall

Join fan-out

Joining two one-to-many tables multiplies rows, and sums grow with them.
Pitfall

Not observable yet

A result that has not had time to happen is unknown, not zero: young cohorts and the current period look like a collapse.
Pitfall

Simpson's paradox

A group can be better in every segment and worse overall, when the segments weigh differently.

Keyword2

Keyword

LEFT JOIN

Keep every row of the left table, even when nothing matches on the right.
Keyword

MAX_BY and MIN_BY

The value of one column on the row where another column is largest (or smallest), in a plain GROUP BY.

Metric5

Metric

Average basket

Revenue divided by the number of orders, over a period.
Metric

Explaining a change

Split a metric's change into parts that add up to it: volume against value, mix against rate, then by segment.
Metric

Revenue

One definition for the whole coffee shop: quantity * unit_price of the order lines, cancelled orders excluded.
Metric

Sample ratio mismatch

Before reading an experiment, check that its groups have the sizes they were meant to have.

Modeling4

Modeling

Staging, intermediate, marts

Each definition lives in one layer: names and types in staging, shared logic in intermediate models, business entities in marts.