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

Structure Guided Large Language Model for SQL Generation

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

TL;DR

SGU-SQL is a new framework that significantly improves how Large Language Models (LLMs) translate natural language into SQL queries. By explicitly mapping the structural relationships between a user's question and a database's schema, and then decomposing the query into syntax-aware subtasks, it achieves state-of-the-art performance on the challenging Spider and BIRD benchmarks.

What Problem Does This Paper Address?

While LLMs like GPT-4 are impressively good at writing code, the "Text-to-SQL" task remains a major hurdle. The authors identify three primary pain points:

  1. Ambiguous User Intent: Natural language is messy. A request like "show me last quarter's performance" requires a model to infer specific tables, metrics, and timeframes that aren't explicitly stated.
  2. Sophisticated Database Architecture: Real-world databases have complex schemas with hundreds of interrelated tables and non-intuitive naming conventions. LLMs often struggle to "link" a query term (e.g., "purchases") to the correct set of tables (e.g., Orders and OrderDetails).
  3. Complex SQL Syntax: SQL is a declarative language with strict rules. Missing a comma, using an incorrect JOIN condition, or misplacing a keyword leads to unexecutable or incorrect queries.

Existing "decomposition" methods try to break the task down, but they often use fixed patterns that don't adapt well to the unique syntactic structure of every individual query.

Methods

The SGU-SQL (Structure GUided text-to-SQL) framework introduces a multi-step pipeline to provide the LLM with better "guardrails":

  • Dual-Graph Encoding: Instead of treating the query and database schema as flat text, the framework builds two graphs. The Query Graph captures syntactic and semantic dependencies between words, while the Schema Graph represents tables, columns, and their relationships (like foreign keys).
  • Structure Linking: The model uses a Relational Graph Attention Network (RGAT) to learn how to align these two graphs. It identifies which specific database elements are relevant to which parts of the user's question, effectively bridging the semantic gap.
  • Syntax-based Decomposition: This is the core innovation. The framework parses the natural language query into a syntax tree. It then maps nodes in this tree to specific SQL operations (like SELECT or FILTER).
  • Structure-Decomposed Prompting: Rather than asking the LLM to write the whole query at once, SGU-SQL guides it through subtasks derived from the syntax tree. This ensures the final SQL query follows a logically coherent and syntactically correct structure.
Figure 1: The overall framework of SGU-SQL.

Datasets

The authors evaluated SGU-SQL on two major benchmarks:

  • Spider: A widely used cross-domain dataset featuring 200 databases and over 10,000 question-SQL pairs.
  • BIRD (Big Bench for Large-scale Database Grounded Text-to-SQL): A more recent and significantly more difficult benchmark. It features massive databases (33.4 GB total) and focuses on how database content (not just the schema) impacts query generation.

Key Insights and Ideas

  • Structure is a First-Class Citizen: The paper argues that LLMs shouldn't just "read" a schema; they need to understand its graph-like nature. Explicitly modeling these links reduces schema-linking errors by roughly 38%.
  • Dynamic Decomposition Beats Fixed Patterns: Previous state-of-the-art models like DIN-SQL use a fixed set of steps for decomposition. SGU-SQL's approach is dynamic—it lets the specific grammar of the user's question dictate how the task is broken down.
  • Reasoning Synergy: Interestingly, the framework provides the biggest boost to models that already have strong reasoning capabilities (like GPT-4). This suggests that structured guidance helps high-performing LLMs "focus" their reasoning power more effectively.
  • Efficiency via Single-Pass Generation: Unlike some methods that require multiple LLM calls for "self-correction" or "debugging," SGU-SQL aims for high accuracy in a single pass, reducing latency and API costs.

Key Results

SGU-SQL consistently outperformed a massive field of competitors, including 11 fine-tuned models and 14 in-context learning models.

  • Spider Benchmark: Achieved a top execution accuracy of 87.95%, surpassing previous leaders like SuperSQL (86.82%) and PURPLE (86.70%).
  • BIRD Benchmark: On this much harder dataset, SGU-SQL reached 61.80% execution accuracy, a significant jump over the next best model, SuperSQL (58.60%).
  • Error Reduction: The framework reduced total errors by approximately 33.5% compared to baseline prompting methods, with major improvements in handling complex JOIN and GROUP BY statements.
Table 1: The Execution Accuracy of text-to-SQL models on SPIDER. The best and second-best results in each column are highlighted in bold font and underlined. ✔and ✘represent that the case is applicable and not applicable, respectively.
Table 2: The Execution Accuracy and Exact Match Accuracy of text-to-SQL models on SPIDER and BIRD. The best and second-best results in each column are highlighted in bold font and underlined. NaN denotes that the result is not available.

Limitations and Future Work

While powerful, the authors note several areas for growth:

  • Dynamic Databases: The current model is optimized for static schemas. Real-world applications often involve "CRUD" operations (Create, Read, Update, Delete) where the database structure or content changes rapidly.
  • Example Selection: The few-shot examples used in prompts are currently selected based on keyword similarity. The authors suggest that selecting examples based on structural similarity (matching the syntax tree) could further boost performance.
  • Synthetic Data: To adapt to new, proprietary databases quickly, the authors propose developing template-based systems to generate synthetic training data, reducing the need for manual annotation.
  • Beyond SQL: The "structure-guided" philosophy could be extended to other graph-based query languages like Cypher.

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 Model for SQL Generation}},
  booktitle = {International Conference on Machine Learning},
  year = {2025},
  url = {https://openreview.net/forum?id=gT8JSEFqaS},
}
Figure