write SQL with AI

Write SQL with AI and verify it before it runs

The query that runs clean and returns the wrong number is the expensive one.

In short

To write SQL with AI, give it your real schema, then verify the query against a number you already know before you trust the result.

  • Without the schema it invents table and column names that look right.
  • Joins and grain are where silent errors live. A query can run perfectly and still double-count.
  • Run EXPLAIN before executing anything against production.
The short version

A clean run is not a correct answer

When you write SQL with AI, the dangerous failure is not a syntax error. Syntax errors announce themselves. The expensive one is a query that executes without complaint and returns a number that is quietly wrong. Usually a join fanned out and every row got counted twice. Two habits remove most of this. First, give the model your actual schema, since a model guessing at column names will produce something plausible and wrong. Second, validate against ground truth: pick one number you already know independently, such as last month total from a report you trust, and check the query reproduces it. If the grain and joins are right for that number, they are probably right for the rest. Then read the plan before running anything heavy. The upload-and-ask flow is covered in ChatGPT data analysis, and structured output prompting helps when you want results in a fixed shape. It is a daily tool in AI for data analytics.

The workflow

How to write SQL with AI

Schema in, query out, then verify against something you know.

  1. 1

    Paste the real schema

    Table definitions, key columns, and how the tables relate. Guessing is where invented column names come from.

  2. 2

    State the grain you want

    'One row per customer per month.' Grain is the single most common source of a silently wrong total.

  3. 3

    Ask for the query and its assumptions

    'List what you assumed about the joins.' The assumptions are where you will find the error.

  4. 4

    Check it against a number you know

    Reproduce one total you can confirm elsewhere. Match means the joins and grain are probably sound.

  5. 5

    Read the plan before you run it

    EXPLAIN shows sequential scans on huge tables and runaway row estimates before they cost you an hour.

  6. 6

    Run it read-only first

    Query against a replica or with a LIMIT. Never let a generated statement touch production data unreviewed.

Watch-outs

  • Never run a generated UPDATE or DELETE unreviewed. Read every statement that writes.
  • A missing join condition produces a cross join that still runs. Check row counts against expectation.
  • Asking the same model whether its own query is right rarely surfaces the error. Use a deterministic check.
FAQ

Common questions

How do I write SQL with AI reliably?

Paste your real schema, state the grain you want, and ask for the query plus its join assumptions. Then verify the output reproduces one number you already know from a trusted source, and read the execution plan before running anything heavy.

Why does AI get joins wrong?

Because a join needs to know the relationship between tables, and without a schema the model infers it from column names. The result is a query that runs and fans out rows, so totals double-count while everything looks fine.

How do I check a generated query is correct?

Test it against ground truth. Pick a figure you can confirm independently, such as a monthly total from a report you trust, and see whether the query reproduces it exactly. If the grain and joins are right there, the rest is usually sound.

Is it safe to run AI-generated SQL on production?

Not without review, and never for statements that write. Run against a replica or with a LIMIT first, read the execution plan, and treat any generated UPDATE or DELETE as something to read line by line before it executes.

See how you actually work with AI

The AI Skills Quiz scores your real habits, including whether you verify AI output against something you trust. Free, and it takes a couple of minutes.

Professionals from these companies build their AI skills with Candova

Study.comMoneyGeekHatchOsteriaGuild CraftLearn.orgFGAParallelVestaboardRieckenProsperityStudy.comMoneyGeekHatchOsteriaGuild CraftLearn.orgFGAParallelVestaboardRieckenProsperity

The future belongs to AI‑native companies.
Make sure yours is one.

We make your people AI-fluent so your company becomes AI-native, with Cando, a personal AI trainer for every employee. Start with Candova AI today.

Free to start · Productivity gain guarantee · Built for your real job

Future-proof your work with AI

On-demand · start today

Get started