NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Structure-Guided Large Language Models for Text-to-SQL Generation

Qinggang Zhang, Hao Chen, Junnan Dong, Shengyuan Chen, Feiran Huang, Xiao Huang

Overview

Translating natural language into SQL queries is a critical step in making databases accessible to non-technical users. While Large Language Models (LLMs) have shown immense promise in this area, they often struggle with the rigid syntax of SQL and the complex, interrelated structures of real-world databases.

This paper introduces SGU-SQL, a novel framework that dramatically improves how LLMs translate natural language into SQL. By using graph neural networks to map user queries to database schemas and employing syntax trees to break down the SQL generation process, SGU-SQL achieves state-of-the-art accuracy and faster inference times on complex database benchmarks.

The Challenge of Text-to-SQL

Translating a user's natural language question (e.g., "Show me last quarter's sales performance") into a functional SQL query is notoriously difficult for three main reasons:

  1. Ambiguous User Intent: Natural language is inherently messy and context-dependent, whereas SQL requires absolute precision.
  2. Sophisticated Database Architectures: Real-world databases contain dozens of interrelated tables, often with non-intuitive naming conventions. LLMs struggle to deduce which specific tables and columns a user is implicitly referencing.
  3. Complex Syntax: SQL is a declarative language with strict grammatical rules.

To solve this, researchers often use "decomposition" prompting (like Chain-of-Thought), asking the LLM to think step-by-step. However, because SQL is highly structured and non-linear, standard step-by-step prompting often fails or even degrades performance. A new approach is needed to guide the LLM using the actual structural logic of both the database and the query itself.

Methods: The SGU-SQL Framework

The authors propose SGU-SQL (Structure GUided text-to-SQL), which tackles the generation problem in three distinct phases:

SGU-SQL Framework Architecture
  1. Graph Construction: Instead of feeding raw text directly to the LLM, the system builds two distinct graphs. The Query Graph maps out the grammatical and semantic dependencies of the user's question. The Schema Graph maps out the database structure, including tables, columns, primary keys, and foreign keys.
  2. Structure Linking via Dual Graph Encoding: SGU-SQL uses a Relational Graph Attention Network (RGAT) to bridge these two graphs. It mathematically learns which words in the user's query correspond to which specific tables and columns in the database, effectively grounding the natural language in the database schema.
  3. Syntax-based Decomposition: Instead of a generic "think step-by-step" prompt, SGU-SQL parses the user's English query into a linguistic syntax tree. It maps the nodes of this tree to specific SQL operations, breaking the complex query into smaller, manageable sub-tasks. The LLM is then prompted to generate the SQL incrementally, strictly following this syntax tree.

Key Insights

  • Chain-of-Thought (CoT) Can Hinder SQL Generation: The authors found that applying standard CoT prompting to models like PaLM-2 actually decreased performance compared to basic few-shot prompting. SQL's declarative nature simply does not map well to standard linear reasoning.
  • Syntax Trees Make Superior Prompts: Dynamically breaking down a prompt based on a sentence's actual grammatical tree works significantly better than using rigid, hard-coded decomposition steps (a limitation in previous state-of-the-art models like DIN-SQL).
  • Graphs Beat API Calls for Efficiency: By using a dedicated Graph Neural Network to handle "schema linking" (matching words to database columns), SGU-SQL avoids forcing the LLM to do this heavy lifting. This drastically reduces the number of expensive, time-consuming API calls to models like GPT-4.
  • Plug-and-Play Generalizability: The SGU-SQL prompting strategy is model-agnostic. When the authors swapped GPT-4 for PaLM-2, CodeX, or ChatGPT, all models saw significant performance boosts (up to 11%), proving the framework's broad versatility.

Key Results

The model was evaluated on highly respected benchmarks, including Spider (a standard cross-domain dataset) and BIRD (a massive, 33.4 GB benchmark designed to test models on messy, real-world data).

Performance comparison on Spider and BIRD datasets
  • State-of-the-Art Accuracy: SGU-SQL paired with GPT-4 achieved an impressive 87.95% Execution Accuracy on Spider and 61.80% on BIRD. This performance outpaces 32 different state-of-the-art baselines, including fine-tuned models and other advanced prompting frameworks.
  • Targeted Error Reduction: Compared to baselines, SGU-SQL reduced errors in notoriously difficult SQL components—such as JOIN statements and schema-linking—by approximately 33.5%.
  • Inference Speed: On the BIRD "Challenging" set, SGU-SQL required only 0.22 hours of inference time, making it nearly twice as fast as competing decomposition methods like MAC-SQL (0.36h) and DIN-SQL (0.39h).

Limitations and Future Work

While highly effective, the authors note a few limitations and exciting avenues for future research:

  • Static vs. Dynamic Databases: Currently, SGU-SQL is designed for read-only querying (static databases). Real-world applications require full CRUD (Create, Read, Update, Delete) capabilities, which is a planned future extension.
  • Smarter Example Selection: The framework currently relies on standard methods for picking few-shot examples. Selecting examples based on structural similarity rather than just semantic keyword matching could push accuracy even higher.
  • Broader Applications: The underlying logic of SGU-SQL isn't limited to SQL. This graph-and-syntax approach could be adapted to generate Cypher queries for graph databases or even to orchestrate complex API planning tasks.

Summary updated

BibTeX
@inproceedings{zhang2025structure,
  author = {Qinggang Zhang and Hao Chen and Junnan Dong and Shengyuan Chen and Feiran Huang and Xiao Huang},
  title = {{Structure-Guided Large Language Models for Text-to-SQL Generation}},
  booktitle = {International Conference on Machine Learning},
  year = {2025},
  url = {https://openreview.net/forum?id=gT8JSEFqaS},
}
Figure