!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

*   [Use cases](/v0.1/docs/use_cases/)
    
    *   [SQL](/v0.1/docs/use_cases/sql/)
        
        *   [Quickstart](/v0.1/docs/use_cases/sql/quickstart/)
        *   [Agents](/v0.1/docs/use_cases/sql/agents/)
        *   [Prompting strategies](/v0.1/docs/use_cases/sql/prompting/)
        *   [Query validation](/v0.1/docs/use_cases/sql/query_checking/)
        *   [Large databases](/v0.1/docs/use_cases/sql/large_db/)
    *   [Chatbots](/v0.1/docs/use_cases/chatbots/)
        
    *   [Extraction](/v0.1/docs/use_cases/extraction/)
        
    *   [Query Analysis](/v0.1/docs/use_cases/query_analysis/)
        
    *   [Q&A with RAG](/v0.1/docs/use_cases/question_answering/)
        
    *   [Tool use](/v0.1/docs/use_cases/tool_use/)
        
    *   [Interacting with APIs](/v0.1/docs/use_cases/api/)
    *   [Tabular Question Answering](/v0.1/docs/use_cases/tabular/)
    *   [Graphs](/v0.1/docs/use_cases/graph/)
        
    *   [Summarization](/v0.1/docs/use_cases/summarization/)
    *   [Agent Simulations](/v0.1/docs/use_cases/agent_simulations/)
        
    *   [Autonomous Agents](/v0.1/docs/use_cases/autonomous_agents/)
        
    *   [Code Understanding](/v0.1/docs/use_cases/code_understanding/)
    *   [Audio/Video Structured Extraction](/v0.1/docs/use_cases/media/)

*   [](/v0.1/)
*   [Use cases](/v0.1/docs/use_cases/)
*   SQL

On this page

SQL
===

In this guide we'll go over the basic ways to create a Q&A chain and agent over a SQL database. These systems will allow us to ask a question about the data in a SQL database and get back a natural language answer. The main difference between the two is that our agent can query the database in a loop as many time as it needs to answer the question.

⚠️ Security note ⚠️[​](#️-security-note-️ "Direct link to ⚠️ Security note ⚠️")
-------------------------------------------------------------------------------

Building Q&A systems of SQL databases can require executing model-generated SQL queries. There are inherent risks in doing this. Make sure that your database connection permissions are always scoped as narrowly as possible for your chain/agent's needs. This will mitigate though not eliminate the risks of building a model-driven system. For more on general security best practices, see [here](/v0.1/docs/security/).

Architecture[​](#architecture "Direct link to Architecture")
------------------------------------------------------------

At a high-level, the steps of most SQL chain and agent are:

1.  **Convert question to SQL query**: Model converts user input to a SQL query.
2.  **Execute SQL query**: Execute the SQL query
3.  **Answer the question**: Model responds to user input using the query results.

![SQL Use Case Diagram](/v0.1/assets/images/sql_usecase-d432701261f05ab69b38576093718cf3.png)

Quickstart[​](#quickstart "Direct link to Quickstart")
------------------------------------------------------

Head to the [Quickstart](/v0.1/docs/use_cases/sql/quickstart/) to get started.

Advanced[​](#advanced "Direct link to Advanced")
------------------------------------------------

Once you've familiarized yourself with the basics, you can head to the advanced guides:

*   [Agents](/v0.1/docs/use_cases/sql/agents/): Building agents that can interact with SQL DBs.
*   [Prompting strategies](/v0.1/docs/use_cases/sql/prompting/): Strategies for improving SQL query generation.
*   [Query validation](/v0.1/docs/use_cases/sql/query_checking/): How to validate SQL queries.
*   [Large databases](/v0.1/docs/use_cases/sql/large_db/): How to interact with DBs with many tables and high-cardinality columns.

* * *

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

[

Previous

Use cases

](/v0.1/docs/use_cases/)[

Next

Quickstart

](/v0.1/docs/use_cases/sql/quickstart/)

*   [⚠️ Security note ⚠️](#️-security-note-️)
*   [Architecture](#architecture)
*   [Quickstart](#quickstart)
*   [Advanced](#advanced)

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.