SQL for analysts
The right grain, proven against a total
Your AI turns a business question into correct SQL the way an analytics engineer would: the grain and definitions pinned first, exploration queries, CTE-structured logic, safe joins and dates, window functions where they fit, and validation against a known number.
By Vander Labs. A Vander Labs Original, written by the Vander Labs team with AI assistance. Works with Claude, ChatGPT, Cursor and Claude Code.
What your AI learns
- Restates every question as a result grain before writing SQL
- Labels join cardinality and prevents fan-out double counting
- Validates the result against a total the business already trusts
How it works
- Restate the grain: One row per what, with which measures, for which range and filters.
- Explore the data: Row counts, keys, null rates, date ranges and status values, run before the real query.
- Build with CTEs: One step per CTE with its grain in a comment; no deep nesting.
- Join on the grain: Cardinality stated for every join, many sides aggregated first, left-join filters kept in the on clause.
- Dates and time zones: Business time zone before truncation, half-open ranges, a calendar series for empty periods.
- Window functions: row_number, lag and lead, running sums and dense_rank instead of self-joins.
- Aggregate carefully: count distinct where identity matters, conditional sums, nullif on every division, ratios of sums.
- Validate and document: Reconcile to a known total, check row counts, spot-check a group, add a header comment.
Limits and safety
- Flags assumed definitions as assumed and asks for confirmation of what counts as active, paid or signed up.
- Not for schema design or production application queries.
Topics: sql, analytics, postgresql, bigquery, window-functions