liveBook · Manning

Text-to-SQL

As explained in AI Agents and Applications

Text-to-SQL is a technique that converts natural-language questions into executable SQL queries.

Text-to-SQL converts a natural-language question into an executable SQL query. It lets nontechnical users ask questions of relational databases containing structured facts such as temperatures, hotels, car rentals, orders, or financial records. section 10.1 section 10.8

A practical text-to-SQL flow connects the user, language model, and database:

  • Describe the target schema with CREATE TABLE statements and representative rows. section 10.3
  • Give the model the natural-language question together with the relevant schema context. section 10.3
  • Generate SQL, execute it against the relational database, and return the results. section 10.8
  • Check complex schemas and database-specific structures carefully because incorrect table or column names remain common failure modes. section 10.3
CREATE TABLE "state" (
    "state_name" TEXT,
    "population" INT DEFAULT NULL,
    "area" DOUBLE DEFAULT NULL,
    "country_name" VARCHAR(3) NOT NULL DEFAULT '',
    "capital" TEXT,
    "density" DOUBLE DEFAULT NULL
);
/* example rows
state_name     population     area      country_name
     capital       density
alabama        3894000        51700.0   usa
              montgomery    75.319149
alaska         401800         591000.0  usa
              juneau       0.679865
arizona        2718000        114000.0  usa
              phoenix      23.842105
*/
-- Answer the following question using the above table schema:
-- {user_question}

Use the CREATE TABLE definition and sample state records as prompt context before supplying a user question. section 10.3

Few-shot prompts containing schema definitions and sample records can reduce hallucinated table and column references. LangChain provides evolving text-to-SQL support, but accuracy still depends on the target schema and database structure. section 10.3

Natural-language database access

A user can ask, “How many units sold last quarter?” and route the question to an SQL database, while questions about executive statements can use a vector store instead. section 10.8

How AI Agents and Applications covers it

How deep it goes
Explains schema-aware prompting and execution, but stops short of database-specific implementation details.
Examples
Uses a state table with CREATE TABLE metadata, sample rows, and questions such as unit sales or resort information.
What you can do after
Apply via an LLM application to generate and execute SQL against structured relational data.
How it connects
Connects relational databases, SQL, few-shot prompting, LangChain, query routing, vector stores, and retrieval-augmented generation.

Also explained in

Compare how 2 books explain text-to-SQL →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of AI Agents and Applications. The section links point to the parts of the book it draws on.