ChatGPT for SQL on a Live Database and Where a Team Needs More

Priya Anand, Data·Dec 2, 2026·7 min read

ChatGPT writes decent SQL from a pasted schema. A team that needs the same answer every week hits five limits, shown here on one real question.

Connected · demo_shop · MySQL · read‑only

Ask the demo shop a question, or type your own:

Reading the schema, writing SQL… Writing SQL… Running (read‑only)… SQL AgentSQL wrote

▋

Refine: refined ✓

A real MySQL shop: customers, products, orders and signups over the last year. AgentSQL writes the SQL, runs it read-only, and answers.

ChatGPT for SQL works well when one person who reads SQL pastes a schema, asks for a query, and checks the result by hand. It stops working for a team when the same questions come back every week, the schema changes, and the person asking cannot tell a right number from a wrong one. This article walks one real question through both routes. AgentSQL is the second route: a read-only AI data analyst for PostgreSQL and MySQL.

The question we will use

Take a small online shop with three tables. customers has id, name, country and created_at. orders has id, customer_id, total_cents, status and created_at. refunds has id, order_id and amount_cents. The question from the head of ops is: "What was net revenue per country last month, after refunds?"

It is a typical weekly question. Nobody in ops writes SQL, the answer feeds a Monday meeting, and a wrong number is worse than a late one.

Route one: paste the schema into ChatGPT

You paste the three table definitions and the question. ChatGPT returns a query that joins the tables, filters to last month and groups by country. The shape is usually right. Then the work starts.

  1. You run it yourself. ChatGPT has no connection to your database, so you copy the query into a client and paste any error back. Our article on whether ChatGPT can query your database covers the options for wiring a connection.
  2. The schema goes stale. Add a column or rename a table and the pasted definitions are wrong. The query fails or, worse, runs on the wrong field.
  3. Silent logic errors. If an order has two refunds and the query joins refunds directly to orders, the order row repeats and revenue is counted twice. The result looks plausible. Someone has to know to check.
  4. Definitions live in a head. Does "revenue" exclude cancelled orders? Is "last month" calendar or rolling? ChatGPT guesses unless you say so every time.
  5. Nothing is shared. The query sits in one person's chat history. The next person asks again and may get a different query and a different number.

The refund join, shown

A correct query sums refunds per order first, then joins:

SELECT c.country,
       SUM(o.total_cents - COALESCE(r.refunded, 0)) / 100.0 AS net_revenue
FROM orders o
JOIN customers c ON c.id = o.customer_id
LEFT JOIN (
  SELECT order_id, SUM(amount_cents) AS refunded
  FROM refunds GROUP BY order_id
) r ON r.order_id = o.id
WHERE o.status = 'paid'
  AND o.created_at >= date_trunc('month', now()) - interval '1 month'
  AND o.created_at < date_trunc('month', now())
GROUP BY c.country
ORDER BY net_revenue DESC;

The naive version joins refunds straight to orders. With one order of $100 and two refunds of $10 and $20, that order's total is added twice: $200 minus $30, a net of $170 instead of $70. The difference is invisible in the output unless you compare against a known figure.

Route two: a read-only tool that sees the schema

AgentSQL connects with a read-only database user, reads the table and column names itself and runs the generated query. You type the same question in plain English and get the number, a chart and the SQL, so someone who reads SQL can confirm the refund subquery is there. The Postgres side is described on the natural language to SQL for PostgreSQL page, and MySQL on the MySQL query generator page. Nothing is pasted, and nothing can be written to your data.

A read-only user is a setting you control on the database. PostgreSQL documents how to grant it in its GRANT reference: give the user SELECT on the tables it needs and nothing else.

What to check on any AI answer, from either route

Whichever tool writes the query, three checks catch most wrong numbers. First, compare one total against a figure you already trust, such as last month's invoiced amount. Second, look for any join to a table that can have several rows per order, and confirm the query aggregates that table before joining. Third, confirm the date range and the status filter match what your team means by the word in the question. Showing the SQL beside the result makes the second and third checks a thirty second job instead of a rerun.

Side by side for a team

NeedChatGPT with a pasted schemaAgentSQL
Who can askSomeone comfortable reading SQLAnyone who can type the question
Runs the queryYou, in your own clientThe tool, read-only
SchemaPasted by hand each timeRead from the connection
Result checkManualSQL shown with the number
PriceDepends on your ChatGPT planTeam $59 a month billed yearly, up to 10 people

What the time adds up to

Say ops asks ten new questions a week and each one costs twelve minutes of copying, running and checking by whoever knows SQL. That is two hours a week, about 104 hours a year, assumptions we picked for the example, not measured data. The Team plan is $708 a year. If the hours belong to someone you pay more than $7 an hour, the arithmetic favors the tool before counting the cost of a wrong number in a meeting.

Who should stay with ChatGPT

If you write SQL daily and want a faster first draft, a general chat model is a fair helper. It is also the right place to learn a syntax you rarely use. The case for a dedicated tool starts when people who do not write SQL need the numbers, or when the same questions repeat. The business users page describes that team, and the Sisense alternative page shows the same idea against a platform quote.

When the numbers feed a client document, the next step is the proposal itself, and Proposalcreation turns a brief into a finished proposal. For the data side, see AgentSQL pricing and connect a read-only user.

›_ frequently asked

Common questions

Can ChatGPT write SQL?
Yes. Given your table and column names it writes working SELECT queries with joins and grouping. It cannot run them, so you copy the query into your own client, and it only knows what you pasted.
Is ChatGPT good enough for SQL at work?
For a one-off query by someone who can read SQL, yes. For a team where non-technical people need weekly numbers, the schema pasting, the copying and the unchecked result become the cost.
What does a team use instead of ChatGPT for SQL?
A tool with a read-only database connection that reads the schema itself, runs the query and shows the SQL beside the result. AgentSQL does this for PostgreSQL and MySQL, and Team is $59 a month billed yearly.

See AgentSQL write and run the SQL live.

Ask a question in plain English, watch the query appear, and get a chart and an answer with the SQL shown. Then point AgentSQL at your own database.

See how it works

Ask your data in plain English.