Text-to-SQL with LLMs

#text-to-sql #llms #natural language processing #sql generation #database querying #nlp #fine-tuning #supervised learning #few-shot learning #natural language understanding

1. Definition and Core Concepts

1.1 Definition and Core Concepts

Text-to-SQL is the task of converting natural language queries into structured SQL queries executable on a relational database. Large Language Models (LLMs) have revolutionized this domain by leveraging their pre-trained knowledge of both language syntax and database schemas. The core challenge lies in accurately mapping ambiguous natural language intents to precise SQL operations while respecting database constraints.

Key Components of Text-to-SQL Systems

A robust Text-to-SQL pipeline consists of three primary components:

Mathematical Formulation

Given a natural language query Q and database schema S, the task is to learn a mapping function f that maximizes the probability of generating the correct SQL query Y:

$$ f(Q, S) = \arg\max_Y P(Y|Q, S; \theta) $$

where θ represents the parameters of the LLM. The probability is typically factorized autoregressively:

$$ P(Y|Q, S) = \prod_{t=1}^T P(y_t|y_{

Schema Encoding Techniques

Modern approaches employ graph-based representations of database schemas, where nodes represent tables/columns and edges represent foreign key relationships. Let G = (V, E) be the schema graph, then a graph neural network computes embeddings for each node:

$$ h_v^{(l)} = \text{AGGREGATE}^{(l)}\left(\{h_u^{(l-1)}: u \in \mathcal{N}(v)\}\right) $$

where AGGREGATE is a permutation-invariant function (e.g., mean pooling) and l denotes the layer depth. These embeddings are concatenated with token embeddings during SQL generation.

Execution-Guided Decoding

To ensure generated queries are executable, state-of-the-art systems employ execution-guided beam search that:

  • Prunes beams containing syntactically invalid partial SQL
  • Rewards beams that produce non-empty results on database samples
  • Penalizes type mismatches (e.g., comparing strings to integers)

The scoring function during beam search incorporates both language model likelihood and database feedback:

$$ \text{score}(Y) = \lambda \log P(Y|Q, S) + (1-\lambda) \mathbb{1}_{\text{exec}}(Y) $$

where λ controls the trade-off and 𝟙exec indicates executability.

Definition and Core Concepts – Text-to-SQL with LLMs – Tutorial Diagram
Diagram Description: The diagram would show the graph-based schema encoding process with tables/columns as nodes and foreign keys as edges, illustrating how embeddings propagate through the graph neural network layers.

1.2 Key Challenges in Text-to-SQL Conversion

Semantic Parsing Ambiguity

Natural language queries often exhibit syntactic and semantic ambiguities that complicate SQL generation. For instance, the phrase "show me the top 5 customers by sales" requires resolving whether "top" refers to maximum value, ranking, or another metric. LLMs must disambiguate such constructs while preserving the intended logical meaning. This becomes particularly challenging with nested queries or implicit joins, where the model must infer table relationships without explicit schema references.

Schema Alignment Complexity

Text-to-SQL systems must map natural language references (e.g., "patient records") to precise database columns (e.g., medical_records.patient_id). Large schemas with hundreds of tables exacerbate this problem, as the model must:

Current approaches like schema-guided decoding or database embeddings only partially mitigate these issues, especially when facing unseen database schemas during inference.

Query Fidelity and Executability

Generated SQL must satisfy strict syntactic validity while accurately reflecting user intent. Common failure modes include:

$$ P(\text{valid SQL}|\text{query}) = \prod_{t=1}^T P(w_t|\mathbf{w}_{<t},\mathcal{S}) $$

where 𝒮 represents the database schema. Even minor errors (e.g., missing GROUP BY clauses when using aggregates) render queries non-executable. Evaluation metrics like execution accuracy (EX) and exact set match (EM) reveal that state-of-the-art models achieve only 60-75% fidelity on complex Spider benchmark queries.

Compositional Generalization

LLMs struggle with novel combinations of known SQL components. For example, a model trained on queries with separate WHERE and HAVING clauses may fail when encountering their conjunction. This mirrors the broader challenge of systematic generalization in neural networks, where the model's performance degrades on query structures not seen during training.

Domain Adaptation Overhead

Specialized domains (e.g., healthcare, finance) introduce terminology and query patterns absent from general-purpose training data. Fine-tuning requires expensive annotation of domain-specific (query, SQL) pairs, as the syntactic structure of medical queries like:

$$ \text{SELECT medication FROM prescriptions WHERE dosage > (SELECT AVG(dosage) FROM patient\_history)} $$

differs substantially from e-commerce or academic benchmarks. Few-shot prompting mitigates but doesn't eliminate this gap.

Performance Optimization

Production systems require generated queries to be computationally efficient. Naive translations often produce:

Current LLMs lack awareness of database execution plans, requiring separate optimization passes that may alter the original semantic intent.

Role of LLMs in Text-to-SQL

Architectural Foundations

Large Language Models (LLMs) like GPT-4, PaLM, and Codex leverage transformer-based architectures to map natural language queries to structured SQL commands. The key innovation lies in their ability to parse semantic intent and translate it into syntactically valid SQL through autoregressive token prediction. Given an input sequence x1, x2, ..., xn, the model computes the conditional probability distribution:

$$ P(y_t | y_{

where ht is the hidden state at position t, and Wo, bo are output projection parameters. The decoder generates SQL tokens autoregressively until a termination condition is met.

Schema-Aware Attention Mechanisms

State-of-the-art Text-to-SQL systems employ specialized attention layers that explicitly model database schema relationships. Given a database schema S = (T, C, F) (Tables, Columns, Foreign keys), the attention scores between natural language tokens qi and schema elements sj are computed as:

$$ \alpha_{ij} = \frac{\exp(f(q_i)^T g(s_j))}{\sum_{k=1}^{|S|} \exp(f(q_i)^T g(s_k))} $$

where f and g are learned linear transformations. This allows the model to ground linguistic references to actual database structures.

Execution-Guided Decoding

Advanced implementations like PICARD employ constrained beam search to reject invalid SQL partials during generation. The search space is restricted to:

$$ \mathcal{V}_{\text{valid}}^{(t)} \subseteq \mathcal{V}^{(t)} $$

where V(t) is the full vocabulary at step t, and Vvalid(t) contains only tokens that maintain syntactic validity when appended to the current partial SQL. This is enforced through:

  • SQL grammar finite state automata
  • Schema-aware type constraints
  • Referential integrity checks

Few-Shot Prompt Engineering

For in-context learning, optimal prompt templates include:

-- Schema: 
-- Table employees (id, name, department_id)
-- Table departments (id, name, budget)

-- Question: Show names of employees in Engineering
SELECT employees.name 
FROM employees 
JOIN departments ON employees.department_id = departments.id 
WHERE departments.name = 'Engineering';

The prompt demonstrates the required mapping from natural language to SQL while maintaining schema reference clarity. Temperature settings typically range from 0.3-0.7 to balance creativity and precision.

Fine-Tuning Strategies

When fine-tuning on domain-specific schemas, the loss function incorporates:

$$ \mathcal{L} = \lambda_1 \mathcal{L}_{\text{CE}} + \lambda_2 \mathcal{L}_{\text{EXEC}} $$

where LCE is standard cross-entropy and LEXEC is execution accuracy loss computed by comparing predicted and gold query results on sample databases. Typical batch sizes range 8-32 with learning rates of 1e-5 to 5e-5.

Role of LLMs in Text-to-SQL – Text-to-SQL with LLMs – Tutorial Diagram
Diagram Description: The diagram would show the transformer-based architecture with attention mechanisms between natural language tokens and database schema elements, illustrating how schema-aware attention weights are computed.

2. Natural Language Understanding (NLU) Module

2.1 Natural Language Understanding (NLU) Module

The NLU module in a Text-to-SQL system is responsible for parsing and interpreting natural language queries to extract structured semantic representations. Unlike traditional rule-based approaches, modern systems leverage large language models (LLMs) to perform this task with higher accuracy and generalization.

Semantic Parsing Architecture

The core of the NLU module is a semantic parser that maps natural language to an intermediate logical form. For Text-to-SQL, this typically involves:

Modern approaches use sequence-to-sequence models with attention mechanisms. Given an input query x, the model learns the conditional probability distribution:

$$ P(y|x) = \prod_{t=1}^{T} P(y_t | y_{<t}, x) $$

where y is the target SQL query and T is its length.

Fine-Tuning LLMs for NLU

Pre-trained LLMs like GPT-3.5 or LLaMA are fine-tuned on Text-to-SQL datasets using:

$$ \mathcal{L}(\theta) = -\sum_{(x,y)\in\mathcal{D}} \log P(y|x; \theta) $$

where θ represents the model parameters and D is the training dataset. Key techniques include:

Handling Complex Queries

For multi-hop or nested queries, the NLU module often employs:

The semantic parsing accuracy is typically measured using exact match (EM) and execution match (EX) metrics:

$$ EM = \mathbb{I}(y_{pred} = y_{gold}) $$ $$ EX = \mathbb{I}(R(y_{pred}) = R(y_{gold})) $$

where R denotes the database execution result.

Real-World Challenges

Practical deployments must handle:

Natural Language Understanding (NLU) Module – Text-to-SQL with LLMs – Tutorial Diagram
Diagram Description: The diagram would show the sequence-to-sequence model architecture with attention mechanisms, illustrating how input queries are transformed into intermediate logical forms and then SQL queries.

SQL Query Generation Module

The SQL Query Generation Module is the core component of a Text-to-SQL system, responsible for translating natural language input into syntactically and semantically correct SQL queries. This module leverages the capabilities of large language models (LLMs) to parse intent, infer database schema context, and generate executable SQL statements.

Architecture and Components

The module typically consists of three primary subcomponents:

Mathematical Formulation

Given a natural language query Q and database schema S, the task is to generate SQL Y that satisfies:

$$ P(Y|Q, S) = \prod_{t=1}^{T} P(y_t | y_{

where y_t represents the t-th token in the SQL query, generated autoregressively by the LLM. The probability distribution is conditioned on the input query Q, schema S, and previously generated tokens y_{.

Fine-Tuning Strategies

LLMs are typically fine-tuned on SQL generation tasks using:

  • Supervised Fine-Tuning (SFT): Trains on parallel (NL, SQL) pairs with cross-entropy loss.
  • Reinforcement Learning (RL): Optimizes for execution accuracy using reward signals from database feedback.
  • Schema-Guided Decoding: Constrains generation to valid SQL syntax via grammar-based beam search.

Execution-Guided Decoding

To improve correctness, modern systems employ execution-guided decoding, where candidate queries are validated against the database during generation. The scoring function incorporates both likelihood and execution feedback:

$$ \text{score}(Y) = \lambda \log P(Y|Q, S) + (1 - \lambda) \mathbb{I}(\text{exec}(Y, D) = \text{correct}) $$

where λ balances between model confidence and execution correctness, and exec(Y, D) verifies the query against database D.

Error Analysis and Refinement

Common failure modes include:

  • Schema Misalignment: Incorrect column/table references due to ambiguous entity resolution.
  • Complex Joins: Inferred join conditions may miss implicit relationships.
  • Nested Queries: Subqueries often require multi-step reasoning.

Iterative refinement techniques, such as self-correction loops or human-in-the-loop verification, mitigate these issues.

Case Study: Spider Benchmark Performance

State-of-the-art models achieve ~75% exact match accuracy on the Spider benchmark, with errors concentrated in:

  • Queries involving 3+ table joins (62% accuracy).
  • Aggregation with GROUP BY clauses (68% accuracy).
  • Nested queries with EXISTS/NOT EXISTS (57% accuracy).
SQL Query Generation Module – Text-to-SQL with LLMs – Tutorial Diagram
Diagram Description: The diagram would show the flow between the three primary subcomponents (Intent Parser, Schema Linker, SQL Generator) and their interactions with the database schema and natural language input.

2.3 Database Schema Integration

Effective Text-to-SQL generation requires the LLM to understand and reason about the database schema, which defines the structure of tables, columns, data types, and relationships. Schema integration involves encoding this metadata in a way that allows the model to map natural language queries to valid SQL operations.

Schema Representation Methods

Three primary approaches exist for representing database schemas to LLMs:

  • Linearized Schema: Flatten the schema into a text sequence (e.g., "TABLE users (id INT, name TEXT) TABLE orders (user_id INT, amount FLOAT)"). Simple but loses relational context.
  • Graph-Based Encoding: Represent tables as nodes and foreign keys as edges in a graph structure, processed using graph neural networks. Captures relationships but computationally expensive.
  • Hybrid Serialization: Combine linearized tables with explicit relationship annotations (e.g., "TABLE orders (user_id INT → users.id)"). Balances efficiency and expressiveness.

Schema-Aware Attention Mechanisms

Modern Text-to-SQL systems employ modified transformer architectures that treat schema elements as first-class tokens. The attention mechanism computes:

$$ \text{Attention}(Q,K,V) = \text{softmax}\left(\frac{QK^T}{\sqrt{d_k}} + M\right)V $$

where M is a bias matrix that enforces schema constraints. For example:

  • Set Mij = -∞ to prevent attention between incompatible columns
  • Set Mij = 0 for columns with valid joins

Foreign Key Handling

Explicit foreign key relationships significantly improve join prediction accuracy. The model learns to attend to related tables through:

$$ p(\text{join}|q,s) = \sigma(W_j[h_q;h_{t1};h_{t2}]) $$

where hq is the question embedding, ht1 and ht2 are table embeddings, and Wj is a learned projection matrix.

Type-Consistent Decoding

During SQL generation, the decoder constrains output tokens based on expected data types:

  • Column references must match declared types in the schema
  • Comparison operators filter based on operand types (e.g., ">" only for numeric columns)
  • Aggregate functions (SUM, AVG) limited to numeric columns

This is implemented through masked language modeling, where invalid tokens receive -∞ logits.

Practical Implementation

For a database with schema S, the input to the LLM becomes:

def format_input(question: str, schema: Schema) -> str:
    tables = "\n".join(f"TABLE {t.name} ({', '.join(f'{c.name} {c.type}' 
                      for c in t.columns)})" 
                      for t in schema.tables)
    fks = "\n".join(f"// {t1}.{c1} REFERENCES {t2}.{c2}" 
                   for t1,c1,t2,c2 in schema.foreign_keys)
    return f"{tables}\n{fks}\n\nQuestion: {question}\nSQL:"
Database Schema Integration – Text-to-SQL with LLMs – Tutorial Diagram
Diagram Description: The diagram would physically show the graph-based encoding of database schemas with tables as nodes and foreign keys as edges, contrasting it with linearized and hybrid representations.

3. Dataset Requirements and Preparation

Dataset Requirements and Preparation

Effective Text-to-SQL systems rely on high-quality datasets that accurately represent the complexity and diversity of real-world database queries. The dataset must include natural language questions, corresponding SQL queries, and the underlying database schema. Key considerations include:

Schema Representation

The database schema must be explicitly defined, including table names, column names, data types, primary/foreign key relationships, and constraints. A common format is JSON or a structured text representation that can be parsed by the model. For example:

{
  "tables": [
    {
      "name": "employees",
      "columns": [
        {"name": "id", "type": "INTEGER", "primary_key": true},
        {"name": "name", "type": "TEXT"},
        {"name": "department_id", "type": "INTEGER", "foreign_key": "departments.id"}
      ]
    }
  ]
}

Natural Language-SQL Pair Quality

Each natural language question must unambiguously map to a syntactically correct SQL query. The dataset should cover a broad range of SQL operations, including:

  • Basic SELECT statements with WHERE, GROUP BY, and ORDER BY clauses
  • JOIN operations across multiple tables
  • Subqueries and nested expressions
  • Aggregate functions (COUNT, SUM, AVG, etc.)
  • Complex operations like UNION, INTERSECT, and window functions

Database Complexity

The underlying databases should vary in size and complexity to ensure model generalization. Ideal datasets include:

  • Small databases (1-5 tables) for simple queries
  • Medium databases (5-15 tables) with multiple relationships
  • Large databases (15+ tables) representing enterprise-scale schemas

Data Distribution

The dataset should balance query types to prevent model bias. A well-distributed dataset might contain:

  • 50% single-table queries
  • 30% multi-table joins
  • 15% subqueries
  • 5% advanced operations (e.g., recursive queries)

Preprocessing Steps

Before training, datasets typically undergo:

  • Schema normalization: Standardizing table/column names while preserving semantics
  • Query canonicalization: Converting SQL to a consistent format (e.g., standardizing whitespace, keyword capitalization)
  • Token alignment: Mapping natural language tokens to SQL schema elements
$$ \text{Alignment Score} = \frac{\sum_{i=1}^{N} \mathbb{I}(\text{NL}_i \leftrightarrow \text{SQL}_i)}{N} $$

where N is the total number of alignable tokens and 𝕀 is the indicator function for correct alignments.

Popular Benchmark Datasets

Several standardized datasets are commonly used for evaluation:

  • Spider: Cross-domain complex queries with 200+ databases
  • WikiSQL: Large-scale single-table question-SQL pairs
  • ATIS: Flight booking domain with nested queries
  • GeoQuery: Geographical queries requiring complex joins

Data Augmentation Techniques

To improve model robustness, datasets can be enhanced through:

  • Paraphrasing: Generating semantically equivalent natural language variations
  • Schema perturbation: Creating synthetic variants of database schemas
  • Query decomposition: Breaking complex queries into simpler sub-queries
def augment_query(query, schema):
    # Apply random paraphrasing to natural language
    paraphrased = paraphrase_model.generate(query['nl'])
    
    # Perturb schema with synonym replacement
    perturbed_schema = replace_synonyms(schema)
    
    return {'nl': paraphrased, 'sql': query['sql'], 'schema': perturbed_schema}

3.2 Supervised vs. Few-Shot Learning Approaches

Supervised Learning for Text-to-SQL

Supervised learning approaches for Text-to-SQL rely on large annotated datasets where natural language questions are paired with their corresponding SQL queries. The model is trained to minimize a loss function that measures the discrepancy between predicted and ground-truth SQL queries. Given a dataset D = {(xi, yi)}i=1N, where xi is a natural language question and yi is the corresponding SQL query, the objective is to learn a mapping function fθ: x → y parameterized by θ.

$$ \mathcal{L}(\theta) = -\sum_{i=1}^{N} \log P(y_i | x_i; \theta) $$

State-of-the-art models like BRIDGE and RAT-SQL employ encoder-decoder architectures, where the encoder processes the question and database schema, and the decoder generates the SQL query autoregressively. These models achieve high accuracy but require extensive labeled data, which is costly to obtain.

Few-Shot Learning for Text-to-SQL

Few-shot learning leverages large language models (LLMs) like GPT-3.5 or GPT-4 to generate SQL queries with minimal task-specific training data. Instead of fine-tuning on labeled examples, the model is prompted with a few demonstrations (typically 2-5 examples) that illustrate the task. The LLM then infers the mapping from the prompt and generalizes to unseen questions.

Given a prompt P = {(x1, y1), ..., (xk, yk)} and a new question xnew, the model generates:

$$ y_{\text{new}} = \text{LLM}(P \oplus x_{\text{new}}) $$

where ⊕ denotes concatenation. Few-shot learning is particularly effective when labeled data is scarce, as it relies on the LLM's pre-existing knowledge of SQL and natural language patterns.

Trade-offs and Practical Considerations

Supervised learning excels when large annotated datasets are available, offering precise control over the model's behavior through fine-tuning. However, it suffers from:

  • High annotation costs for complex SQL queries.
  • Limited generalization to unseen database schemas.
  • Brittleness when faced with paraphrased or out-of-distribution questions.

Few-shot learning reduces dependency on labeled data but introduces challenges such as:

  • Prompt engineering sensitivity—performance varies significantly with the choice of demonstrations.
  • Higher computational cost at inference time due to the need for in-context learning.
  • Less predictable behavior compared to fine-tuned models.

Hybrid Approaches

Recent work explores hybrid methods that combine the strengths of both paradigms. For instance, SPIDER + GPT-3 uses supervised pre-training on the SPIDER dataset followed by few-shot adaptation to new domains. Another approach involves retrieval-augmented few-shot learning, where relevant demonstrations are dynamically retrieved from a corpus based on the input question.

$$ y_{\text{new}} = \text{LLM}(\text{Retrieve}(x_{\text{new}}) \oplus x_{\text{new}}) $$

This mitigates prompt engineering challenges by automating demonstration selection.

3.3 Evaluation Metrics for Text-to-SQL Models

Evaluating Text-to-SQL models requires specialized metrics that assess both syntactic correctness and semantic alignment with the intended query. Unlike traditional NLP tasks, where metrics like BLEU or ROUGE suffice, Text-to-SQL demands precise evaluation of SQL query structure, database execution accuracy, and logical equivalence.

Execution Accuracy (EX)

Execution Accuracy measures whether the generated SQL query produces the same result as the ground truth query when executed against the database. Given a database instance D, a predicted SQL query Qpred, and a ground truth query Qgt, EX is defined as:

$$ EX(Q_{pred}, Q_{gt}, D) = \begin{cases} 1 & \text{if } Q_{pred}(D) = Q_{gt}(D) \\ 0 & \text{otherwise} \end{cases} $$

While EX is straightforward, it has limitations: minor syntactic variations (e.g., reordered WHERE clauses) may yield identical results but be marked incorrect. Additionally, EX does not account for queries that are logically equivalent but syntactically different.

Exact Matching (EM)

Exact Matching evaluates whether the predicted SQL string matches the ground truth exactly, including whitespace and keyword casing. EM is stricter than EX but often too rigid, penalizing semantically equivalent queries with trivial differences.

$$ EM(Q_{pred}, Q_{gt}) = \begin{cases} 1 & \text{if } Q_{pred} \equiv Q_{gt} \\ 0 & \text{otherwise} \end{cases} $$

Component-Level Matching

To address the limitations of EX and EM, component-level matching decomposes SQL queries into logical components (e.g., SELECT clauses, JOIN conditions) and evaluates each independently. Common approaches include:

  • Clause-Level Accuracy: Measures correctness of individual SQL clauses (SELECT, WHERE, GROUP BY, etc.).
  • Precision/Recall/F1: Computes token-level overlap between predicted and ground truth queries, treating SQL as a sequence of tokens.

Test Suite Accuracy

Proposed by Zhong et al. (2020), Test Suite Accuracy evaluates queries against a set of database instances designed to test edge cases. A query passes only if it produces correct results across all test cases, ensuring robustness to database variations.

$$ TSA(Q_{pred}, Q_{gt}, \{D_i\}) = \begin{cases} 1 & \text{if } \forall D_i, Q_{pred}(D_i) = Q_{gt}(D_i) \\ 0 & \text{otherwise} \end{cases} $$

Human Evaluation

Despite automated metrics, human evaluation remains critical for assessing natural language alignment, readability, and real-world usability. Common criteria include:

  • Fluency: Does the SQL query follow standard syntax and conventions?
  • Faithfulness: Does the query correctly reflect the user's intent?
  • Efficiency: Is the query optimized for performance (e.g., proper indexing)?

Challenges and Trade-offs

No single metric captures all aspects of Text-to-SQL performance. Execution Accuracy favors pragmatism but overlooks syntactic nuances, while Exact Matching prioritizes form over function. Hybrid approaches, such as combining EX with component-level F1, often provide a more balanced assessment. Recent benchmarks like Spider and BIRD emphasize both execution correctness and query complexity, penalizing models for oversimplifying or hallucinating queries.

4. Setting Up the Environment

4.1 Setting Up the Environment

Prerequisites

Before configuring the environment for Text-to-SQL with LLMs, ensure the following dependencies are installed:

  • Python 3.8+ — Required for compatibility with major machine learning libraries.
  • CUDA Toolkit 11.7+ — Necessary for GPU acceleration if leveraging NVIDIA hardware.
  • PyTorch 2.0+ or TensorFlow 2.12+ — Core frameworks for fine-tuning and inference.
  • Hugging Face Transformers — Provides pre-trained LLMs like T5, GPT-3.5, or Codex.
  • SQLAlchemy or psycopg2 — For database connectivity and schema introspection.

Installation via Conda

For reproducible environments, use Conda to manage dependencies:

conda create -n text2sql python=3.9
conda activate text2sql
conda install pytorch torchvision cudatoolkit=11.7 -c pytorch
pip install transformers datasets sqlalchemy psycopg2-binary

Database Configuration

To enable schema-aware SQL generation, configure a database connection. For PostgreSQL:

from sqlalchemy import create_engine
engine = create_engine("postgresql://user:password@localhost:5432/mydb")
metadata = MetaData(bind=engine)
metadata.reflect()  # Auto-loads schema

LLM Selection and Initialization

Load a pre-trained LLM with Hugging Face's pipeline API. For a decoder-only model like GPT-3.5:

from transformers import pipeline
text2sql = pipeline(
  "text-generation",
  model="gpt-3.5-turbo",
  tokenizer="gpt-3.5-turbo",
  device=0 if torch.cuda.is_available() else -1
)

Environment Validation

Verify the setup by testing a simple Text-to-SQL query:

prompt = "Convert to SQL: Find all customers from New York"
schema_hint = "Tables: customers(id, name, city)"
response = text2sql(f"{schema_hint}\n{prompt}")
print(response[0]['generated_text'])

Performance Optimization

For latency-sensitive applications, enable these runtime optimizations:

  • Flash Attention — Install via pip install flash-attn for 2-4x speedup in autoregressive decoding.
  • ONNX Runtime — Quantize the model with optimum.onnxruntime for CPU deployment.
  • vLLM — Use continuous batching for high-throughput serving.

Using Pre-Trained Models (e.g., GPT-3, T5)

Pre-trained language models like GPT-3 and T5 have demonstrated remarkable capabilities in text-to-SQL tasks by leveraging their extensive knowledge of language structure and database schemas. These models excel at zero-shot or few-shot learning, requiring minimal fine-tuning to generate accurate SQL queries from natural language prompts.

Architectural Foundations

GPT-3 employs a decoder-only transformer architecture with 175 billion parameters, enabling it to generate coherent SQL queries through autoregressive prediction. The model processes input tokens sequentially, using self-attention mechanisms to capture long-range dependencies between natural language questions and database schema elements.

$$ P(y_t|y_{<t}, x) = \text{softmax}(W_o h_t) $$

where ht represents the hidden state at position t, and Wo is the output projection matrix. T5, in contrast, uses an encoder-decoder architecture that separately processes the input question and schema before generating the SQL output:

$$ P(y|x) = \prod_{t=1}^T P(y_t|y_{<t}, \text{Enc}(x)) $$

Prompt Engineering Strategies

Effective text-to-SQL conversion requires carefully constructed prompts that include:

  • Database schema definitions (tables, columns, relationships)
  • Example question-SQL pairs for few-shot learning
  • Explicit instructions about query formatting requirements

For GPT-3, a typical prompt structure might appear as:

Database schema:
Table employees: [id, name, department_id, salary]
Table departments: [id, name, location]

Example 1:
Question: "Find all employees in the sales department"
SQL: SELECT employees.name FROM employees JOIN departments ON employees.department_id = departments.id WHERE departments.name = 'sales'

Question: "List departments with average salary over 50000"

Fine-Tuning Approaches

While pre-trained models can perform zero-shot translation, fine-tuning on domain-specific datasets significantly improves accuracy. For T5, this involves:

  • Converting the text-to-SQL task into a text-to-text format
  • Adding special tokens to represent schema elements
  • Training with masked language modeling objectives on SQL patterns

The fine-tuning objective minimizes the negative log-likelihood:

$$ \mathcal{L} = -\sum_{(x,y)\in\mathcal{D}} \log P(y|x; \theta) $$

Performance Optimization

Several techniques enhance model performance for production deployments:

  • Schema-guided decoding: Constrain output tokens to valid SQL keywords and schema references
  • Intermediate representations: Generate abstract syntax trees before final SQL
  • Execution feedback: Use database errors to iteratively refine queries

Recent benchmarks on the Spider dataset show GPT-3 achieving 58% exact match accuracy in zero-shot settings, while fine-tuned T5 models reach 75% accuracy. Hybrid approaches that combine LLM generation with symbolic verification demonstrate even higher reliability for mission-critical applications.

4.3 Customizing Models for Domain-Specific SQL

Fine-tuning large language models (LLMs) for domain-specific Text-to-SQL tasks requires addressing schema-aware reasoning, query complexity, and domain lexicon adaptation. The process involves three key technical components: schema grounding, supervised fine-tuning (SFT), and retrieval-augmented generation (RAG).

Schema Grounding Techniques

Effective domain adaptation begins with schema grounding, where the model learns to map natural language queries to database-specific table and column references. The grounding loss Lg is computed as:

$$ L_g = -\sum_{i=1}^N \log P(y_i | x, \mathcal{S}) $$

where x is the natural language query, yi are the schema elements, and 𝒮 represents the database schema. For hierarchical schemas, we extend this with graph attention networks:

$$ \alpha_{ij} = \frac{\exp(\text{LeakyReLU}(a^T[Wh_i||Wh_j]))}{\sum_{k \in \mathcal{N}_i} \exp(\text{LeakyReLU}(a^T[Wh_i||Wh_k]))} $$

where hi represents schema node embeddings and 𝒩i denotes neighboring nodes.

Supervised Fine-Tuning Strategies

Domain-specific SFT requires carefully constructed datasets with:

  • Complex joins (3+ tables) covering 25-40% of examples
  • Nested subqueries in 15-20% of samples
  • Domain-specific functions (e.g., geospatial operations for GIS systems)

The training objective combines standard cross-entropy with execution-guided loss:

$$ L_{total} = \lambda_1 L_{CE} + \lambda_2 L_{exec} + \lambda_3 L_{schema} $$

where Lexec verifies SQL correctness through database execution and Lschema enforces type consistency.

Retrieval-Augmented Generation

For dynamic domains, implement a dual-encoder RAG system:


class SQLRetriever(nn.Module):
    def __init__(self, encoder):
        super().__init__()
        self.encoder = encoder
        
    def forward(self, query, schema_chunks):
        q_emb = self.encoder(query)
        s_embs = [self.encoder(chunk) for chunk in schema_chunks]
        scores = torch.matmul(q_emb, torch.stack(s_embs).T)
        return torch.topk(scores, k=3)
  

The retriever provides relevant schema context to the generator with <90ms latency, improving exact match accuracy by 18-22% on complex queries.

Domain-Specific Optimization

Specialized optimizations include:

  • Lexical adaptation: Subword tokenizer retraining on domain corpora reduces OOV rates by 30-50%
  • Type prediction heads: Auxiliary classifiers for SQL data types improve CAST operation accuracy
  • Constraint injection: Hardcoding primary/foreign key relationships reduces ill-formed joins by 40%

For temporal domains, add temporal reasoning modules that learn to handle date arithmetic and time-based aggregations through specialized attention mechanisms:

$$ \text{TA}(Q,K,V) = \text{softmax}\left(\frac{QK^T + M_{temporal}}{\sqrt{d_k}}\right)V $$

where Mtemporal encodes relative time intervals between query dates and database timestamps.

Customizing Models for Domain-Specific SQL – Text-to-SQL with LLMs – Tutorial Diagram
Diagram Description: The section describes complex schema grounding with graph attention networks and temporal attention mechanisms, which inherently involve spatial relationships between schema nodes and time-based interactions.

5. Enterprise Database Querying

5.1 Enterprise Database Querying

Enterprise databases often contain complex schemas with hundreds of tables, intricate relationships, and domain-specific constraints. Traditional Text-to-SQL systems struggle with such environments due to schema ambiguity, lack of contextual understanding, and the need for precise query generation. Large Language Models (LLMs) mitigate these challenges by leveraging their pretrained knowledge of database structures and ability to infer implicit relationships.

Schema-Aware Prompt Engineering

Effective Text-to-SQL in enterprise settings requires explicit schema integration into prompts. A well-structured prompt includes:

  • Table definitions (column names, data types, primary/foreign keys)
  • Relevant business rules (e.g., "Sales records older than 5 years are archived")
  • Example queries demonstrating the desired JOIN and WHERE patterns
$$ P(Q|S,T) = \prod_{i=1}^n P(q_i|S,T,q_{

where Q is the generated SQL query, S is the database schema, and T is the natural language text input. The autoregressive nature of LLMs allows them to condition each token qi on the schema and partial query.

Handling Complex Joins and Nested Queries

Enterprise queries frequently involve multi-table joins and subqueries. LLMs outperform template-based systems by:

  • Inferring implicit join paths through foreign key analysis
  • Generating optimized query structures based on table cardinality
  • Handling nested aggregations (e.g., monthly sales averages per region)
-- LLM-generated query joining 4 tables with subquery
SELECT d.department_name, 
       COUNT(e.employee_id) AS headcount,
       AVG(s.salary) AS avg_salary
FROM departments d
JOIN employees e ON d.department_id = e.department_id
JOIN salaries s ON e.employee_id = s.employee_id
WHERE s.effective_date IN (
  SELECT MAX(effective_date) 
  FROM salaries 
  GROUP BY employee_id
)
GROUP BY d.department_name
ORDER BY headcount DESC;

Performance Optimization Techniques

Enterprise deployments require query efficiency. Two key methods are:

Query Plan Guidance

Augmenting prompts with database-specific optimization hints:

  • Index availability
  • Common query patterns
  • Materialized view definitions

Execution-Time Constraints

Enforcing runtime limits through prompt engineering:

/* 
Execution constraints: 
- Use index: idx_customer_region
- Timeout: 5s 
- Max rows: 10,000 
*/
SELECT ...

Security and Access Control

Enterprise systems implement row-level security (RLS) and column masking. LLM prompts must incorporate:

  • User role definitions
  • Data governance policies
  • Query rewrite rules for compliance

For example, a prompt for financial analysts might include:

User context: 
- Role: "regional_sales_analyst" 
- Permissions: 
  - Tables: sales, products, regions 
  - Columns: 
    - sales: all except cost_price 
    - regions: only authorized territories

5.2 Interactive Data Exploration Tools

Interactive data exploration tools bridge the gap between natural language queries and structured database operations by providing real-time feedback, query refinement, and visualization capabilities. These tools leverage LLMs to interpret user intent while maintaining the precision required for accurate SQL generation.

Architecture of Interactive Text-to-SQL Systems

A robust interactive Text-to-SQL system typically consists of three core components:

  • Natural Language Interface: Processes raw user input through an LLM to extract semantic intent and entity relationships.
  • Query Refinement Engine: Implements iterative feedback loops where the system proposes query variants based on confidence scores and schema constraints.
  • Visualization Layer: Dynamically renders results in tabular, graphical, or hybrid formats to facilitate rapid pattern recognition.
$$ C_q = \frac{1}{n}\sum_{i=1}^n \text{sim}(e_i, s_j) $$

Where Cq represents the contextual alignment score between extracted entities ei and schema elements sj, with similarity measured through embedding cosine distance.

Query Disambiguation Techniques

When faced with ambiguous natural language queries, advanced systems employ:

  • Schema-Aware Attention: Modifies transformer self-attention weights to prioritize database schema tokens during SQL generation.
  • Interactive Clarification Dialogs: Generates targeted follow-up questions when confidence in JOIN paths or aggregation functions falls below threshold τ.
$$ \tau = \mu_{conf} - 2\sigma_{conf} $$

With μconf and σconf derived from the model's validation set performance metrics.

Visual Query Building

Modern implementations combine LLMs with graphical interfaces that:

  • Render ER diagrams with selectable entities
  • Animate query execution plans
  • Highlight data lineage through interactive result tables

This multimodal approach reduces cognitive load by 37% compared to pure text interfaces (measured through eye-tracking studies).

Performance Optimization

For latency-sensitive applications, systems implement:

  • Query Caching: Hashes semantic representations of frequent question patterns
  • Partial Evaluation: Returns approximate results during complex query formulation
  • Index Hints: Embeds database-specific optimization directives in generated SQL
$$ t_{response} = t_{parse} + \alpha t_{gen} + \beta t_{exec} $$

Where α and β are learned weights balancing generation time against execution time based on query complexity.

Interactive Data Exploration Tools – Text-to-SQL with LLMs – Tutorial Diagram
Diagram Description: The diagram would physically show the three core components (Natural Language Interface, Query Refinement Engine, Visualization Layer) with data flow arrows between them, plus the mathematical alignment score's role in the system.

5.3 Limitations and Edge Cases

Despite the impressive capabilities of large language models (LLMs) in generating SQL queries from natural language, several limitations and edge cases persist. These challenges arise from inherent model constraints, database schema complexities, and ambiguities in natural language interpretation.

Schema Complexity and Ambiguity

LLMs struggle with highly normalized database schemas where tables are split across multiple relations. For example, a query requiring joins across five or more tables often results in incorrect or inefficient SQL. The model may miss implicit join conditions or fail to recognize composite keys, leading to Cartesian products or incomplete results. Additionally, ambiguous column names (e.g., id, name) across tables can cause incorrect attribute references.

$$ P(\text{Correct Join} | \text{N Tables}) \propto e^{-\lambda N} $$

where λ represents the schema complexity factor, empirically observed to range between 0.2 and 0.5 for modern LLMs.

Temporal and Aggregation Queries

Temporal logic (e.g., "sales last quarter") and nested aggregations (e.g., "average of monthly maxima") frequently produce incorrect SQL. LLMs often misinterpret time intervals or apply aggregation functions in the wrong scope. For instance, the query "Find departments with above-average employee counts" might generate:

SELECT department 
FROM employees 
WHERE COUNT(*) > AVG(COUNT(*)) -- Invalid SQL: Window function required

instead of the correct window function implementation.

Out-of-Distribution Queries

LLMs exhibit poor performance on queries requiring:

  • Domain-specific knowledge (e.g., medical billing codes)
  • Non-standard SQL dialects (e.g., Snowflake's QUALIFY clause)
  • Proprietary functions (e.g., PostGIS spatial operations)

The performance degradation follows an inverse power-law relationship with training data rarity:

$$ \text{Accuracy} \approx \frac{1}{1 + (x/x_0)^k} $$

where x represents query rarity and x0, k are model-specific constants.

Security and Injection Risks

LLMs may generate unsafe SQL by:

  • Concatenating user input directly into queries
  • Omitting parameterization for dynamic values
  • Creating excessive permissions (e.g., GRANT ALL)

For example, the natural language prompt "Delete all users named John" might produce vulnerable code:

DELETE FROM users WHERE name = 'John'; -- No input sanitization

Performance Optimization Failures

Generated queries often lack:

  • Proper index utilization
  • Join order optimization
  • Partition pruning
  • Subquery flattening

Benchmarks show that LLM-generated SQL executes 3-10× slower than expert-written equivalents on complex analytical workloads. The performance gap widens with query complexity:

$$ \frac{T_{\text{LLM}}}{T_{\text{Expert}}} \approx 1 + \alpha J + \beta S^2 $$

where J is the number of joins, S is subquery depth, and α, β are database-dependent coefficients.

6. Data Privacy and Security

6.1 Data Privacy and Security

Privacy Risks in Text-to-SQL Systems

Large Language Models (LLMs) used in Text-to-SQL applications can inadvertently expose sensitive data through several attack vectors:

  • Prompt Injection: Malicious queries may trick the model into revealing unauthorized database contents.
  • Model Memorization: LLMs trained on private datasets may reproduce sensitive records verbatim.
  • SQL Injection: Poorly sanitized natural language inputs can generate exploitable SQL queries.

Differential Privacy for Query Generation

Applying differential privacy to the Text-to-SQL pipeline ensures that query outputs don't reveal individual database records. The privacy budget ε controls the noise injection:

$$ \mathcal{M}(D) = f(D) + \text{Laplace}\left(\frac{\Delta f}{\epsilon}\right) $$

Where Δf is the query's sensitivity (maximum change a single record can cause) and ε is the privacy parameter. For a COUNT query, Δf = 1, while for SUM queries, Δf equals the maximum possible value.

Secure Query Execution Architecture

A three-layer defense strategy mitigates risks:

  • Input Sanitization: Remove PII from natural language queries using NER models before LLM processing.
  • Query Validation: Verify generated SQL against schema-aware policies (e.g., SELECT only permitted columns).
  • Output Filtering: Apply differential privacy or k-anonymity to query results before display.

Policy-Based Access Control Example

Implement attribute-based access control (ABAC) for SQL generation:


-- Policy: Restrict access to salary data
CREATE POLICY salary_access ON employees
  USING (current_user IN (
    SELECT approver FROM access_controls 
    WHERE table_name = 'employees' AND column_name = 'salary'
  ));
  

Homomorphic Encryption Approaches

For maximum security, process encrypted queries using partially homomorphic encryption (PHE). Let E be an encryption function satisfying:

$$ E(a) \oplus E(b) = E(a + b) $$ $$ E(a) \otimes E(b) = E(a \times b) $$

This allows the LLM to generate SQL that operates on ciphertexts. Practical implementations use Paillier cryptosystem for additive homomorphism or TFHE for full homomorphism at higher computational cost.

Audit Logging Requirements

Maintain immutable logs of all Text-to-SQL interactions with:

  • Original natural language query
  • Generated SQL with execution plan
  • User identity and timestamp
  • Privacy budget consumption (for differential privacy)

Store logs cryptographically hashed using Merkle trees to prevent tampering:

$$ H_n = H(H_{n-1} || H(\text{log entry}_n)) $$
Data Privacy and Security – Text-to-SQL with LLMs – Tutorial Diagram
Diagram Description: The three-layer defense strategy for secure query execution would benefit from a visual representation to clearly show the sequential flow of input sanitization, query validation, and output filtering.

6.2 Bias Mitigation in Query Generation

Large language models (LLMs) trained for text-to-SQL tasks inherit biases from their training data, which manifest in generated queries as skewed representations, unfair filtering conditions, or exclusionary joins. These biases often stem from imbalanced datasets, societal stereotypes encoded in text corpora, or overrepresentation of certain database schemas. Mitigating such biases requires a multi-faceted approach combining data curation, model fine-tuning, and post-generation validation.

Sources of Bias in SQL Generation

Three primary bias categories affect text-to-SQL systems:

  • Lexical bias: Over-reliance on specific column names (e.g., associating "salary" with male employees) due to imbalanced training examples
  • Structural bias: Preferred JOIN patterns that systematically exclude minority-represented tables
  • Semantic bias: Implicit WHERE clause conditions reflecting stereotypes (e.g., filtering by gender when unspecified)

Quantifying Bias with Fairness Metrics

For a given query template T and sensitive attribute A (e.g., gender, race), measure the disparate impact ratio (DIR):

$$ DIR(T,A) = \frac{P(\text{query}|A=a)}{P(\text{query}|A=b)} $$

where a and b represent different attribute values. A DIR significantly deviating from 1 indicates bias. For JOIN operations, compute the table inclusion disparity:

$$ \Delta_J = \frac{1}{N}\sum_{i=1}^N \left(\mathbb{I}(T_i \in Q) - \mathbb{E}[\mathbb{I}(T_i \in Q)]\right) $$

where Ti are tables and Q is the generated query.

Mitigation Techniques

Data-Level Interventions

Augment training data with:

  • Counterfactual examples where sensitive attributes are swapped
  • Synthetic minority schema configurations using graph-based generation
  • Adversarial examples that force the model to consider edge cases

Model-Level Techniques

During fine-tuning:

  • Apply counterfactual logit pairing to minimize output differences between sensitive attribute variations
  • Use gradient reversal layers to prevent the model from learning biased correlations
  • Implement attention masking to reduce focus on stereotypical token combinations
$$ \mathcal{L}_{total} = \mathcal{L}_{SQL} + \lambda \mathbb{E}_{x,a,a'}[\|f(x,a) - f(x,a')\|_2^2] $$

where f(x,a) is the model output for input x with attribute a, and λ controls the debiasing strength.

Post-Hoc Validation

Deploy runtime checks:

  • Constraint-based verification against predefined fairness rules
  • Differential testing by generating multiple query variants
  • Execution plan analysis for disproportionate resource allocation

Case Study: Healthcare Database Queries

When generating SQL for patient records, an unmitigated model exhibited 23% higher probability of including BMI filters for female patients. After implementing counterfactual fine-tuning and attention masking, the disparity reduced to 4%, while maintaining 98% of original query accuracy on the Spider benchmark.

6.3 Transparency and Explainability

Large Language Models (LLMs) for Text-to-SQL face significant challenges in transparency and explainability due to their black-box nature. Unlike rule-based or template-driven SQL generation systems, LLMs generate queries through probabilistic inference, making it difficult to trace how specific SQL clauses are derived from natural language input. This opacity raises concerns in high-stakes applications like healthcare, finance, and legal systems, where incorrect or biased queries could have severe consequences.

Attention Mechanisms as Explanation Proxies

Transformer-based LLMs use multi-head attention mechanisms to weight input tokens when generating SQL. The attention weights can be visualized to approximate model "focus" during query generation. For a given input question Q and output SQL S, the cross-attention between tokens qi and sj is given by:

$$ A_{ij} = \text{softmax}\left(\frac{QK^T}{\sqrt{d_k}}\right)_{ij} $$

where Q and K are query and key matrices, and dk is the dimension of key vectors. Higher Aij values suggest stronger influence of question token i on SQL token j. However, attention patterns alone are insufficient for full explainability as they don't capture: (1) nonlinear interactions across layers, (2) the role of feed-forward networks, or (3) knowledge embedded in model parameters.

Counterfactual Explanations for SQL Generation

More robust explanations can be generated by systematically perturbing inputs and observing output changes. Given an input question Q that produces SQL S, we compute:

$$ \Delta S = \text{LLM}(Q \setminus \{q_i\}) - \text{LLM}(Q) $$

where Q \ {qi} denotes the question with token qi removed. Significant changes in ΔS indicate that qi was critical for certain SQL elements. This approach reveals dependencies but requires O(n) forward passes for a question with n tokens.

Intermediate Representation Tracing

Some architectures like PICARD or IRNet introduce intermediate symbolic representations between text and SQL. These act as explainable stepping stones:

  • Semantic Parsing: First converts text to a domain-specific logical form
  • Schema Linking: Explicitly maps entities to database columns
  • SQL Sketching: Generates a partial query structure filled by the LLM

For example, the question "Show departments with more than 50 employees" might first be parsed to COUNT(employees) > 50 → departments, making the translation to SQL SELECT department FROM ... WHERE COUNT(employees) > 50 more interpretable.

Uncertainty Quantification

LLMs should provide confidence estimates for generated SQL queries. Calibrated uncertainty measures help users assess reliability. For a query S with m tokens, the joint probability is:

$$ P(S|Q) = \prod_{j=1}^m P(s_j|s_{

Low-probability tokens or abrupt probability drops during beam search often indicate potential errors. Techniques like Monte Carlo dropout or deep ensembles can improve uncertainty estimation by sampling from multiple forward passes with different dropout masks.

Human-in-the-Loop Verification

Hybrid systems combine LLMs with human verifiable components:

  • Execution Feedback: Run preliminary queries on small data samples to check for errors
  • Natural Language Justification: Have the LLM generate plain-English explanations of its SQL
  • Differential Testing: Compare outputs against simpler rule-based translators for consistency

For instance, the system might append: "I selected 'salary > 100000' because the question mentioned 'high earners' and the database has a 'salary' column." This meta-explanation bridges the gap between neural decisions and human understanding.

Transparency and Explainability – Text-to-SQL with LLMs – Tutorial Diagram
Diagram Description: The diagram would show attention weight visualization between question tokens and SQL tokens, and counterfactual explanation flow with input perturbations.

7. Key Research Papers

7.1 Key Research Papers

  • PDF Analysis of Text-to-SQL Benchmarks: Limitations, Challenges and ... — 3 TEXT-TO-SQL DATASETS A text-to-SQL dataset is a set of NL/SQL query pairs defined over one or more databases. Text-to-SQL datasets play an integral role in the development and benchmarking of text-to-SQL sys-tems. Notably, early non-neural systems did not rely on common benchmarks [17]. WikiSQL and Spider are the first large-scale,
  • Evaluating and Enhancing LLMs for Multi-turn Text-to-SQL with Multiple ... — without mastering complex SQL knowledge. The advent of LLMs, with their remarkable capacity for following instruc-tions, has transformed the text-to-SQL domain. LLM-based methods have delivered remarkable outcomes across various text-to-SQL tasks [1], [2], while the assessment of their robustness is increasingly drawing attention. Current research
  • [2308.15363] Text-to-SQL Empowered by Large Language Models: A ... - ar5iv — In this paper, we will focus on enhancing LLMs' Text-to-SQL capabilities with supervised fine-tuning. It is worth noting that despite the extensive research on prompt engineering for Text-to-SQL, there is a scarcity of studies exploring the supervised fine-tuning of LLMs for Text-to-SQL (Sun et al., 2023), leaving this area as an open question.
  • PDF Synthesizing Text-to-SQL Data from Weak and Strong LLMs - ACL Anthology — text-to-SQL ability of open-source models through SFT remains an open challenge. A signicant bar-rier to this progress is the high cost of achieving text-to-SQL data, which relies on manual expert annotation. The generation of high-quality text-to-SQL ne-tuning data should consider two primary perspectives. First, the inclusion of diverse data
  • Evaluating and Enhancing LLMs for Multi-turn Text-to-SQL with Multiple ... — Existing studies have only sporadically addressed the effective assessment and handling of multi-type questions. Most text-to-SQL research focuses on achieving high accuracy for single-type or single-round user questions [4, 6, 12, 13], often overlooking the need to develop systems capable of multi-turn dialogues that handle a variety of question types [].
  • Research Paper Example: QUERY BRIDGE: A Text to SQL LLM Model — 3.1 Traditional Text-to-SQL Approaches. Earlier strategies for text-to-SQL conversion relied on template-based and rule-driven systems which required extensive manual configuration. Although effective for straightforward queries, these methods often failed when handling complex or variable database schemas (Agarwal, Rawat & Chauhan 2025).
  • gtgspot/gtgspot - GitHub — The data model is key-value, but many different kind of values are supported: Strings, Lists, Sets, Sorted Sets, Hashes, Streams, HyperLogLogs, Bi ... Mooler0410/LLMsPracticalGuide - A curated list of practical guide resources of LLMs (LLMs Tree, Examples, Papers) ... vanna-ai/vanna - 🤖 Chat with your SQL database 📊. Accurate Text-to-SQL ...
  • A Survey on Evaluation of Large Language Models — Transformers have revolutionized the field of NLP with their ability to handle sequential data efficiently, allowing for parallelization and capturing long-range dependencies in text. One key feature of LLMs is in-context learning , where the model is trained to generate text based on a given context or prompt. This enables LLMs to generate ...
  • RB-SQL: A Retrieval-based LLM Framework for Text-to-SQL — (a) An example of utilizing LLM to solve text-to-SQL task. (b) The diagrams of DPR model and our proposed RB-model. Compared with DPR model, RB-model expands the input from document to other data ...
  • PDF Generating Sql From Natural Language in Few-shot and Zero-shot Scenarios — to generate the correct answer. The paper concludes with the result being that all methods tested fail to consistently generate SQL since they all focus on limited-scope problems. In a more recent study [6], uses Natural language processing (NLP) techniques to con-vert natural language questions into SQL queries by introducing a QCNER approach ...

7.2 Open-Source Tools and Libraries

  • Open-SQL Framework: Enhancing Text-to-SQL on Open-source Large Language ... — View PDF HTML (experimental) Abstract: Despite the success of large language models (LLMs) in Text-to-SQL tasks, open-source LLMs encounter challenges in contextual understanding and response coherence. To tackle these issues, we present \ours, a systematic methodology tailored for Text-to-SQL with open-source LLMs. Our contributions include a comprehensive evaluation of open-source LLMs in ...
  • Top 14 text-to-sql Open-Source Projects - LibHunt — 7 2 334 9.4 Python End-to-End Local-First Text-to-SQL Pipelines ... What are some of the best open-source text-to-sql projects? This list will help you: # Project Stars; 1: vanna: 17,487: 2: WrenAI: 7,721: 3: sqlchat: 5,161: 4: ... LibHunt tracks mentions of software libraries on relevant social networks. Based on that data, you can find the ...
  • Text to SQL Queries with LLM? The Answer to WebDev Dreams - 13 Open ... — Text-to_SQL open-source Apps and Tools 1- Vanna. Vanna is a free and open-source Python RAG framework designed for easily generating SQL from text. It allows you to convert questions into dynamic SQL queries and retrieve relevant answers from any vector database.
  • GitHub - eosphoros-ai/Awesome-Text2SQL: Curated tutorials and resources ... — Text-to-SQL (or Text2SQL), as the name implies, is to convert text into SQL. A more academic definition is to convert natural language problems in the database field into structured query languages that can be executed in relational databases. Therefore, Text-to-SQL can also be abbreviated as ...
  • DataGpt-SQL-7B: An Open-Source Language Model for Text2SQL - arXiv.org — An example of this is the CodeS model series Li et al. , which is an open-source language model explicitly designed for text-to-SQL tasks. It achieves significant gains in SQL generation and natural language understanding through incremental pre-training. ... Synthesizing text-to-sql data from weak and strong llms. arXiv preprint arXiv:2408. ...
  • text-to-sql · GitHub Topics · GitHub — GitHub is where people build software. More than 150 million people use GitHub to discover, fork, and contribute to over 420 million projects. ... Accurate Text-to-SQL Generation via LLMs using RAG 🔄. agent sql database ai data ... Star 7.8k. Code Issues Pull requests Discussions 🤖 Open-source GenBI AI Agent that empowers data-driven ...
  • CodeS: Towards Building Open-source Language Models for Text-to-SQL — Language models have shown promising performance on the task of translating natural language questions into SQL queries (Text-to-SQL). However, most of the state-of-the-art (SOTA) approaches rely on powerful yet closed-source large language models (LLMs), such as ChatGPT and GPT-4, which may have the limitations of unclear model architectures, data privacy risks, and expensive inference ...
  • Rotational Labs | How to build a text-to-sql LLM application — With the rise of Large Language Models (LLMs), many businesses are starting to wonder if it might be possible to streamline with simple chat interfaces, which use a text-to-SQL language model on the back end to translate between the natural language questions of the user to valid SQL queries that can be directly executed against the database.
  • Training small open-source LLM for Text to SQL generation — Exploring open-source LLMs for SQL generation, achieving up to 84.1% accuracy with fine-tuning and advanced methods on the Spider dataset.
  • GitHub - vanna-ai/vanna: Chat with your SQL database . Accurate Text ... — Accurate Text-to-SQL Generation via LLMs using RAG 🔄. - vanna-ai/vanna. ... Software Development View all Explore. Learning Pathways Events & Webinars Ebooks & Whitepapers ... Vanna is an MIT-licensed open-source Python RAG (Retrieval-Augmented Generation) framework for SQL generation and related functionality. ...

7.3 Recommended Courses and Tutorials

  • Enhancing Text-to-SQL with Open-source LLMs - OpenReview — fine-tuned LLM that focuses on text-to-SQL, CodeS-15B[13], still lag-behind advanced in-context learning methods based on GPT-4o by 14%. This performance disparity underscores the limitations of current open-source LLMs in handling complex text-to-SQL tasks. To bridge this gap, exploring better text-to-SQL strategies with open-source LLMs is ...
  • FinSQL: Model-Agnostic LLMs-based Text-to-SQL Framework for Financial ... — Fortunately, Large Language Models (LLMs)-based Text-to-SQL can satisfy these requirements, and several LLMs-based Text-to-SQL methods have been proposed recently. However, existing state-of-the-art LLMs-based Text-to-SQL methods typically depend on OpenAI's APIs, such as GPT-3.5-turbo or GPT-4, which are expensive and have risks of ...
  • From Natural Language to SQL: Review of LLM-based Text-to-SQL Systems — Abstract. Since the onset of LLMs, translating natural language queries to structured SQL commands is assuming increasing. Unlike the previous reviews, this survey provides a comprehensive study of the evolution of LLM-based text-to-SQL systems, from early rule-based models to advanced LLM approaches, and how LLMs impacted this field.
  • State of Text2SQL 2024 - Prem — So, to make this whole text-to-SQL task more productive and focused, we should use different tools and create these agents to make the overall workflow more robust in nature. MAC-SQL is a recent framework designed where only model-based Text-to-s SQL methods fail after a certain point. This framework is designed to tackle the challenges LLM ...
  • GitHub - waltatgit/sqlcoder: transform text into sql using LLM — transform text into sql using LLM. Contribute to waltatgit/sqlcoder development by creating an account on GitHub. ... Defog's SQLCoder is a family of state-of-the-art LLMs for converting natural language questions to SQL queries. Interactive Demo | 🤗 HF Repo ... (best performance) pip install "sqlcoder[transformers]" If running on Apple ...
  • Natural Language to SQL Query using an Open Source LLM — Introduction. In the dynamic landscape of data utilization, the ability to effortlessly interact with databases is paramount. Traditionally, this interaction required a deep understanding of Structured Query Language (SQL), posing a barrier to entry for many users. However, the advent of Natural Language Processing (NLP) to SQL Query Engines has transformed this landscape, allowing users to ...
  • GitHub - explosion/spacy-llm: Integrating LLMs into structured NLP ... — Maybe you want to use a cheap text classification model to help you find the texts to summarize, or maybe you want to add a rule-based system to sanity check the output of the summary. These before-and-after tasks are much easier with a mature and well-thought-out library, which is exactly what spaCy provides.
  • GitHub - nomic-ai/gpt4all: GPT4All: Run Local LLMs on Any Device. Open ... — Nomic contributes to open source software like llama.cpp to make LLMs accessible and efficient for all. pip install gpt4all from gpt4all import GPT4All model = GPT4All ( "Meta-Llama-3-8B-Instruct.Q4_0.gguf" ) # downloads / loads a 4.66GB LLM with model . chat_session (): print ( model . generate ( "How can I run LLMs efficiently on my laptop ...
  • The Top 10 Open Source LLMs: 2025 Edition - Scribble Data — Discover top 10 open-source LLMs like GPT-NeoX, BERT, Falcon-180B, providing cutting-edge language models for diverse applications. ... CodeGen's training spanned The Pile (English text), BigQuery (multilingual data), and BigPython (Python code). ... Best Practices for Insurers. Underwriting is the ground zero of group benefits. The place ...