data-write-query
When to use
Use this when someone describes what they want in plain English — "top 10 customers by revenue this quarter, excluding refunds" — and needs runnable SQL back. Intent in, correct dialect-specific query out.
Not for: debugging or speeding up an existing query (use data-sql-queries), deciding whether a result is significant (use data-statistical-analysis), or capturing a warehouse's metric definitions (use data-context-extractor).
Method
- Extract the spec: metric, output grain, filters, grouping, sort, limit, time window. Restate it so a misread is caught before running anything.
- Map to schema. Identify tables and columns for each piece; confirm keys and each table's grain. Decision point: if the schema is unknown, state assumed table/column names so they are easy to correct.
- Resolve ambiguity with explicit defaults. "This quarter" → calendar or fiscal? "Revenue" → gross or net of refunds? Pick the common-sense default, label it, and encode it visibly in the SQL.
- Write for the target dialect (BigQuery, Snowflake, Redshift, Postgres, DuckDB). One CTE per logical step; filter before aggregating; guard against join fan-out on non-unique keys.
- Optimize while writing: prune partitions, avoid
SELECT *, use window functions over self-joins, pre-aggregate before joining wide tables.
- Predict the output shape — columns and a sample row — so the requester knows what to expect.
Example
Request: "top 10 customers by net revenue this quarter." BigQuery:
-- ASSUMPTION: calendar quarter; net = gross - refunds
WITH sales AS (
SELECT customer_id, SUM(amount) - SUM(refund_amount) AS net_rev
FROM `proj.core.orders`
WHERE order_date >= DATE_TRUNC(CURRENT_DATE(), QUARTER)
GROUP BY customer_id
)
SELECT customer_id, net_rev
FROM sales
ORDER BY net_rev DESC
LIMIT 10;
Pitfalls
- Silently choosing gross vs net revenue — a wrong definition returns a plausible but wrong number.
- Joining on a non-unique key and inflating SUMs via fan-out.
- Applying a WHERE filter to a LEFT-joined table's columns, quietly turning it into an inner join.
- Porting date/window syntax between dialects without adjusting it.
Output format
Interpreted need: <spec in 1-2 lines>
SQL: <final query, dialect labeled, commented>
Assumptions: <every default: definitions, calendar, table/column names>
Expected output: <columns + one illustrative row>
Adjustments: <one-line swaps: fiscal calendar, gross revenue>
Make assumptions impossible to miss.
1---2name: data-write-query3version: 1.0.04description: Turn a natural-language data need into optimized, dialect-specific SQL you can run immediately, with every assumption made visible.5author: matrixx00706tags: [sql, nl2sql, query, warehouse, optimization, text-to-sql]7capabilities: []8metadata:9 source: https://skillmd.com/skills/matrixx0070/data-write-query10---1112# data-write-query1314## When to use1516Use this when someone describes what they want in plain English — "top 10 customers by revenue this quarter, excluding refunds" — and needs runnable SQL back. Intent in, correct dialect-specific query out.1718**Not for:** debugging or speeding up an existing query (use data-sql-queries), deciding whether a result is significant (use data-statistical-analysis), or capturing a warehouse's metric definitions (use data-context-extractor).1920## Method21221. **Extract the spec:** metric, output grain, filters, grouping, sort, limit, time window. Restate it so a misread is caught before running anything.232. **Map to schema.** Identify tables and columns for each piece; confirm keys and each table's grain. Decision point: if the schema is unknown, state assumed table/column names so they are easy to correct.243. **Resolve ambiguity with explicit defaults.** "This quarter" → calendar or fiscal? "Revenue" → gross or net of refunds? Pick the common-sense default, label it, and encode it visibly in the SQL.254. **Write for the target dialect** (BigQuery, Snowflake, Redshift, Postgres, DuckDB). One CTE per logical step; filter before aggregating; guard against join fan-out on non-unique keys.265. **Optimize while writing:** prune partitions, avoid `SELECT *`, use window functions over self-joins, pre-aggregate before joining wide tables.276. **Predict the output shape** — columns and a sample row — so the requester knows what to expect.2829## Example3031Request: "top 10 customers by net revenue this quarter." BigQuery:3233```sql34-- ASSUMPTION: calendar quarter; net = gross - refunds35WITH sales AS (36 SELECT customer_id, SUM(amount) - SUM(refund_amount) AS net_rev37 FROM `proj.core.orders`38 WHERE order_date >= DATE_TRUNC(CURRENT_DATE(), QUARTER)39 GROUP BY customer_id40)41SELECT customer_id, net_rev42FROM sales43ORDER BY net_rev DESC44LIMIT 10;45```4647## Pitfalls4849- Silently choosing gross vs net revenue — a wrong definition returns a plausible but wrong number.50- Joining on a non-unique key and inflating SUMs via fan-out.51- Applying a WHERE filter to a LEFT-joined table's columns, quietly turning it into an inner join.52- Porting date/window syntax between dialects without adjusting it.5354## Output format5556```57Interpreted need: <spec in 1-2 lines>58SQL: <final query, dialect labeled, commented>59Assumptions: <every default: definitions, calendar, table/column names>60Expected output: <columns + one illustrative row>61Adjustments: <one-line swaps: fiscal calendar, gross revenue>62```6364Make assumptions impossible to miss.