A Survey of NL2SQL with Large Language Models: Where are we, and where are we going?

Xinyu Liu, Shuyu Shen, Boyan Li, Peixian Ma, Runzhi Jiang, Yuyu Luo, Yuxin Zhang, Ju Fan, Guoliang Li, Nan TangarXiv 2024 · arXiv · OpenReview

Abstract

Translating users' natural language queries (NL) into SQL queries (i.e., NL2SQL) can significantly reduce barriers to accessing relational databases. The performance of NL2SQL has been greatly enhanced with the emergence of Large Language Models (LLMs). This survey provides a comprehensive review of NL2SQL techniques powered by LLMs, covering its entire lifecycle: (1) Model translation techniques; (2) Data collection and synthesis; (3) Multi-angle evaluation; and (4) Error analysis. We discuss research challenges and open problems in the LLM era.

Introduction: The Challenge of Democratizing Data

Relational databases are the backbone of modern information systems, yet accessing them remains gated behind the technical requirement of SQL proficiency. Text-to-SQL (NL2SQL) aims to democratize data access by allowing users to query databases using natural language. However, this task faces significant hurdles: natural language ambiguity, extreme schema complexity in real-world databases, and the "mapping gap" between free-form intent and formal SQL constraints.

The Evolution of Text-to-SQL Solutions

The field has transitioned through several distinct eras, moving from rigid rule-based systems to the flexible, emergent capabilities of Large Language Models.

The evolution of Text-to-SQL solutions from statistical methods to Large Language Models, categorized by challenge levels and target users.

As shown in the evolution timeline, the complexity of challenges has scaled from simple single-table queries to real-world databases requiring multi-turn dialogues and domain-specific knowledge. While early statistical and neural models required expert intervention, the LLM era has opened the door for novice users, albeit with new requirements for advanced SQL feature support and efficient generation.

The NL2SQL Lifecycle: A Modular Framework

Modern NL2SQL research is best understood through a modular lifecycle that spans from model development to rigorous error analysis.

The comprehensive lifecycle of Text-to-SQL, including model evolution, data synthesis, evaluation frameworks, and error analysis.

1. Modeling and Pipeline

The translation process typically follows a three-stage pipeline:

  • Pre-Processing: Focuses on schema linking and database content retrieval. In the LLM era, Schema Pruning is critical to fit complex database structures into limited context windows.
  • Translation: The core engine. This involves either In-Context Learning (prompt engineering with models like GPT-4) or Fine-tuning specialized models (e.g., CodeS). Advanced decoding strategies like PICARD help enforce SQL grammar during generation.
  • Post-Processing: Refines output through execution-guided correction and self-consistency checks.

2. Data and Benchmarks

Benchmarks have evolved from simple sets like WikiSQL to complex, cross-domain standards like Spider. Newer datasets like BIRD emphasize execution efficiency, while AmbiQT targets the resolution of natural language ambiguity.

3. Evaluation and Error Analysis

Evaluation has moved beyond simple execution accuracy to include metrics like the Valid Efficiency Score (VES). Detailed error analysis reveals that the majority of failures occur in SELECT (25.5%) and GROUP BY (16.6%) clauses, often stemming from a failure in "Database Grounding"—understanding the specific quirks of an unfamiliar schema.

Key Insights and Results

  • The Bottleneck Shift: LLMs excel at language understanding but struggle with grounding queries in specific, massive database schemas.
  • Efficiency Trade-offs: LLM-based methods (e.g., DIN-SQL) can be significantly more accurate but suffer from 5x higher latency and 10x higher token consumption compared to optimized Pre-trained Language Models (PLMs).
  • Performance Gap: While accuracy on standard benchmarks is high (~80%), it drops precipitously to ~50% for "Extra Hard" cases involving nested queries and complex joins.

Future Directions

The survey identifies several frontiers:

  • Open-Domain NL2SQL: Querying across multiple, heterogeneous data sources.
  • Cost-Effective Routing: Using small models for simple queries and escalating to LLMs only when necessary.
  • Trustworthiness: Developing explainable AI tools that allow non-technical users to verify the logic behind a generated SQL query.
  • Adaptive Synthesis: Automatically generating training data to target specific model weaknesses.

Summary updated

Abstract

Translating users' natural language queries (NL) into SQL queries (i.e., NL2SQL) can significantly reduce barriers to accessing relational databases and support various commercial applications. The performance of NL2SQL has been greatly enhanced with the emergence of Large Language Models (LLMs). In this survey, we provide a comprehensive review of NL2SQL techniques powered by LLMs, covering its entire lifecycle from the following four aspects: (1) Model: NL2SQL translation techniques that tackle not only NL ambiguity and under-specification, but also properly map NL with database schema and instances; (2) Data: From the collection of training data, data synthesis due to training data scarcity, to NL2SQL benchmarks; (3) Evaluation: Evaluating NL2SQL methods from multiple angles using different metrics and granularities; and (4) Error Analysis: analyzing NL2SQL errors to find the root cause and guiding NL2SQL models to evolve. Moreover, we provide a rule of thumb for developing NL2SQL solutions. Finally, we discuss the research challenges and open problems of NL2SQL in the LLMs era.
BibTeX
@misc{liu2024survey,
  author = {Xinyu Liu and Shuyu Shen and Boyan Li and Peixian Ma and Runzhi Jiang and Yuyu Luo and Yuxin Zhang and Ju Fan and Guoliang Li and Nan Tang},
  title = {{A Survey of NL2SQL with Large Language Models: Where are we, and where are we going?}},
  year = {2024},
  eprint = {2408.05109},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2408.05109},
}
Figure