SQL queries written and debugged from a described schema
Hand-writing multi-join queries, window functions, and CTEs — then hunting the one bug that makes the totals wrong — eats hours of an analyst's week. AI writes correct SQL from a schema described in text, no live database connection required, and it is one of the most-cited everyday wins. The trick is that it must work only from the tables and columns you actually give it.
You are a senior data analyst and expert in {{dialect}} SQL. Write a query, or fix the one I provide. Database schema (tables, columns, and types I am giving you): {{schema}} What I need the query to return: {{question}} Rules: - Use ONLY the tables and columns in the schema above. Never invent a column, table, or join key. If the request needs something not in the schema, stop and list what is missing as "[NEED: describe the column or table]" instead of guessing. - State your assumptions about grain, filters, and business logic in a short "Assumptions" list before the query — I will correct them. - Comment each non-obvious CTE or window function in one line. - Handle the obvious edge cases: NULLs in join or filter columns, duplicate rows changing counts, and integer division. Say which you handled. - After the query, give me a short validation query I can run to sanity-check the result against a known control total or row count. - If I pasted a query to debug, first tell me in one sentence what was actually wrong before showing the fix.
Fill in your details and the prompt updates live — then copy.
Assumptions: revenue = SUM(amount) on non-cancelled orders; a "paying customer" is any customer with at least one such order in the month; month bucketed by order_ts in UTC. WITH paid AS ( SELECT customer_id, amount, DATE_TRUNC(order_ts, MONTH) AS mth FROM orders WHERE status != 'cancelled' -- exclude cancellations ) SELECT mth, SUM(amount) AS revenue, COUNT(DISTINCT customer_id) AS paying_customers FROM paid WHERE mth BETWEEN '2025-01-01' AND '2025-12-01' GROUP BY mth ORDER BY mth; Validation: SUM(revenue) here should equal SELECT SUM(amount) FROM orders WHERE status != 'cancelled' AND EXTRACT(YEAR FROM order_ts)=2025.
The full workflow
- Paste the real schema (or a redacted copy of it), never live rows of customer data
- Read the Assumptions list and correct any business logic the model got wrong
- Run the validation query and tie the result to a known control total before trusting it
- Resolve every [NEED] flag with the actual schema, not a guess, before shipping the query
Watch out for
Do not paste real customer records, PII, or confidential values to get a query — a described schema and dummy values work just as well. Free and Plus-tier chatbots may train on your input unless you disable it, and exposing personal data can breach GDPR or CCPA; use an approved, training-disabled instance.
A query that runs is not a query that is right. The model can silently pick the wrong join grain or miss a filter, inflating every number downstream — validate against a control total every time.
Watch integer division and NULL handling: these produce confident, wrong-looking-correct results rather than errors.
Where this comes from
Every use case on this site is grounded in real reports from working data analysts — not invented by us.