What We'll Cover
Fast, and Sometimes Wrong
An LLM is a genuinely good SQL drafter. Give it a question and it will hand you a query in seconds, with joins wired up and columns named sensibly. That speed is real, and you should use it. The catch is that the same tool produces queries that look correct, read fluently, and return a wrong number with total confidence.
The failures are rarely syntax errors, because syntax errors are easy to catch: the query simply does not run. The dangerous ones run fine and give you a plausible answer. The model joins on the wrong key, double counts revenue through a one-to-many join, silently drops rows where a value is NULL, or answers at the wrong grain, giving you one row per order line when you asked for one row per customer. None of that raises an error. It raises a number, and you paste it into a report.
Why This Matters
The person who ran the query owns the number. If a dashboard says revenue is double what finance recorded, "the AI wrote it" is not an answer anyone accepts. You stay responsible for correctness, so the skill worth building is not prompting faster, it is verifying what comes back.
How to Prompt So the SQL Is Correct
Most wrong queries come from a vague prompt. The model cannot see your database. It guesses table names, column names, and relationships, and a confident guess that happens to be wrong is worse than a question. You cut the guessing by giving it four things: the schema, the exact question with its grain, the dialect, and a small example of the output you expect.
Here is a vague prompt of the kind that produces a good-looking, wrong query:
The model has to invent every table name, decide what "revenue" means, guess whether you want a date or a month label, and pick a dialect. Compare that with a structured prompt that leaves almost nothing to invent:
The structured prompt states the grain of every table, so the model knows order_items is
one-to-many against orders. That single fact is what stops the most common revenue bug,
which we will see next. Naming the dialect stops it from reaching for a function your database does
not have. The example row tells it the shape you want back, which keeps it from returning one row per
line item when you asked for one per customer per month.
The Four Things
Schema (tables, columns, keys, grain), the exact question and the grain of the answer, the SQL dialect, and one example output row. Give all four and the model has little left to guess.
A Worked Example: Monthly Revenue Per Customer
Let us stay with that schema. We have customers, their orders, and the line items on each order.
Revenue lives at the line level, one line per product on an order, so the total for an order is the
sum of quantity * unit_price across its lines.
Ask an LLM for "monthly revenue per customer" without spelling out the grain, and it will very often produce something like this. Read it slowly, because it looks right.
There are two problems. The small one is that order_total was never in our schema, so this
query assumes a column that may not exist. The large one is the join, which we will pull apart now.
The Fan-Out Join That Doubles Your Revenue
When you join orders to order_items, one order row is repeated once for every
line item on that order. An order with three line items becomes three rows. This is a fan-out join,
and it is completely normal. The bug is what you do after the fan-out.
If an order-level value like order_total exists and you SUM it after the
fan-out, you add that same order total once per line item. A 100 dollar order with three lines gets
counted as 300 dollars. The number is not a little off, it is multiplied by the number of lines, and
it changes from order to order, so it does not even look like a clean doubling you might spot.
Watch Out!
Any time you join a table to a more detailed child table and then aggregate a column from the parent, you risk multiplying that value by the number of child rows. This is the single most common way an LLM query returns inflated totals.
There are two clean fixes. The first computes revenue from the line items directly, which is where revenue actually lives, so there is no parent column to double:
Because we sum quantity * unit_price from order_items, the fan-out is exactly
what we want: each line contributes its own revenue once. The second fix aggregates the line items
first, so each order carries a single revenue figure before it ever meets the customer table:
Here order_revenue collapses the line items to one row per order, so joining it to
orders is one-to-one and there is no fan-out at all. Both queries give the same answer.
The pre-aggregated version is often easier to reason about, and it is the pattern you want when the
same order gets joined to several child tables at once.
How to Check the Output
You do not have to trust either query. You check it, and the checks take a minute. The point is to confirm the grain is what you asked for and the total matches something you already know.
First, confirm the grain. You asked for one row per customer per month, so that combination should never appear twice. If this returns any rows, the grain is wrong:
Second, compare the grand total against a figure computed a completely different way, straight from the line items with no customer or month grouping. If your report sums to the same number, the joins did not add or drop revenue:
Run the wrong query against that total and it will come out visibly larger, because the fan-out inflated it. Run either correct query and it will match to the cent. That one comparison catches the double-counting bug on its own.
Third, spot-check a single customer by hand. Pick one customer id, list their raw line items for a month, add them up in your head or a calculator, and confirm the report shows the same figure:
Add up the line_revenue column and check it against the March 2025 row for customer 42 in
your report. If a customer you know well shows a number you can reproduce by hand, your confidence in
the whole result goes up fast. It also flags NULL trouble: if a customer with orders is missing from
the report, or a completed order is missing from a customer, a join or a filter is dropping rows.
A Verification Checklist
Keep these habits close whenever an LLM hands you a query. They take a couple of minutes and they are the difference between shipping a number and shipping a guess.
Before You Trust the Query
- Read the joins and name the grain of each table out loud. Any parent column summed after a join to a child table is a fan-out risk.
- Confirm every column the query references actually exists in your schema.
- Run the grain check: the key you grouped by should never repeat.
- Compare the grand total against the same figure computed a different way.
- Spot-check one row you can reproduce by hand.
- Check the row count. A total that is far too high often means duplicated rows; far too low often means an INNER JOIN or a filter dropped NULLs.
- Look for anything compared to NULL with
=or!=instead ofIS NULL, which silently removes rows.
The Skill Is Verification, Not Prompting
An LLM will draft SQL faster than you can, and that is fine. The value you add is not the prompt, it is the judgment to check the result before anyone acts on it. An analyst who verifies is worth more in the AI era, not less, because the tools produce more output that needs a trained pair of eyes. Learn the checks, run them every time, and the speed becomes a real advantage instead of a faster way to be wrong.