Can AI Bots Like ChatGPT Replace Business Analysts? A LangChain‑Powered Data Analysis Demo

This article explores whether AI agents such as ChatGPT can help business owners quickly perform data analysis by linking large language models with local MySQL databases via LangChain, presenting the experimental setup, schema creation, sample queries, results, and practical insights.

Alibaba Cloud Developer
Alibaba Cloud Developer
Alibaba Cloud Developer
Can AI Bots Like ChatGPT Replace Business Analysts? A LangChain‑Powered Data Analysis Demo

Background

The author imagines a scenario where a trade‑company owner needs real‑time operational insights from a private inventory system covering customers, suppliers, sales, stock, and purchases, and wonders if ChatGPT‑style AI agents can fulfill these analytical requests through natural‑language interaction.

Working Principle

Because ChatGPT’s knowledge cutoff is September 2021 and it cannot directly access the internet or local networks, the solution combines ChatGPT with LangChain’s data‑awareness and agent capabilities to link the language model to external data sources. The owner can ask questions in natural language, and the engine generates and executes SQL against the local database, returning results and visualizations.

Data Model and Test Data

The following SQL creates a simple inventory management schema and populates it with synthetic test data.

CREATE TABLE Product (
    ProductID INT PRIMARY KEY,
    ProductName VARCHAR(255),
    ProductDescription TEXT,
    CostPrice DECIMAL(10,2),
    SalePrice DECIMAL(10,2)
);

CREATE TABLE Supplier (
    SupplierID INT PRIMARY KEY,
    SupplierName VARCHAR(255),
    ContactPerson VARCHAR(255),
    ContactPhone VARCHAR(20),
    Address TEXT
);

CREATE TABLE Customer (
    CustomerID INT PRIMARY KEY,
    CustomerName VARCHAR(255),
    ContactPerson VARCHAR(255),
    ContactPhone VARCHAR(20),
    Address TEXT
);

CREATE TABLE Inventory (
    InventoryID INT PRIMARY KEY,
    ProductID INT,
    Quantity INT,
    FOREIGN KEY (ProductID) REFERENCES Product(ProductID)
);

CREATE TABLE PurchaseOrder (
    PurchaseOrderID INT PRIMARY KEY,
    SupplierID INT,
    OrderDate DATE,
    FOREIGN KEY (SupplierID) REFERENCES Supplier(SupplierID)
);

CREATE TABLE SalesOrder (
    SalesOrderID INT PRIMARY KEY,
    CustomerID INT,
    OrderDate DATE,
    FOREIGN KEY (CustomerID) REFERENCES Customer(CustomerID)
);

CREATE TABLE PurchaseOrderDetail (
    PurchaseOrderDetailID INT PRIMARY KEY,
    PurchaseOrderID INT,
    ProductID INT,
    Quantity INT,
    Price DECIMAL(10,2),
    FOREIGN KEY (PurchaseOrderID) REFERENCES PurchaseOrder(PurchaseOrderID),
    FOREIGN KEY (ProductID) REFERENCES Product(ProductID)
);

CREATE TABLE SalesOrderDetail (
    SalesOrderDetailID INT PRIMARY KEY,
    SalesOrderID INT,
    ProductID INT,
    Quantity INT,
    Price DECIMAL(10,2),
    FOREIGN KEY (SalesOrderID) REFERENCES SalesOrder(SalesOrderID),
    FOREIGN KEY (ProductID) REFERENCES Product(ProductID)
);
INSERT INTO Product VALUES
(1,'Product1','Description1',10.00,20.00),
(2,'Product2','Description2',15.00,25.00),
(3,'Product3','Description3',20.00,30.00),
(4,'Product4','Description4',25.00,35.00),
(5,'Product5','Description5',30.00,40.00),
(6,'Product6','Description6',35.00,45.00),
(7,'Product7','Description7',40.00,50.00),
(8,'Product8','Description8',45.00,55.00),
(9,'Product9','Description9',50.00,60.00),
(10,'Product10','Description10',55.00,65.00);

INSERT INTO Supplier VALUES
(1,'Supplier1','Contact1','1234567890','Address1'),
(2,'Supplier2','Contact2','1234567890','Address2'),
(3,'Supplier3','Contact3','1234567890','Address3'),
(4,'Supplier4','Contact4','1234567890','Address4'),
(5,'Supplier5','Contact5','1234567890','Address5'),
(6,'Supplier6','Contact6','1234567890','Address6'),
(7,'Supplier7','Contact7','1234567890','Address7'),
(8,'Supplier8','Contact8','1234567890','Address8'),
(9,'Supplier9','Contact9','1234567890','Address9'),
(10,'Supplier10','Contact10','1234567890','Address10');

INSERT INTO Customer VALUES
(1,'Customer1','Contact1','1234567890','Address1'),
(2,'Customer2','Contact2','1234567890','Address2'),
(3,'Customer3','Contact3','1234567890','Address3'),
(4,'Customer4','Contact4','1234567890','Address4'),
(5,'Customer5','Contact5','1234567890','Address5'),
(6,'Customer6','Contact6','1234567890','Address6'),
(7,'Customer7','Contact7','1234567890','Address7'),
(8,'Customer8','Contact8','1234567890','Address8'),
(9,'Customer9','Contact9','1234567890','Address9'),
(10,'Customer10','Contact10','1234567890','Address10');

INSERT INTO Inventory VALUES
(1,1,100),(2,2,200),(3,3,300),(4,4,400),(5,5,500),(6,6,600),(7,7,700),(8,8,800),(9,9,900),(10,10,1000);

INSERT INTO PurchaseOrder VALUES
(1,1,'2023-01-01'),(2,2,'2023-02-01'),(3,3,'2023-03-01'),(4,4,'2023-04-01'),(5,5,'2023-05-01'),(6,6,'2023-06-01'),(7,7,'2023-07-01'),(8,8,'2023-08-01'),(9,9,'2023-09-01'),(10,10,'2023-10-01');

INSERT INTO SalesOrder VALUES
(1,1,'2023-01-01'),(2,2,'2023-02-01'),(3,3,'2023-03-01'),(4,4,'2023-04-01'),(5,5,'2023-05-01'),(6,6,'2023-06-01'),(7,7,'2023-07-01'),(8,8,'2023-08-01'),(9,9,'2023-09-01'),(10,10,'2023-10-01');

INSERT INTO PurchaseOrderDetail VALUES
(1,1,1,10,10.00),(2,2,2,20,15.00),(3,3,3,30,20.00),(4,4,4,40,25.00),(5,5,5,50,30.00),(6,6,6,60,35.00),(7,7,7,70,40.00),(8,8,8,80,45.00),(9,9,9,90,50.00),(10,10,10,100,55.00);

INSERT INTO SalesOrderDetail VALUES
(1,1,1,10,20.00),(2,2,2,20,25.00),(3,3,3,30,30.00),(4,4,4,40,35.00),(5,5,5,50,40.00),(6,6,6,60,45.00),(7,7,7,70,50.00),(8,8,8,80,55.00),(9,9,9,90,60.00),(10,10,10,100,65.00);

Test Environment

MySQL 8.0 was used as the local database, the large language model was OpenAI’s GPT‑3, and LangChain version 0.0.191 provided the linking framework.

Experimental Process

Five typical business queries were issued to the system. For each scenario the natural‑language prompt, the generated SQL, the engine’s answer, and a correctness judgment are shown.

Scenario 1

Prompt: “How many suppliers do we have and how many different products do they supply?”

SELECT COUNT(DISTINCT SupplierID) AS 'Number of Suppliers', COUNT(DISTINCT ProductID) AS 'Number of Products' FROM Supplier, Product;

Result: There are 10 suppliers and 10 products. ✅

Scenario 2

Prompt: “What is the inventory quantity for each product?” SELECT ProductID, Quantity FROM Inventory; Result: Product 1 has 100, Product 2 has 200, …, Product 10 has 1000. ✅

Scenario 3

Prompt: “Show monthly sales and inventory for each product.”

SELECT MONTH(sod.SalesOrderID) AS Month, p.ProductName, SUM(sod.Quantity) AS Sales, i.Quantity AS Inventory FROM SalesOrderDetail sod INNER JOIN Product p ON sod.ProductID = p.ProductID INNER JOIN Inventory i ON p.ProductID = i.ProductID GROUP BY Month, p.ProductName, i.InventoryID ORDER BY Month ASC LIMIT 10;

Result: Correct sales numbers but month grouping was off. ❌ (partial)

Scenario 4

Prompt: “Give monthly sales quantity and revenue.”

SELECT MONTH(OrderDate) AS Month, SUM(Quantity) AS SalesQuantity, SUM(Quantity * Price) AS SalesAmount FROM SalesOrder JOIN SalesOrderDetail ON SalesOrder.SalesOrderID = SalesOrderDetail.SalesOrderID GROUP BY MONTH(OrderDate) ORDER BY Month ASC LIMIT 10;

Result: Correct. ✅

Scenario 5

Prompt: “Show monthly sales, revenue, and profit.”

SELECT MONTH(OrderDate) AS Month, SUM(Quantity) AS Sales, SUM(Quantity * Price) AS Revenue, SUM(Quantity * Price) - SUM(Quantity * CostPrice) AS Profit FROM SalesOrder JOIN SalesOrderDetail ON SalesOrder.SalesOrderID = SalesOrderDetail.SalesOrderID JOIN Product ON SalesOrderDetail.ProductID = Product.ProductID GROUP BY MONTH(OrderDate) ORDER BY Month ASC LIMIT 10;

Result: Monthly sales, revenue, and profit values were correctly computed. ✅

Conclusion

LangChain successfully bridges local data sources with large language models, enabling high‑accuracy task completion. The experiment shows that ChatGPT can generate correct SQL and retrieve useful insights, though limitations remain such as token limits, occasional SQL errors, and data‑security considerations when connecting to real production databases.

SQLLangChainData AnalysisChatGPTMySQLAI integration
Alibaba Cloud Developer
Written by

Alibaba Cloud Developer

Alibaba's official tech channel, featuring all of its technology innovations.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.