Text to SQL: how we used the GPT API to turn plain English into database queries

GPT API for SQL query creation: chat prompt turned into a database query

Text to SQL lets someone ask a database a question in plain English and get an answer without writing any SQL. We used the GPT API to let our non-technical stakeholders do exactly that, and first wrote up the process in April 2023. This updated version keeps our original account and adds what the evidence now says about how accurate and how safe text to SQL is.

The model is rarely the hard part. How accurate the answers are depends mostly on the context the model is given about your database. How safe it is depends on what the generated query is allowed to touch.

What is text to SQL?

Text to SQL, also known as NL2SQL or natural language to SQL, is a way of querying a database by asking a question in everyday language. A large language model (LLM) reads the question, works out which tables and columns it refers to, and writes the SQL query. The database then runs that query and returns the result.

At a minimum, the model needs the structure of the database, known as the schema, and the question. It does not have to see the data itself. Some systems also add sample values from the data to improve accuracy. Pinterest, for example, adds common column values to the schema it gives the model.

How we used the GPT API for text to SQL

Our team implemented the GPT API into our database management process to allow non-technical stakeholders to write SQL queries in natural language. The process involved four steps.

  1. Providing the database schema. We first provided the GPT API with the database schema to help it understand the relationships between tables and columns in the database. This was done by defining the schema using JSON and providing it to the GPT API.
  2. User inputs query in natural language. The user inputs their query in natural language using a web interface.
  3. GPT generates SQL query. The GPT API processes the input and generates a SQL query based on the user's intent and the provided database schema.
  4. SQL query is executed. The SQL query is executed on the database, and the results are displayed to the user.

For example, a user may input the following natural language query: “What are the total sales by product category for the last quarter?” The GPT API would then generate the following SQL query.

SELECT ProductCategory,
       SUM(Sales)
FROM SalesTable
WHERE Date >= '2021-10-01'
  AND Date <= '2021-12-31'
GROUP BY ProductCategory;

This process allows non-technical stakeholders to write SQL queries without having to understand the syntax and structure of the language.

In this design, the GPT API received the schema and the user's question. The query itself ran against our database, and the results went back to the user. Where the model runs matters if your questions or your schema are sensitive. We compare those options in our guide to choosing between a private LLM or an API.

What text to SQL gave our team

Implementing the GPT API into our database management process had three main benefits.

  • Improved efficiencyBy allowing non-technical stakeholders to write SQL queries in natural language, we reduced the time and effort required to create queries. This resulted in faster query creation and improved productivity.
  • Fewer syntax errorsNatural language input reduces the likelihood of syntax errors in SQL queries, because users no longer write the SQL by hand. However, it does not remove every error. A query can be valid SQL and still answer a slightly different question, which is why the checks below matter.
  • Better collaborationThe GPT API allowed for better collaboration between technical and non-technical stakeholders. Non-technical stakeholders could create queries without relying on technical staff, leading to more efficient communication and collaboration.

How accurate is text to SQL now?

When we first wrote this up, we noted that the GPT API is not without its limitations. We also noted that the GPT-3 model we used is not perfect. Nevertheless, at the time we concluded that the benefits of using the GPT API outweighed the limitations. The evidence published since then shows where those limitations sit.

In short, text to SQL now gets most answers right on well-described test databases, and far fewer on large business ones. Specifically, two public benchmarks show the gap.

What the text to SQL benchmarks show

First, BIRD contains 12,751 questions across 95 databases, with 33.4 GB of data. When it was published in May 2023, a month after our original post, ChatGPT reached 40.08% execution accuracy, while human experts reached 92.96%. As of November 2026, the top system on the BIRD leaderboard, AskData from AT&T, reaches 83.40%.

Spider 2.0, on the other hand, is closer to real business work. It has 632 tasks on enterprise databases that often contain more than 1,000 columns, in systems such as BigQuery and Snowflake. In the paper presented at ICLR 2025, the best model solved 21.3% of these tasks. By comparison, the same model scored 91.2% on the original Spider benchmark and 73.0% on BIRD.

Why context matters more than the model

The difference comes down to context. For example, BIRD's authors point to messy data values, and to the outside knowledge needed to link a question to the right tables. Consequently, our original line that accuracy depends on the quality of the training data needs correcting. In practice, accuracy depends far more on how well the schema and the business terms are described to the model.

Similarly, Pinterest's engineering team saw this in its own text to SQL tool. Adding documentation for its tables raised the share of searches that found the right tables from 40% to as much as 90%. Meanwhile, its first-shot acceptance rate for generated SQL rose from 20% to above 40%. Users also completed SQL tasks 35% faster with AI help.

Is text to SQL safe on a live database?

It can be, if the generated query is treated as untrusted input. The OWASP Top 10 for LLM Applications 2025 covers this risk under improper output handling. LLM-generated SQL that runs without proper parameterisation can lead to SQL injection. In one of its attack scenarios, a chat feature writes SQL for a back-end database, and an unchecked query deletes every table.

In addition, OWASP's guidance on excessive agency adds a second control. Give the model's connection only the permissions the task needs. To illustrate, an agent in OWASP's database example that only needs to read one table should not be able to insert, update or delete records. We set out the same thinking for any agent with system access in our guide to AI agent governance.

The trap

Connecting the model to the database through the same account your developers use. One wrong or manipulated query can then change or delete data.

The fix

Run generated queries through a read-only account limited to the tables users need. Check each query before it runs, and log what was asked and what ran.

OWASP also ranks prompt injection first on the same list. This is the risk that text the model reads carries instructions of its own. It is the same weakness behind the SalesBleed flaws in Salesforce Agentforce. There, a public lead form was enough to hijack a company's agent.

Finally, if users' questions or query results can include personal data, UK GDPR applies to the whole process. The ICO's guidance on AI and data protection covers this, although the ICO says the guidance is under review following the Data (Use and Access) Act.

What to check before you build text to SQL

Most of these checks are about the database and the process around the model, rather than the model itself.

  1. Describe the schema. Clear table and column names, with short descriptions, give the model less to guess. Metabase, which builds text to SQL into its analytics tool, says schema quality decides accuracy more than the choice of model does.
  2. Define business terms once. Words such as revenue or active customer often mean different things to different teams. Therefore, define them in one place the model can reference, often called a semantic layer.
  3. Limit what a query can touch. Use a read-only account restricted to the tables people need, as OWASP recommends.
  4. Show the SQL. Let users see the generated query, and keep a person in the loop for any answer that feeds a decision. Metabase compares text to SQL to a junior analyst, fast and useful but in need of review on anything that matters.
  5. Test against known answers. Keep a set of questions with correct answers, and re-run it whenever the schema or the prompts change.

Frequently asked questions

What is text to SQL?

Text to SQL turns a question written in plain English into a SQL query that runs on a database. To do this, a large language model writes the query from the database schema and the question.

Is NL2SQL the same as text to SQL?

Yes. NL2SQL, natural language to SQL and text to SQL all describe the same task. Each means converting a natural language question into a SQL query. A text to SQL LLM is simply a large language model used for that task.

Can ChatGPT write SQL queries?

Yes. ChatGPT and the GPT API can write SQL from a plain English question. However, they need the database schema to get table and column names right, and each query should be checked before it runs on live data.

How accurate is text to SQL?

It depends on the database. As of November 2026, the top system on the BIRD benchmark reaches 83.40% execution accuracy, against 92.96% for human experts. On Spider 2.0's enterprise tasks, the best model in the published paper solved 21.3%.

Is it safe to let an LLM run SQL on a live database?

Only with controls in place. Treat the generated query as untrusted, run it through a read-only account limited to the tables needed, and check it before it runs, as OWASP recommends.

Do you still need SQL skills with text to SQL?

Someone on the team does. People who can read SQL can check the generated query, and technical staff still own the schema, the business definitions and the tests.

Thinking about text to SQL for your own data?

We build LLM and generative AI applications for UK businesses. We have also used the GPT API for text to SQL in our own database process.

Our free 30-minute discovery call looks at one process in your business and gives you a rough cost, timeline and estimated return.

Book a discovery call

Sources

Have a project in mind? Let’s get to work.

Let’s chat about how we can help you. Fill in the details and we’ll get back to you as soon we can.