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

Structure Guided Large Language Model for SQL Generation

Qinggang Zhang, Junnan Dong, Hao Chen, Wentao Li, Feiran Huang, Xiao Huang · DOI

Abstract

Large Language Models (LLMs) have shown promise in natural language processing, yet they often struggle with the structural and logical rigors of Text-to-SQL generation. This paper presents SGU-SQL (Structure GUided text-to-SQL), a framework that leverages graph-based representations and syntax-tree decomposition to bridge the gap between natural language intent and complex database schemas. SGU-SQL achieves state-of-the-art results on the Spider and BIRD benchmarks, demonstrating significant improvements in handling "extra hard" queries and complex schema-linking tasks.

Introduction

The conversion of natural language questions into executable SQL queries, known as Text-to-SQL, is a critical task for democratizing data access. Despite the capabilities of LLMs, three primary hurdles persist:

  1. Ambiguous Intent: Natural language often lacks the precision required for database operations, requiring models to infer implicit constraints.
  2. Schema Complexity: Real-world databases with numerous tables and intricate foreign-key relationships can overwhelm standard prompting techniques.
  3. Strict Syntax: SQL's hierarchical and declarative nature is poorly served by linear reasoning chains like standard Chain-of-Thought (CoT).

Methodology: SGU-SQL Framework

SGU-SQL moves beyond simple text-to-text translation by incorporating structural priors into the LLM's reasoning process. The architecture is built upon two core components:

Dual Graph Encoding

To resolve schema ambiguity, SGU-SQL constructs a "Query-Schema Graph." It employs a Relational Graph Attention Network (RGAT) to model the dependencies between query tokens and database elements (tables and columns). This "Structure Linking" process ensures that the model accurately maps natural language entities to their corresponding database schema components.

Syntax-based Decomposition

Unlike generic decomposition methods, SGU-SQL utilizes a context-free grammar to generate a syntax tree of the target query. The generation task is then broken down into sub-tasks aligned with specific SQL clauses (e.g., SELECT, FILTER, JOIN). This hierarchical approach guides the LLM to construct queries incrementally, ensuring syntactic validity and logical coherence.

The SGU-SQL architecture, illustrating the flow from user query and schema to graph encoding, syntax-based decomposition, and final prompt generation.

Experimental Evaluation

The framework was evaluated on two prominent benchmarks:

  • Spider: The standard cross-domain dataset focusing on complex SQL structures.
  • BIRD (Big Bench for Large-scale Database Grounded Text-to-SQL): A challenging dataset featuring large-scale databases and queries requiring domain-specific reasoning.

Results and Discussion

SGU-SQL demonstrates superior performance across both benchmarks, setting new state-of-the-art (SOTA) records.

Comparative performance of SGU-SQL against various finetuning-based, structure learning, and in-context learning methods on Spider and BIRD benchmarks.

Key Findings:

  • State-of-the-Art Accuracy: Using GPT-4, SGU-SQL achieved an execution accuracy of 87.95% on Spider and 61.80% on BIRD, significantly outperforming previous leaders like DIN-SQL and DAIL-SQL.
  • Model Agnostic Gains: The framework consistently improved performance across various LLMs, including ChatGPT, Llama-2, and CodeLlama, highlighting its versatility.
  • Efficiency and Robustness: By providing precise structural guidance, SGU-SQL reduced the need for iterative self-correction, resulting in a 40% faster inference time on complex query sets compared to DIN-SQL. It also achieved a 33.5% reduction in total errors, particularly in complex JOIN operations.

Conclusion and Future Directions

SGU-SQL proves that explicit structural modeling is essential for high-fidelity Text-to-SQL generation. By prioritizing the topology of databases and the hierarchy of SQL syntax, the framework overcomes the limitations of flat-text prompting. Future research will focus on adapting SGU-SQL for dynamic, frequently changing database schemas and exploring structure-aware example selection to further refine few-shot prompting performance.

Summary updated

BibTeX
@inproceedings{zhang2025structure,
  author = {Qinggang Zhang and Junnan Dong and Hao Chen and Wentao Li 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