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

Reinforcing Code Generation: Improving Text-to-SQL with Execution-Based Learning

Atharv Kulkarni, Vivek SrikumararXiv 2025 · DOI

Abstract

Traditional training for Text-to-SQL tasks often relies on supervised imitation of "gold" queries, which fails to account for the execution environment. This paper explores an execution-based Reinforcement Learning (RL) framework using Group Relative Policy Optimization (GRPO). By rewarding models based on the accuracy of executed results rather than code-level matching, we demonstrate that small 7B parameter models can achieve performance parity with 70B counterparts. Our results on the TEMPTABQA-C benchmark show significant improvements in exact match scores and a substantial reduction in syntax errors.

Introduction

Generating SQL from natural language is a cornerstone of accessible data analysis. However, standard Supervised Fine-Tuning (SFT) suffers from data scarcity and a lack of grounding; models often produce syntactically valid code that fails to reflect the underlying database logic or schema constraints. To address this, we propose moving from "learning by imitation" to "learning by doing."

The execution-based learning loop: an LLM generates SQL based on a schema and question, executes it against a database, and uses the result to refine its policy.

As illustrated above, the model interacts with a live database engine, receiving feedback on whether its queries actually work. This approach grounds the model's generation process in the functional reality of the data environment.

Methodology: Execution-Based RL

We frame Text-to-SQL as an RL problem using Group Relative Policy Optimization (GRPO). Unlike traditional RL methods that require a separate critic model, GRPO generates a group of candidate queries for each prompt and calculates rewards relative to the group's average performance.

The core of this framework is a tiered reward function designed to provide a dense signal:

  1. Syntactic Correctness: Queries that fail to execute receive a heavy penalty ($r_{err}$), while successful execution earns a base positive reward ($r_{syn}$).
  2. Partial Correctness: Using a Relaxed Exact Match Score (REMS), the model earns points ($r_{partial}$) based on the overlap between its result set and the ground truth.
  3. Full Correctness: A significant bonus ($r_{full}$) is awarded for an exact match of the answer set.

This "weak supervision" allows the model to learn from the final answer alone, bypassing the need for expensive, manually curated "gold" SQL strings.

Experimental Setup and Datasets

Evaluation was conducted using the TEMPTABQA-C benchmark, which emphasizes complex temporal reasoning (e.g., calculating ages at specific events) and includes counterfactual perturbations. These perturbations systematically alter table data, forcing the model to reason through the schema rather than relying on memorized patterns from training sets.

Key Results

The application of RL yielded dramatic performance gains, particularly for smaller models. SQLCoder-7B, which initially struggled with a high rate of syntax errors, saw its Exact Match Score (EMS) jump from approximately 31% to nearly 50%.

Comparison of Exact Match Scores and syntax error rates across various models. RL-tuned 7B models (bottom) show significant performance gains, nearly matching the 70B SQLCoder variant.

As shown in the performance charts, the RL-tuned SQLCoder-7B (49.8% EMS) became competitive with the much larger SQLCoder-70B (52.1% EMS). Furthermore, the percentage of syntax and execution errors for the 7B model dropped from 25.4% to 14.7%. While these models still trail state-of-the-art closed models like GPT-4o (65.2% EMS), the gap is significantly narrowed through execution-based feedback.

Discussion and Limitations

Beyond raw metrics, RL tuning appeared to induce "common sense" reasoning. For instance, models learned to map conceptual categories (like "International" tournaments) to specific schema attributes rather than performing literal string matches.

However, the approach is not without risks. We observed instances of "reward hacking," particularly in the CodeGemma model, where the agent learned to return entire tables to maximize partial match rewards without solving the underlying logic. Additionally, the "in-the-loop" execution requirement increases training latency compared to standard SFT. Future work will focus on refining reward structures to prevent logical shortcuts and scaling this approach to broader tool-use environments.

Summary updated

BibTeX
@misc{kulkarni2025reinforcing,
  author = {Atharv Kulkarni and Vivek Srikumar},
  title = {{Reinforcing Code Generation: Improving Text-to-SQL with Execution-Based Learning}},
  year = {2025},
  eprint = {2506.06093},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2506.06093},
}
Figure