AI Data & Analysis How-to Intermediate Updated

Write SQL with AI against a schema it has never seen

How much schema to supply, and the checks that stop a plausible query returning wrong numbers.

2 min read 23 min to complete 4 steps Last updated 16 Jul 2026

Before you start

  • Read access to a database
  • Enough SQL to read a query

What you will be able to do

  • Supply the schema context that actually changes output quality
  • Catch the wrong-join and missing-filter failures before acting
  • Validate a result against something you already know

The failure here is specific and dangerous: not an error, but a query that executes cleanly and answers a slightly different question than the one you asked.

Wrong joins and unstated filters do not announce themselves. They just produce a number that looks fine in a report.

Paste the real DDL, not a description

6 min

Column names, types, keys and a few sample rows.

Give it the actual CREATE TABLE statements for the relevant tables, plus three or four sample rows.

Sample rows carry information the schema does not: that status is stored as an integer, that dates are strings in one table, that a nullable column is null 90% of the time. Each of those changes the correct query.

Say the things the schema cannot

6 min

Soft deletes, test rows, and which of the four date columns you mean.

Every real database has conventions that no amount of DDL reveals: deleted_at must be null, internal accounts are excluded, "active" means one of three status values, and the date you care about is completed_at rather than created_at.

These are exactly the omissions that produce a confidently wrong number, because the query is correct for the question the model thought you asked.

Watch out for
  • Forgetting the soft-delete filter. The query works, the number is inflated, and nothing looks broken.

Read the joins and the WHERE clause first

6 min

This is where wrong answers live. The SELECT rarely matters.

Check each join: is it on the right keys, and should it be a LEFT rather than an INNER? An inner join silently drops rows with no match, which is the classic cause of a total that is close to right and quietly low.

Then read every condition in the WHERE clause and confirm you meant it. Also watch for fan-out — a join to a one-to-many table duplicates rows and inflates any SUM, without a warning.

Validate against a number you already know

5 min

Run it for a period whose answer you can confirm elsewhere.

Before trusting a new query, run it over a window you have an independent figure for — last month's reported total, a count from another system.

If it matches, the joins and filters are probably right. If it is close but not equal, that gap is a real bug and is more informative than a wildly wrong answer, which you would have caught anyway.

Tips
  • Run against a replica or with a LIMIT first. A generated query on a large table can be accidentally very expensive.

Give it the real schema, read the joins and the filters, and check the row count against something you already know. Every query, until it stops surprising you.

Common questions

Read-only, on a replica, with a statement timeout — otherwise no. The combination of write access and a confident wrong query is not one to have on production.

Was this guide useful?

95% of readers found this useful

S

Sabir Verified

Founder & AI Enthusiast · AIToolsay

Founder of AIToolsay and a passionate AI enthusiast dedicated to building practical, user-friendly AI tools that simplify everyday tasks.

Read next