Building intelligent assistants that can understand natural language and query business data is now practical for many teams. A common use case is letting users ask questions in plain English and getting answers from a SQL database. In this step-by-step tutorial, you’ll learn LangChain SQL assistant basics: setting up the environment, connecting to a database, creating a SQL chain or agent, and running natural-language queries against your data.
Step 1: Setting Up the Environment
Start by creating a Python environment and installing the required libraries. Then import what you need for a LangChain SQL assistant.
import os
from langchain.llms import OpenAI
from langchain.sql_database import SQLDatabase
from langchain.chains import SQLDatabaseChain
from langchain.agents import create_sql_agent
If you’re not using OpenAI, replace OpenAI with your preferred provider and update the initialization accordingly.
Step 2: Connecting to a SQL Database
Next, connect to your database. This example uses SQLite, but the same LangChain SQL assistant pattern works for PostgreSQL, MySQL, and other SQL engines (via the appropriate connection string).
# Connect to the SQLite database
db_path = "path_to_your_database.db"
db = SQLDatabase.from_uri(f"sqlite:///{db_path}")
Step 3: Setting Up the Language Model
Configure the language model you want LangChain to use. Make sure your API key is available as an environment variable or loaded securely.
# Set up the OpenAI language model
openai_api_key = os.getenv("OPENAI_API_KEY") # recommended over hardcoding
llm = OpenAI(openai_api_key=openai_api_key)
This model will power your LangChain SQL assistant by translating natural language questions into SQL queries.
Step 4: Creating the SQL Database Chain (or SQL Agent)
LangChain gives you two common approaches:
- SQLDatabaseChain: A direct “question → SQL → answer” workflow
- SQL Agent: More flexible, especially for multi-step reasoning and tool use
Option A: SQLDatabaseChain
# Create the SQL Database Chain
sql_chain = SQLDatabaseChain(llm=llm, db=db)
Option B: SQL Agent
# Create the SQL Agent
sql_agent = create_sql_agent(llm=llm, db=db)
Both options can form the core of a LangChain SQL assistant. Start simple with a chain, then move to an agent if you need richer behaviors.
Step 5: Querying the Database with Natural Language
Now you can run natural language queries against your database. Here’s a basic example:
Using SQLDatabaseChain
# Define a natural language query
query = "Show me the total sales for the last quarter."
# Use the SQL Database Chain to process the query and fetch results
result = sql_chain.run(query)
print(result)
Using the SQL Agent
# Define a natural language query
query = "Show me the total sales for the last quarter."
# Use the SQL Agent to process the query and fetch results
result = sql_agent.invoke(query)
# Display the result
print(result)
With a LangChain SQL assistant, you can expand from single questions to more guided experiences: clarifying questions, safe query constraints, and better result formatting.
Conclusion
This guide covered the foundations of building a LangChain SQL assistant that translates natural language into SQL and returns answers from your database. From here, you can extend the assistant by adding better schema context, improving prompting, enforcing guardrails (like limiting tables and columns), supporting multi-step questions, and connecting multiple data sources.
In the last five years, we at CoReCo Technologies have worked with 60+ businesses across industries globally. We not only developed their products and platforms but also helped bring more clarity into their vision and strategy.
For more details about such case studies, visit us at www.corecotechnologies.com and if you would like to convert this virtual conversation into a real collaboration, please write to us.