!function(){function t(t){document.documentElement.setAttribute("data-theme",t)}var e=function(){var t=null;try{t=new URLSearchParams(window.location.search).get("docusaurus-theme")}catch(t){}return t}()||function(){var t=null;try{t=localStorage.getItem("theme")}catch(t){}return t}();t(null!==e?e:"light")}(),document.documentElement.setAttribute("data-announcement-bar-initially-dismissed",function(){try{return"true"===localStorage.getItem("docusaurus.announcement.dismiss")}catch(t){}return!1}())

[Skip to main content](#__docusaurus_skipToContent_fallback)

LangChain v0.2 is coming soon! Preview the new docs [here](/v0.2/docs/introduction/).

[

![🦜️🔗 Langchain](/v0.1/img/brand/wordmark.png)![🦜️🔗 Langchain](/v0.1/img/brand/wordmark-dark.png)

](/v0.1/)[Docs](/v0.1/docs/get_started/introduction/)[Use cases](/v0.1/docs/use_cases/)[Integrations](/v0.1/docs/integrations/platforms/)[API Reference](https://api.js.langchain.com)

[More](#)

*   [People](/v0.1/docs/people/)
*   [Community](/v0.1/docs/community/)
*   [Tutorials](/v0.1/docs/additional_resources/tutorials/)
*   [Contributing](/v0.1/docs/contributing/)

[v0.1](#)

*   [v0.2](https://js.langchain.com/v0.2/docs/introduction)
*   [v0.1](/v0.1/docs/get_started/introduction/)

[🦜🔗](#)

*   [LangSmith](https://smith.langchain.com)
*   [LangSmith Docs](https://docs.smith.langchain.com)
*   [LangChain Hub](https://smith.langchain.com/hub)
*   [LangServe](https://github.com/langchain-ai/langserve)
*   [Python Docs](https://python.langchain.com/)

[Chat](https://chatjs.langchain.com)[](https://github.com/langchain-ai/langchainjs)

Search

On this page

SQL
===

This example demonstrates the use of `Runnables` with questions and more on a SQL database.

This example uses Chinook database, which is a sample database available for SQL Server, Oracle, MySQL, etc.

info

Looking for the older, non-LCEL version? Click [here](/v0.1/docs/modules/chains/popular/sqlite_legacy/).

Set up[​](#set-up "Direct link to Set up")
------------------------------------------

First install `typeorm`:

*   npm
*   Yarn
*   pnpm

    npm install typeorm

    yarn add typeorm

    pnpm add typeorm

Then, install the dependencies needed for your database. For example, for SQLite:

*   npm
*   Yarn
*   pnpm

    npm install sqlite3

    yarn add sqlite3

    pnpm add sqlite3

LangChain offers default prompts for: default SQL, Postgres, SQLite, Microsoft SQL Server, MySQL, and SAP HANA.

Finally follow the instructions on [https://database.guide/2-sample-databases-sqlite/](https://database.guide/2-sample-databases-sqlite/) to get the sample database for this example.

tip

See [this section for general instructions on installing integration packages](/v0.1/docs/get_started/installation/#installing-integration-packages).

*   npm
*   Yarn
*   pnpm

    npm install @langchain/openai

    yarn add @langchain/openai

    pnpm add @langchain/openai

    import { DataSource } from "typeorm";import { SqlDatabase } from "langchain/sql_db";import { ChatOpenAI } from "@langchain/openai";import { PromptTemplate } from "@langchain/core/prompts";import { RunnableSequence } from "@langchain/core/runnables";import { StringOutputParser } from "@langchain/core/output_parsers";/** * This example uses Chinook database, which is a sample database available for SQL Server, Oracle, MySQL, etc. * To set it up follow the instructions on https://database.guide/2-sample-databases-sqlite/, placing the .db file * in the examples folder. */const datasource = new DataSource({  type: "sqlite",  database: "Chinook.db",});const db = await SqlDatabase.fromDataSourceParams({  appDataSource: datasource,});const llm = new ChatOpenAI();/** * Create the first prompt template used for getting the SQL query. */const prompt =  PromptTemplate.fromTemplate(`Based on the provided SQL table schema below, write a SQL query that would answer the user's question.------------SCHEMA: {schema}------------QUESTION: {question}------------SQL QUERY:`);/** * You can also load a default prompt by importing from "langchain/sql_db" * * import { *   DEFAULT_SQL_DATABASE_PROMPT *   SQL_POSTGRES_PROMPT *   SQL_SQLITE_PROMPT *   SQL_MSSQL_PROMPT *   SQL_MYSQL_PROMPT *   SQL_SAP_HANA_PROMPT * } from "langchain/sql_db"; * *//** * Create a new RunnableSequence where we pipe the output from `db.getTableInfo()` * and the users question, into the prompt template, and then into the llm. * We're also applying a stop condition to the llm, so that it stops when it * sees the `\nSQLResult:` token. */const sqlQueryChain = RunnableSequence.from([  {    schema: async () => db.getTableInfo(),    question: (input: { question: string }) => input.question,  },  prompt,  llm.bind({ stop: ["\nSQLResult:"] }),  new StringOutputParser(),]);const res = await sqlQueryChain.invoke({  question: "How many employees are there?",});console.log({ res });/** * { res: 'SELECT COUNT(*) FROM tracks;' } *//** * Create the final prompt template which is tasked with getting the natural language response. */const finalResponsePrompt =  PromptTemplate.fromTemplate(`Based on the table schema below, question, SQL query, and SQL response, write a natural language response:------------SCHEMA: {schema}------------QUESTION: {question}------------SQL QUERY: {query}------------SQL RESPONSE: {response}------------NATURAL LANGUAGE RESPONSE:`);/** * Create a new RunnableSequence where we pipe the output from the previous chain, the users question, * and the SQL query, into the prompt template, and then into the llm. * Using the result from the `sqlQueryChain` we can run the SQL query via `db.run(input.query)`. */const finalChain = RunnableSequence.from([  {    question: (input) => input.question,    query: sqlQueryChain,  },  {    schema: async () => db.getTableInfo(),    question: (input) => input.question,    query: (input) => input.query,    response: (input) => db.run(input.query),  },  finalResponsePrompt,  llm,  new StringOutputParser(),]);const finalResponse = await finalChain.invoke({  question: "How many employees are there?",});console.log({ finalResponse });/** * { finalResponse: 'There are 8 employees.' } */

#### API Reference:

*   [SqlDatabase](https://api.js.langchain.com/classes/langchain_sql_db.SqlDatabase.html) from `langchain/sql_db`
*   [ChatOpenAI](https://api.js.langchain.com/classes/langchain_openai.ChatOpenAI.html) from `@langchain/openai`
*   [PromptTemplate](https://api.js.langchain.com/classes/langchain_core_prompts.PromptTemplate.html) from `@langchain/core/prompts`
*   [RunnableSequence](https://api.js.langchain.com/classes/langchain_core_runnables.RunnableSequence.html) from `@langchain/core/runnables`
*   [StringOutputParser](https://api.js.langchain.com/classes/langchain_core_output_parsers.StringOutputParser.html) from `@langchain/core/output_parsers`

You can include or exclude tables when creating the `SqlDatabase` object to help the chain focus on the tables you want. It can also reduce the number of tokens used in the chain.

    const db = await SqlDatabase.fromDataSourceParams({  appDataSource: datasource,  includesTables: ["Track"],});

If desired, you can return the used SQL command when calling the chain.

    import { DataSource } from "typeorm";import { SqlDatabase } from "langchain/sql_db";import { ChatOpenAI } from "@langchain/openai";import { PromptTemplate } from "@langchain/core/prompts";import { RunnableSequence } from "@langchain/core/runnables";import { StringOutputParser } from "@langchain/core/output_parsers";/** * This example uses Chinook database, which is a sample database available for SQL Server, Oracle, MySQL, etc. * To set it up follow the instructions on https://database.guide/2-sample-databases-sqlite/, placing the .db file * in the examples folder. */const datasource = new DataSource({  type: "sqlite",  database: "Chinook.db",});const db = await SqlDatabase.fromDataSourceParams({  appDataSource: datasource,});const llm = new ChatOpenAI();/** * Create the first prompt template used for getting the SQL query. */const prompt =  PromptTemplate.fromTemplate(`Based on the provided SQL table schema below, write a SQL query that would answer the user's question.------------SCHEMA: {schema}------------QUESTION: {question}------------SQL QUERY:`);/** * Create a new RunnableSequence where we pipe the output from `db.getTableInfo()` * and the users question, into the prompt template, and then into the llm. * We're also applying a stop condition to the llm, so that it stops when it * sees the `\nSQLResult:` token. */const sqlQueryChain = RunnableSequence.from([  {    schema: async () => db.getTableInfo(),    question: (input: { question: string }) => input.question,  },  prompt,  llm.bind({ stop: ["\nSQLResult:"] }),  new StringOutputParser(),]);/** * Create the final prompt template which is tasked with getting the natural * language response to the SQL query. */const finalResponsePrompt =  PromptTemplate.fromTemplate(`Based on the table schema below, question, SQL query, and SQL response, write a natural language response:------------SCHEMA: {schema}------------QUESTION: {question}------------SQL QUERY: {query}------------SQL RESPONSE: {response}------------NATURAL LANGUAGE RESPONSE:`);/** * Create a new RunnableSequence where we pipe the output from the previous chain, the users question, * and the SQL query, into the prompt template, and then into the llm. * Using the result from the `sqlQueryChain` we can run the SQL query via `db.run(input.query)`. * * Lastly we're piping the result of the first chain (the outputted SQL query) so it is * logged along with the natural language response. */const finalChain = RunnableSequence.from([  {    question: (input) => input.question,    query: sqlQueryChain,  },  {    schema: async () => db.getTableInfo(),    question: (input) => input.question,    query: (input) => input.query,    response: (input) => db.run(input.query),  },  {    result: finalResponsePrompt.pipe(llm).pipe(new StringOutputParser()),    // Pipe the query through here unchanged so it gets logged alongside the result.    sql: (previousStepResult) => previousStepResult.query,  },]);const finalResponse = await finalChain.invoke({  question: "How many employees are there?",});console.log({ finalResponse });/** * { *   finalResponse: { *     result: 'There are 8 employees.', *     sql: 'SELECT COUNT(*) FROM tracks;' *   } * } */

#### API Reference:

*   [SqlDatabase](https://api.js.langchain.com/classes/langchain_sql_db.SqlDatabase.html) from `langchain/sql_db`
*   [ChatOpenAI](https://api.js.langchain.com/classes/langchain_openai.ChatOpenAI.html) from `@langchain/openai`
*   [PromptTemplate](https://api.js.langchain.com/classes/langchain_core_prompts.PromptTemplate.html) from `@langchain/core/prompts`
*   [RunnableSequence](https://api.js.langchain.com/classes/langchain_core_runnables.RunnableSequence.html) from `@langchain/core/runnables`
*   [StringOutputParser](https://api.js.langchain.com/classes/langchain_core_output_parsers.StringOutputParser.html) from `@langchain/core/output_parsers`

Disclaimer ⚠️
=============

The query chain may generate insert/update/delete queries. When this is not expected, use a custom prompt or create SQL users without write permissions.

The final user might overload your SQL database by asking a simple question such as "run the biggest query possible". The generated query might look like:

    SELECT * FROM "public"."users"    JOIN "public"."user_permissions" ON "public"."users".id = "public"."user_permissions".user_id    JOIN "public"."projects" ON "public"."users".id = "public"."projects".user_id    JOIN "public"."events" ON "public"."projects".id = "public"."events".project_id;

For a transactional SQL database, if one of the table above contains millions of rows, the query might cause trouble to other applications using the same database.

Most datawarehouse oriented databases support user-level quota, for limiting resource usage.

* * *

#### Help us out by providing feedback on this documentation page:

*   [Set up](#set-up)

Community

*   [Discord](https://discord.gg/cU2adEyC7w)
*   [Twitter](https://twitter.com/LangChainAI)

GitHub

*   [Python](https://github.com/langchain-ai/langchain)
*   [JS/TS](https://github.com/langchain-ai/langchainjs)

More

*   [Homepage](https://langchain.com)
*   [Blog](https://blog.langchain.dev)

Copyright © 2024 LangChain, Inc.