Skip to main content


The SqlDatabaseChain allows you to answer questions over a SQL database. This example uses Chinook database, which is a sample database available for SQL Server, Oracle, MySQL, etc.

Set up

First install typeorm:

npm install typeorm

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

npm install sqlite3

For other databases see

Finally follow the instructions on to get the sample database for this example.

import { DataSource } from "typeorm";
import { OpenAI } from "langchain/llms";
import { SqlDatabase } from "langchain/sql_db";
import { SqlDatabaseChain } from "langchain/chains";

export const run = async () => {
const datasource = new DataSource({
type: "sqlite",
database: "Chinook.db",

const db = await SqlDatabase.fromDataSourceParams({
appDataSource: datasource,

const chain = new SqlDatabaseChain({
llm: new OpenAI({ temperature: 0 }),
database: db,

const res = await"How many tracks are there?");

await datasource.destroy();