The Problem

Business teams need data to make decisions, but 90% can't write SQL. They rely on overburdened data analysts to run basic queries, creating bottlenecks. A simple question like "What were last month's sales?" can take days to answer due to ticket queues.

Existing BI tools require training, complex dashboards, and pre-built reports. Ad-hoc questions still require SQL knowledge. Non-technical employees are locked out of their own company's data, slowing down decision-making and innovation.

The Solution

Ask-Data uses LangChain SQL Agents and GPT-4 to convert natural language questions into safe, optimized SQL queries. Users type questions in plain English, and the system translates them to SQL, executes the query (read-only), and returns formatted results—all in under 2 seconds.

sql_agent.py
from langchain.agents import create_sql_agent
from langchain.agents.agent_toolkits import SQLDatabaseToolkit
from langchain.sql_database import SQLDatabase
from langchain.llms import OpenAI

class NaturalLanguageSQL:
    def __init__(self, db_uri: str):
        # Connect to read-only database replica
        self.db = SQLDatabase.from_uri(
            db_uri,
            read_only=True,  # Prevent write operations
            sample_rows_in_table_info=3
        )
        
        # Create SQL toolkit with schema info
        self.toolkit = SQLDatabaseToolkit(
            db=self.db,
            llm=OpenAI(temperature=0)
        )
        
        # Create agent with built-in query validation
        self.agent = create_sql_agent(
            llm=OpenAI(temperature=0, model="gpt-4"),
            toolkit=self.toolkit,
            verbose=True,
            max_iterations=10
        )
    
    def query(self, natural_language_question: str):
        """Convert natural language to SQL and execute"""
        try:
            # Agent automatically:
            # 1. Understands table schema
            # 2. Generates SQL query
            # 3. Validates syntax
            # 4. Executes query
            # 5. Returns results
            result = self.agent.run(natural_language_question)
            
            return {
                "success": True,
                "sql": self.extract_sql(result),
                "data": self.format_results(result),
                "execution_time": self.get_timing()
            }
        except Exception as e:
            return {
                "success": False,
                "error": "Could not generate valid SQL for this question"
            }

The system maintains a catalog of table schemas and business definitions, ensuring queries reference the correct columns. All queries are executed in read-only mode against database replicas, preventing accidental data modification. Query results are cached for 5 minutes to reduce database load.

Key Features

  • Zero SQL Required: Users ask questions in natural language—no training needed
  • Schema-Aware: Understands table relationships, joins, and business logic automatically
  • Query Validation: Validates SQL syntax and prevents expensive queries (timeout limits, row caps)
  • Read-Only Security: All queries run against replicas with SELECT-only permissions
  • Fast Results: Returns data in under 2 seconds with automatic caching
  • Export Ready: One-click export to CSV, Excel, or visualization tools

Impact

A 200-person SaaS company deployed Ask-Data and saw data analyst tickets drop by 70% in the first month. Non-technical teams (sales, marketing, ops) ran 500+ queries independently, uncovering insights that would have required analyst support.

Decision-making speed increased dramatically—executives could answer their own questions in real-time during meetings rather than waiting days for reports. The data team shifted focus from running queries to building strategic models and dashboards.