Query and Conquer: Execution-Guided SQL Generation

Lukasz Borchmann, Marek WydmucharXiv 2025 · DOI

Abstract

Large Language Models (LLMs) demonstrate significant potential in text-to-SQL tasks but often struggle with selecting the correct query from multiple candidates. We introduce an execution-guided self-consistency approach that shifts the selection process from syntactic comparison to behavioral analysis. By executing candidate queries and identifying the most consistent output, a 7B parameter model can achieve performance parity with leading reasoning models like OpenAI’s o1 and o3-mini while reducing inference costs by approximately 30x.

Introduction

While LLMs frequently generate correct SQL queries within a set of samples (pass@k), their first-attempt accuracy (pass@1) remains a bottleneck. Traditional self-consistency methods, which rely on majority voting of the generated text, are ineffective for SQL. This is because SQL is syntactically diverse; multiple distinct query strings can be semantically equivalent, producing identical result sets. Consequently, string-based voting fails to recognize these equivalent solutions.

Methodology: Execution-Guided Self-Consistency

To address the limitations of syntactic voting, we propose a three-step framework based on execution results:

  1. Sampling: The model generates a pool of candidate SQL queries for a given natural language prompt.
  2. Execution: Each candidate is executed against the target database to produce a data frame. In production environments, EXPLAIN plans can serve as an approximate version to reduce data retrieval overhead.
  3. Consistency Selection: We compare these data frames using a novel "recall of cells" similarity metric. The final output is the query that exhibits the highest accumulated similarity to all other candidates—the "most average" result.
Overview of the execution-guided self-consistency workflow.

Partial Executability with PipeSQL

The research also explores PipeSQL, a dialect where every prefix of a query (separated by |>) is a valid statement. This allows for consistency checks during the generation process, enabling the model to identify and correct errors in joins or filters before completing the query string.

Experimental Setup

The method was primarily evaluated on BIRD-SQL, currently the most challenging benchmark for text-to-SQL tasks. To test generalizability beyond SQL, the authors also applied the technique to Python coding benchmarks, including HumanEval+ and MBPP+.

Key Results and Insights

The core insight is that semantic equivalence in code is best measured by its output rather than its text. This "behavioral" consistency disproportionately benefits smaller, specialized models.

Accuracy vs. inference cost, showing the Qwen 7B model matching heavyweights at significantly lower cost.
  • Performance: Qwen 2.5 Coder 7B, utilizing 30 samples with execution guidance, achieved 54.8% accuracy on BIRD-SQL, surpassing OpenAI’s o1 (53.9%) and o3-mini (52.1%).
  • Efficiency: The 7B model achieves these results at roughly 1/30th the inference cost of the larger "heavy reasoner" models it competes with.
  • Generalization: On the MBPP+ Python benchmark, the method improved Llama 3.1 8B's accuracy by over 12 percentage points (from 55.3% to 68.0%).
  • Error Mitigation: Qualitative analysis indicates that execution guidance is particularly effective at filtering out queries with "schema linking" errors (hallucinating non-existent columns) and improper table joins.

Limitations and Future Work

While effective, the method introduces challenges regarding execution cost and latency. Running dozens of queries on large production databases can be resource-intensive. Future work may involve refining the use of EXPLAIN plans as cheaper proxies for full execution. Additionally, while PipeSQL shows promise, its adoption requires transpilation steps that could introduce new errors. Finally, models with very high baseline accuracy, such as GPT-4o, show diminishing returns compared to smaller models.

Conclusion

Execution-guided self-consistency provides a powerful, "plug-and-play" inference-time strategy to boost the performance of specialized small models. By focusing on what the code does rather than how it is written, we can achieve state-of-the-art results with significantly improved efficiency.

Summary updated

BibTeX
@misc{borchmann2025query,
  author = {Lukasz Borchmann and Marek Wydmuch},
  title = {{Query and Conquer: Execution-Guided SQL Generation}},
  year = {2025},
  eprint = {2503.24364},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2503.24364},
}
Figure