Back to notes

Make SQL easier to review

Write explicit time ranges and grouping rules so readers can quickly check what a query returns.

Make the date range explicit

Use an inclusive start and an exclusive end. Put the grouping grain in the column names or a nearby comment so reviewers can tell what one row represents.

SELECT event_day, COUNT(*) AS event_count
FROM sample_events
WHERE event_day >= '2026-11-01'
  AND event_day < '2026-11-08'
GROUP BY event_day
ORDER BY event_day;

Check the grouping grain

Compare raw records with the aggregation over a short window. Before joining a dimension table, check whether its join keys are unique so duplicate matches do not inflate the counts.