The query that looked fine and wasn’t
A few months ago I asked an LLM to write me a query for monthly churn. It came back fast, formatted nicely, with a comment explaining what it was doing. I almost ran it against the production database without a second look, because it read like something I would have written myself.
I didn’t run it blind, and that’s the only reason I caught the problem. The query counted a customer as “churned” the moment their subscription status flipped to canceled, with no window for reactivations. In my business, a meaningful chunk of canceled customers come back within a few weeks because of how our downgrade flow works. The LLM had no way to know that. It wrote syntactically correct SQL that answered a question I hadn’t actually asked.
That’s the whole story of this article, really. LLMs are useful for SQL. They are not a substitute for knowing your own data.
What an LLM is actually good at here
For someone running a business alone, without a data team to lean on, an LLM is genuinely useful for a few things:
- Turning a question in plain English into a first-draft query, so you’re not starting from a blank editor.
- Remembering SQL syntax you don’t use often: window functions, date truncation, the right way to do a self-join.
- Explaining someone else’s query back to you in plain language, line by line.
- Suggesting a few ways to structure the same calculation so you can pick the one that matches how you think about the problem.
All of that is real time saved, especially on the mechanical parts of SQL that have nothing to do with your business and everything to do with syntax you’d otherwise be looking up. If you’re comfortable reading SQL but slow at writing it, this is where an LLM earns its keep.
What it can’t know about your business
Here’s the part that matters more. An LLM doesn’t know your schema unless you tell it, and even when you tell it, it doesn’t know the parts of your data that live in your head and nowhere else. Things like:
- Whether “active” means the subscription status column says active, or whether it also requires a payment in the last 30 days because your status field lags behind reality.
- Whether a customer row can appear twice because of a migration you did two years ago and never fully cleaned up.
- Whether refunds get logged as negative rows in the same table as charges, or in a separate table you have to remember to join.
- What counts as the “first” event in a funnel when a user can restart the funnel after abandoning it.
None of this is written down anywhere an LLM could read it, unless you’ve documented it somewhere and pasted that documentation into the conversation. Most solo operators haven’t, because writing internal documentation for an audience of one doesn’t feel worth the time, until it is.
This is the core issue with LLM SQL for analytics: the model can write correct SQL for the wrong definition of your metric, and there’s nothing about the output that signals which one happened. A wrong join gives you an error. A wrong assumption about what “churned” means gives you a clean, confident, wrong number.
The process I actually use
I still use LLMs to draft SQL constantly, for churn, for cohort retention, for funnel drop-off, for attribution pulls. The difference is what happens after the draft shows up.
I write the definition first, before I ask for the query. Not “churn rate,” but “a customer counts as churned if their subscription status is canceled and stays canceled for at least 14 days, measured at the end of each month.” If I can’t state the definition in a sentence, I’m not ready to ask for a query yet, and I’m definitely not ready to trust one that gets handed to me.
I paste in the actual schema, not a description of it. Column names, types, and a sample row or two if the table is unusual. “We have a customers table” gets you a generic guess. CREATE TABLE customers (id, plan_id, status, canceled_at, reactivated_at) gets you a query built on what’s actually there.
I read every join before I run anything. Specifically, I check what happens to rows that don’t match. An inner join on a customers-to-charges relationship will silently drop every customer who hasn’t been charged yet, which quietly deletes your newest signups from an MRR calculation. This is the single most common way an LLM-drafted query goes wrong, and it’s also the easiest thing to check once you know to look for it.
I check the grain. Before trusting a total, I ask: what does one row in this result represent? One customer? One subscription? One customer-month? If a customer can have two active subscriptions at once, and the query wasn’t built with that in mind, your customer count and your subscription count will quietly diverge and you won’t notice until the totals stop reconciling with something else.
I run it against a number I already trust. If I have last month’s MRR from my billing provider’s dashboard, I run the new query for that same month and see if it lands close. Not identical, since definitions differ slightly across systems, but close enough that a big gap tells me something’s wrong before I build a report on top of it.
A second example: cohort retention
Cohort tables are a good stress test for this, because they’re easy to get subtly wrong. Say you want week-by-week retention for customers who signed up in a given week. An LLM will often draft something that groups by signup week and counts active customers in each following week, which sounds right and usually runs without errors.
The question worth asking before you trust it: what happens to a customer who upgrades plans partway through, and does that change their customer ID or account ID in your system? If it does, and the query is joining on the wrong ID, that customer silently drops out of their original cohort and your retention curve looks worse than it actually is. This isn’t a hypothetical the model would think to flag. It’s a fact about your specific database that only you know to check.
When to slow down further
Some queries deserve more scrutiny than others. I’m more careful when:
- The number is going into a report I’ll act on, like deciding whether to keep or kill a channel based on attribution.
- The metric involves money, like CLV or MRR, where a small definitional error compounds across every customer.
- The query touches a table I know has messy history, like one that survived a platform migration.
- I’m going to run this same query on a schedule, because a mistake that runs once is a mistake, but a mistake that runs every week is a habit.
For a one-off exploratory query where I’m just poking around to get a feel for the data, I’m looser. I still skim the SQL, but I’m not reconciling it against a second source.
A simple checklist before you ship a query
- Can you state the metric’s definition in one sentence, before you asked for the SQL?
- Did you give the model the real schema, not a paraphrase of it?
- Did you check every join for what happens to unmatched rows?
- Do you know what one row in the output represents?
- Does the result land close to a number you already trust from another source?
If you can answer all five, you’re not trusting the LLM. You’re using it as a fast typist for a query you understand and could defend if someone asked you to.
The point of all this
None of this means LLM-drafted SQL is unreliable in some special way SQL you write yourself isn’t. Hand-written queries have the same failure modes: wrong joins, wrong assumptions about grain, definitions that drift over time. The difference is that when you write the query yourself, the process of writing it forces you to think through those questions. When a model writes it for you, that thinking has to happen somewhere else, on purpose, or it doesn’t happen at all.
The tool is fine. It’s fast and it’s genuinely useful for a one-person operation without a data team to check its work. The judgment about what your data actually means still has to come from you, because you’re the only one who was in the room when the schema got messy.
If you want more on how to sanity-check the numbers your business runs on, from churn and MRR to cohorts and attribution, that’s what Data Research Analysis Collection is here for.
Get new guides and videos first — join the Telegram channel.