Text to SQL is the conversion of a question written in plain language into an executable SQL query, so that a person can get an answer from a database without writing the query themselves. The same idea is often called NL2SQL.

Definition card explaining text to SQL as the conversion of a question written in plain language into an executable SQL query so a person can get an answer without writing the query, with three defining features: it is also called NL2SQL, schema linking that maps words to the right tables and columns decides most of the accuracy, and generated queries should be read-only and run with the asking user's permissions.

A simple example shows the shape of it. The question “What were total sales by region last quarter?” becomes a query that selects the sales amount, groups it by region and filters the order date to the previous quarter. The database runs the query and returns a small table that answers the question.

Text to SQL is used by business users who cannot write SQL, by analysts who want a first draft of a query, and by software that needs to answer questions on a user’s behalf, such as AI assistants inside analytics tools.

How Text to SQL Works

Most text to SQL systems follow five steps:

  1. Parse the question: identify the measure, the grouping, the time period and any filters the question asks for.
  2. Link words to tables and columns: map “sales”, “region” and “last quarter” to the actual tables, columns and date logic in the database. This step is called schema linking.
  3. Generate the SQL: a model, today usually a large language model, writes the query from the question and the linked schema.
  4. Validate and run it: check that the query parses, touches only permitted objects and is read-only, then execute it.
  5. Return and explain the result: present the rows, often with a chart and a sentence describing what the result shows.
Diagram of how text to SQL works in five steps: parse the question for its measure, grouping, period and filters, link those words to tables, columns and date logic in the schema, generate the SQL with a model, validate that the query is read-only and touches only permitted objects before running it, then return the rows with a chart and a sentence explaining them.

The order matters. A query generated before the words are linked to the right columns can be valid SQL and still answer the wrong question.

Text to SQL, NL2SQL and Natural Language Query

Three names describe overlapping ideas. Text to SQL and NL2SQL are the same thing: the output is a SQL statement. Natural language query is broader. It describes any interface that lets people ask data questions in everyday language, whether the system underneath writes SQL, queries a semantic model or calls an API.

Why Enterprise Schemas Make Text to SQL Hard

Demonstrations of text to SQL usually run on a few clean tables. Enterprise databases are different:

  • Thousands of tables and cryptic column names: an ERP schema can hold thousands of tables with names that mean little to a business user or a language model.
  • Joins the question never mentions: “invoices by supplier” can need several joins through headers, lines, sites and parties that no one says out loud.
  • Business meaning outside the schema: which accounts count as “operating expenses”, which orders count as “booked” and which calendar is the fiscal one live in finance policy, not in column names.

The model is rarely the weak link on an enterprise database. Schema linking is.

Schema Linking and the Semantic Layer

There are two broad ways to give text to SQL the context it needs:

ApproachWhat the model seesEffect on accuracy
Querying raw tablesTable and column names, perhaps with commentsThe model has to guess joins and business meaning, so errors grow with schema size
Querying a governed business modelBusiness objects, defined relationships and approved metricsThe model chooses from defined measures, so answers match the reports finance already uses

Shared metric definitions are the part people underestimate. If “gross margin” is defined once in the model, every generated query uses that definition. If it is not, two users asking the same question can get two different numbers.

How Text to SQL Accuracy Is Measured

Two public benchmarks dominate the research:

  • Spider: a cross-domain benchmark of questions written over about 200 databases, released by Yale researchers in 2018. It tests whether a system can handle schemas it has not seen.
  • BIRD: a later benchmark built on larger databases with messier values, where many questions need outside knowledge to answer correctly.

How a result is scored matters. Exact match compares the generated query with a reference query, which penalizes correct queries written differently. Execution accuracy runs both and compares the results, which is closer to what a user cares about.

Benchmark scores overstate enterprise accuracy. The databases are smaller and better named than a real ERP, and the questions come with clean intent. Teams should test against their own questions and known answers before trusting a score.

Security and Permissions in Generated Queries

A generated query is still a query against production data, so three controls apply:

  • Read-only access: the system should only ever run SELECT statements, through an account that cannot change data.
  • Row-level security and inherited roles: queries should run under the asking user’s permissions, so row-level security and role rules filter the result exactly as they would in a report.
  • Logging every generated query: recording the question, the SQL and the user makes errors traceable and gives auditors something to review.

Text to SQL on Oracle ERP Data

Oracle offers its own route. Oracle’s Select AI lets Autonomous Database users ask questions in natural language and generates the SQL with a large language model.

Oracle Fusion Cloud adds a constraint: customers cannot query the Fusion database directly, so data is usually extracted to a warehouse first. Teams that want to use AI to access Oracle Fusion data quickly find that the hard part is not generating SQL but knowing which of Fusion’s business objects and security rules a question touches. Orbit Analytics offers ThinkBoxes, which let business users ask questions of Oracle Fusion data with no SQL and no prompt engineering, answering from Orbit’s Fusion data models and a semantic layer rather than raw tables.

Oracle E-Business Suite customers usually own their database, so direct querying is possible. The difficulty is the schema: thousands of tables, flexfields and multi-org security that a generated query has to respect.

Text to SQL vs Writing SQL by Hand

Generated SQL saves time on routine work:

  • Where generated SQL helps: lookups, totals, rankings and period comparisons, and first drafts that an analyst then refines.
  • Where an analyst still writes the query: complex reconciliations, performance-sensitive queries, data quality investigations and anything that will feed a regulatory or audited report.

For analysts who do write SQL, Orbit Analytics offers a SQL editor for Oracle Fusion that runs queries against Oracle Fusion Cloud in real time from Excel.

Frequently Asked Questions

Q1. What is text to SQL in simple terms?

It is a way to ask a database a question in plain language and have software write the SQL query for you. The query runs, and you get the answer without writing code.

Q2. Is NL2SQL the same as text to SQL?

Yes. NL2SQL, short for natural language to SQL, is another name for the same task: turning a plain-language question into a SQL statement.

Q3. How accurate is text to SQL?

It varies widely. Systems score well on public benchmarks with clean schemas, but accuracy drops on large enterprise databases unless the system queries a governed business model with defined metrics.

Q4. Is text to SQL safe to run on production data?

It can be, with controls: read-only access, the asking user’s own permissions and a log of every generated query. Without those, it is a security risk.

Q5. What is schema linking in text to SQL?

Schema linking is the step that maps words in the question to the right tables, columns and filters. On enterprise databases it decides more of the accuracy than the model does.

Text to SQL works best when it answers from a model that already knows what the data means. Orbit Analytics provides governed data models for Oracle Fusion Cloud and EBS, so plain-language questions return consistent figures. Request a demo to see it on your own data.

wpChatIcon