Topic 1 of 5 · 3 min read
Describing the question and the schema
Goal: Write a text-to-SQL request that includes the relevant schema, the business definitions and the time window.
A text-to-SQL request works when it carries four things the model cannot see: the tables and columns, your business definitions, the exact time window, and the shape of the answer. The model has never seen your database. If you leave these out, it guesses names and meanings, and a guess still produces a query that runs.
Check yourself · 1 of 3
You ask a chat assistant: "How many active customers did we have last month?" Why is this request risky even though the model writes valid SQL?
Why it matters
Chat assistants such as ChatGPT, Claude, Gemini and Copilot write fluent SQL. Fluent is not the same as correct. "How many active customers did we have last month?" has at least four unstated choices: what active means, which table holds customers, which dates count as last month, and whose midnight starts the day. The model picks one silently, and the number looks as trustworthy as a right one.
How to do it
- Paste the schema. Give table names, the columns you need with their types, and how tables link (for example
orders.customer_idpoints tocustomers.id). A few lines are enough. Do not paste real rows if they hold personal data; sample values you invent work as well. - Define the business terms. Write "active customer means at least one paid order in the last 90 days, refunds excluded." Say which column marks a paid order, such as
status = 'paid'. - Fix the time window and zone. Write "1 to 31 October 2026, in Europe/Berlin time, end date included." Say whether you want calendar months or the last 30 days.
- Say what the answer looks like. One number, or one row per country sorted high to low? Name the SQL dialect too: PostgreSQL, MySQL, SQL Server and BigQuery differ in date functions.
- Ask it to state its assumptions before the query, so a wrong guess shows up in plain words.
Before and after
Before: "Show me our top customers this quarter."
After: "PostgreSQL. Tables: customers(id, name, country), orders(id, customer_id, total_eur, status, created_at). Top customers means the highest sum of total_eur over orders with status = 'paid'. Quarter means 1 Oct to 31 Dec 2026, in UTC. Return the top 10 as name, country, revenue. List your assumptions first."
The second request leaves the model almost no room to guess, and its assumptions list lets you correct a misreading before any query runs.
What goes wrong
- Silent definitions. "Revenue" might mean gross, net of refunds, or net of tax. If you do not say, the model chooses.
- Time zone drift. A timestamp stored in UTC, grouped by day, puts evening sales in the next day for a shop in another zone.
- Too much schema. Pasting two hundred tables hides the three that matter. Give only the relevant ones, and say which of two similar tables to use.
Examples
Same question, vague and specified
Vague: "How many users churned last month?"
Specified: "PostgreSQL. subscriptions(id, user_id, started_at, cancelled_at). A user churned in a month if cancelled_at falls in that month and they have no other open subscription. Month: November 2026, Europe/London. Return one number. List assumptions first."
The vague version hides three decisions: what churn means, which month, and what to do with users who have two subscriptions.
Asking for assumptions first
Add one line to any request: "Before the SQL, list every assumption you made about meanings, dates and tables." If the list says "assumed amount is in dollars" and your column is in cents, you catch it in ten seconds instead of in a board meeting.
Key points
- The model cannot see your database, so every table, column and relationship it needs must be in the request.
- Business terms such as active customer or revenue need a written definition tied to specific columns and values.
- Give the time window as exact dates with a time zone and say whether the end date is included.
- Name the SQL dialect and the output shape, and ask the model to list its assumptions before writing the query.
- Send only the relevant part of the schema, and invented sample values rather than real personal data.
Common mistakes
- Asking in business words only and trusting the model to know what your company means by active, churned or revenue.
- Giving a date like "last month" with no zone and no end rule, so the edges of the window shift.
- Pasting the whole schema, or real customer rows, instead of the few relevant tables and invented sample values.
- Leaving out the dialect, then getting date functions that only exist in another database.
Check yourself · 2 of 3
Your orders are stored with UTC timestamps, but your shop runs in Tokyo. You want daily sales. What should the request say?
Check yourself · 3 of 3
Which addition to this request helps most? "Write SQL for revenue by country. Tables: orders, customers."
Practice items, not counted towards your mastery.
Start here
- SQLBolt: interactive SQL lessons (opens in a new tab)CourseFree
Short free exercises on SELECT, WHERE, JOIN and aggregates: enough to read what an assistant wrote.
- BIRD text-to-SQL benchmark (opens in a new tab)PaperFree
A benchmark built on large, messy real databases, with a public leaderboard that shows how far tools still are from perfect.
- Spider 2.0: enterprise text-to-SQL workflows (opens in a new tab)PaperFree
A benchmark of enterprise-sized schemas and multi-step tasks, showing why real warehouses are harder than demos.