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

