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

SQLPrompt: In-Context Text-to-SQL with Minimal Labeled Data

Ruoxi Sun, Sercan Ö. Arik, Rajarishi Sinha, Hootan Nakhost, Hanjun Dai, Pengcheng Yin, Tomas Pfister

Abstract

Large Language Models (LLMs) have demonstrated significant potential in code generation, yet their performance in Text-to-SQL tasks often lags behind specialized, fine-tuned models. This paper presents SQLPrompt, a novel few-shot prompting framework designed to bridge this gap. By leveraging execution-based consistency—a method of voting on query results rather than raw code—and diversifying prompts across multiple styles and models, SQLPrompt achieves near state-of-the-art performance on the Spider benchmark without the need for expensive fine-tuning. Our results show that SQLPrompt (0-shot) achieves 76.6% execution accuracy, significantly outperforming standard LLM approaches and demonstrating that execution feedback is a critical signal for SQL generation.

1. Introduction

The ability to translate natural language questions into structured query language (SQL) is essential for democratizing data access. Historically, achieving high accuracy on complex benchmarks like Spider required fine-tuning models on thousands of domain-specific examples. While Large Language Models (LLMs) like GPT-4 and PaLM offer a "few-shot" alternative, they face two primary hurdles: extreme sensitivity to prompt phrasing (brittleness) and a tendency to generate syntactically valid but logically incorrect queries relative to a specific database schema.

SQLPrompt addresses these challenges by shifting the focus from the syntax of the generated code to the semantics of the execution result. Instead of treating SQL generation as a pure text-to-text task, SQLPrompt incorporates the database environment into the decoding process, ensuring that the final output is grounded in actual data retrieval.

2. The SQLPrompt Framework

The SQLPrompt framework is built upon three core methodological innovations designed to increase the robustness and accuracy of LLM-generated SQL.

2.1 Execution-based Consistency Decoding

Standard self-consistency methods typically involve generating multiple candidate answers and selecting the most frequent string. However, in SQL, multiple distinct queries can be semantically equivalent (e.g., varying in join order or alias naming). SQLPrompt executes all candidate queries against the target database and performs a majority vote on the execution results. This approach identifies a consensus based on what the code does rather than what it looks like, effectively grouping semantically identical queries.

2.2 Execution Error Filtering

Before the voting process, SQLPrompt implements an automated pruning step. Any generated SQL query that results in an execution error (e.g., referencing non-existent columns or syntax errors) is immediately discarded. This filters out "hallucinated" code and ensures that the voting pool consists only of valid, executable candidates.

2.3 MixPrompt and MixLLMs

To mitigate the brittleness of LLMs, SQLPrompt employs "MixPrompt," which utilizes a variety of prompt designs—ranging from concise schema descriptions to verbose, natural language explanations. By diversifying the input, the system triggers different reasoning paths within the model. Furthermore, the framework can aggregate outputs from different LLMs (MixLLMs), leveraging the collective strengths of multiple architectures to avoid common failure modes associated with a single model.

3. Experimental Setup

The framework was evaluated using the Spider benchmark, a large-scale, cross-domain Text-to-SQL dataset containing over 10,000 questions and 5,000 unique SQL queries across 200 databases. The evaluation focused on the "Dev split," comprising 1,034 samples across 20 databases. The primary metric used was Execution Accuracy (EX), which measures whether the generated query produces the correct result set when executed.

4. Results and Discussion

SQLPrompt demonstrates that sophisticated prompting strategies can rival fine-tuned specialists.

4.1 Performance Benchmarks

In a 0-shot configuration, SQLPrompt achieved an execution accuracy of 76.6%, notably higher than ChatGPT’s 70.1% on the same task. When provided with just four labeled examples (4-shot), the accuracy improved further to 77.1%. These results suggest that for many enterprise applications, the high cost of fine-tuning may no longer be a prerequisite for high-quality Text-to-SQL performance.

4.2 The Impact of Execution Feedback

Ablation studies highlight the necessity of execution-based consistency. Removing this component resulted in a 13.6% drop in performance, confirming that the execution result provides a significantly stronger signal for correctness than text-based similarity. This finding underscores the importance of "closing the loop" between code generation and execution.

5. Conclusion and Future Work

SQLPrompt provides a powerful, data-efficient alternative to fine-tuning for Text-to-SQL tasks. By prioritizing execution consistency and prompt diversity, it enables general-purpose LLMs to achieve high precision in structured data retrieval.

Despite its success, the framework introduces increased computational cost and latency, as it requires generating and executing multiple candidates (typically 32 or more) per question. Future research will explore "early stopping" mechanisms to reduce inference costs and investigate the application of execution-based filtering to other programming languages like Python or C++.

Summary updated

BibTeX
@inproceedings{sun2023sqlprompt,
  author = {Ruoxi Sun and Sercan Ö. Arik and Rajarishi Sinha and Hootan Nakhost and Hanjun Dai and Pengcheng Yin and Tomas Pfister},
  title = {{SQLPrompt: In-Context Text-to-SQL with Minimal Labeled Data}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2023},
  doi = {10.18653/v1/2023.findings-emnlp.39},
}
Figure