· 8 min
SQL Window Functions: The 5 Patterns Every Data Engineer Uses Weekly
Deduplication, running totals, sessionization, period-over-period comparisons, and top-N per group, plus frames, pitfalls, and live playground queries.
- #sql
- #tutorials
All stacks · Languages & local engines
Query patterns, window functions, and modeling.
· 8 min
Deduplication, running totals, sessionization, period-over-period comparisons, and top-N per group, plus frames, pitfalls, and live playground queries.
· 12 min
DISTINCT, GROUP BY, ROW_NUMBER, QUALIFY, MERGE, and stream-side dedup compared on cost and semantics, plus how to pick the survivor row deterministically.
· 12 min
How WITH RECURSIVE executes, patterns for hierarchy traversal and BOM explosion, and the cycle guards that keep a bad edge from running forever.
· 12 min
Conditional aggregation, native PIVOT syntax, unpivoting wide tables, and how to handle dynamic categories without generating SQL at runtime.
· 12 min
What happens to a CTE in Postgres, Snowflake, and BigQuery, when it blocks predicate pushdown, and when referencing it twice costs you two scans.
· 12 min
How window frames actually evaluate, why RANGE and ROWS give different answers on ties, and six window patterns that delete self-joins from your models.
· 9 min
The join fan-out failure class: grain checks before and after every join, EXISTS for semi-joins, anti-joins done right, and the postmortem pattern.
· 9 min
Five SQL habits that scan more than they should: SELECT *, functions on filter columns, DISTINCT as a bug fix, OR-joins, and per-row UDF calls.
· 17 min
When sports feeds assign different IDs to one fixture, canonical match records, provider mappings, and guarded updates prevent duplicate data.
· 10 min
Use Snowflake AI functions in batch pipelines: filter before you classify, constrain the labels, summarize with AI_AGG, and keep the token bill visible.
· 9 min
Define metrics once in a Snowflake semantic view, query them with plain SQL or Cortex Analyst, and add materializations without forking definitions.
· 4 min
A pragmatic comparison: Databricks for Lakehouse/Spark/ML vs BigQuery for Serverless/SQL-native analytical workflows on GCP.
· 11 min
How incremental dbt models actually write: unique_key as merge grain, is_incremental filters, lookback windows, and late arrivals that silently drop rows.
· 10 min
How Dynamic Tables actually refresh: TARGET_LAG, the warehouse that pays for it, streams vs DT, and why I still keep dbt for gold.
SQL is the one skill in data engineering that has never stopped compounding. Every warehouse, lakehouse, and streaming engine converges on it, and the gap between adequate SQL and fluent SQL shows up directly in pipeline cost, correctness, and how long code review takes.
The articles here go past syntax: window functions used the way production models use them, join semantics that stop duplicate-row bugs, query anti-patterns that quietly multiply warehouse spend, and the modeling patterns — deduplication, sessionization, SCDs — that repeat in every stack.
SQL plus one orchestration tool and one cloud platform is a credible junior profile — and interviews weight SQL heavily because it is hard to fake. Strong window-function fluency alone puts you ahead of a surprising share of candidates.
Deduplication with row_number, top-N per group, running totals and moving averages, gaps-and-islands, and join questions designed to expose fan-out misunderstandings. Our interview section drills each with worked solutions.
The right side was not unique on the join key, so each left row matched multiple right rows — the classic fan-out. Check grain before joining: if the key is not unique, aggregate or dedupe first. This one bug pays for all the time you spend learning grain discipline.
In modern warehouses, generally no — most optimizers inline CTEs, so structure queries for readability. The exception is recursive CTEs and engines that materialize CTEs referenced multiple times; measure before contorting your SQL.
Use the free SQL playground on this site — SQLite in your browser with realistic orders, customers, and events tables — or DuckDB locally over public Parquet datasets. Both cover 95% of interview-relevant SQL.