Weekday and weekend classes available

Book a free 20-minute session with a mentor

No payment on this website: confirm your schedule and payment options with our team

Artificial Intelligence

How to check SQL an AI assistant writes for you

Vivek Kumar, Co-Founder & Chief Learning Officer3 min read
Check the SQL your AI writes

AI assistants write SQL quickly, and most of it runs. Running is not the same as being right. This is the short routine we teach for checking AI-written SQL before a number goes into a report.

An example that runs and is wrong

Suppose you ask an assistant for "revenue per customer from completed orders" and it returns this query. Tables: customers, orders (one row per order, with amount) and order_items (one row per product line).

SELECT c.name, SUM(o.amount) AS revenue
FROM customers c
JOIN orders o      ON o.customer_id = c.customer_id
JOIN order_items i ON i.order_id = o.order_id
WHERE o.status = 'completed'
GROUP BY c.customer_id, c.name;
namerevenue
Asha690.00
Ben400.00
Cara120.00
Eli60.00

It runs, the names look right and the numbers look plausible. But order_items has more than one line for some orders, so the join repeats those orders and SUM(o.amount) counts them more than once. Asha has an order of 250 with two item lines, so 250 is added twice.

The check that catches it: reconcile to a known total

SELECT SUM(amount) FROM orders WHERE status = 'completed';

Completed orders add up to 1,030. The query's customers add up to 1,270. The numbers disagree, so something is double-counted. Removing the unnecessary join gives:

SELECT c.name, SUM(o.amount) AS revenue
FROM customers c
JOIN orders o ON o.customer_id = c.customer_id
WHERE o.status = 'completed'
GROUP BY c.customer_id, c.name;
namerevenue
Asha440.00
Ben400.00
Cara120.00
Eli60.00

These add up to 1,020. The remaining 10 is a completed order with no customer ID. That is now a known, explainable difference, which you can mention in a footnote, instead of a hidden error.

A five-step routine for any AI-written query

  1. Read it before you run it. Say in plain words what each join and filter does. If you cannot, do not use the result yet.
  2. Count rows. Check the row count before and after each join. A count that grows unexpectedly is a fan-out.
  3. Reconcile one total. Compare the query's grand total with a simple, trusted query or with the finance report.
  4. Test an edge case. Pick one customer or one day and work out the answer by hand.
  5. Log what you checked. One line per check in your notes or README. It makes your work reviewable, and it is good portfolio evidence.

Prompts that help

Give the assistant the table structures and the grain of each table ("orders has one row per order; order_items has one row per product line"). Ask it to explain each join and to list the checks it would run. Then run those checks yourself. The assistant can suggest; you are responsible for the number.

This is what we mean by using AI like a professional, not as a shortcut: faster drafts, the same standard of proof.

More from Insights

Newsletter

Stay Ahead in Data and AI

Get new learning resources, practical guides, project ideas, and AI insights delivered to your inbox.

  • New free PDFs and Insights articles
  • We confirm by email before adding you
  • Unsubscribe in one click, any time
  • Learn data
  • Use AI
  • Build real projects
  • Build your portfolio
  • Develop professional skills
  • Get guided support
  • Apply it at work

Find your program.

A few short questions and a personalized report. Or talk to a mentor for free.

Talk to a mentorFind my program

Your cookie choices

Choose which optional cookies we may use. Essential storage is always on because the site needs it.

EssentialAlways on

Your theme and currency, this choice, and spam protection on forms.

Analytics

Google Analytics, to count visits and see which pages and links help people. Also lets us note which link brought you here and send it with a request you make.

Advertising

Meta (Facebook, Instagram) and Google Ads, to measure whether our ads lead to enquiries.

Cookie policy · Privacy policy ·