New to AI? Text-to-SQL: When the SQL Is Right, and When It Only Looks Right

Hi all. Text-to-SQL (you'll also see NL2SQL) is the demo everyone loves: type "top 5 customers last month" and get a query, maybe even the result. Sometimes it's brilliant. Sometimes the SQL runs perfectly and answers a different question. This post is about telling those two apart.

How it works

The model gets two things in its context window: your question, and a description of your schema, meaning table names, column names, types and, if you have them, comments. From that it writes SQL. It doesn't see your data and it doesn't know your business words, unless you tell it.

Where you'll meet itWhat it does
Oracle Select AIYou set up an AI profile with the tables it may use, then ask in SQL: SELECT AI showsql top 5 customers last month;. Other actions: runsql, explainsql, narrate, chat.
Copilot in SSMSWrites and explains T-SQL in the query editor and runs queries with the permissions of your login.
MCP toolsAn agent reads the schema through an MCP server and runs the SQL it wrote. Works for any engine, including PostgreSQL.

Interesting counterpoint: Microsoft's SQL MCP Server deliberately doesn't do NL2SQL. It gives the agent fixed, described entities instead of free SQL, because generated SQL is hard to make safe and repeatable.

Looks right vs. is right

Both queries run. Both look professional. Only one matches what finance means by "last month". Typical traps:

  • Ambiguous words: last month, active customer, revenue (with or without tax? cancelled orders?).
  • Invented names: a column that "should" exist, like customer_name when yours is cust_nm.
  • Join mistakes: a one-to-many join that doubles the totals. No error, just wrong numbers.
  • NULLs and status flags: soft-deleted rows, test accounts, status = 'X' that only your team knows about.

How a DBA makes it better

This is where we're actually useful, because most fixes are classic database work:

  • Comments on tables and columns. COMMENT ON COLUMN orders.amount IS 'Net amount, excl. VAT, in EUR' works in Oracle and PostgreSQL; SQL Server uses extended properties. The model reads them.
  • Clean views with business names. One view v_monthly_revenue beats twelve raw tables. Fewer joins to guess.
  • A small object list. Give the profile or the tool only the tables it needs, not the whole schema.
  • Show the SQL first. showsql before runsql. Read it like code review.
  • Test questions with known answers. Ten questions where you know the right number. Rerun them when the model or the schema changes.
  • A read-only account with limits. Timeouts and resource limits, so a bad query costs seconds, not the night batch.
Generated SQL is a draft from someone who has never seen your data. Review it like one.

Official reading

Next: MCP, agents and skills, how AI gets real hands on a database. Have you caught a generated query that ran fine and was still wrong? Tell me in the comments.

The New to AI? series

All parts, in one place: New to AI? And if you want every AI word mapped to something DBAs already know, start with You Already Speak AI (also for SQL Server and PostgreSQL).

Comments