---
title: 'CogniSQL-R1-Zero: Execution-Aligned Text-to-SQL'
url: https://www.emergentmind.com/topics/cognisql-r1-zero
type: topic
---

# CogniSQL-R1-Zero: Execution-Aligned Text-to-SQL

CogniSQL-R1-Zero is a Text-to-SQL reinforcement learning framework and model for translating natural-language questions into executable SQL over relational database schemas. Its defining design choice is to optimize execution correctness directly, using a lightweight reward centered on execution success and output-format compliance rather than intermediate supervision, hybrid multi-stage pipelines, or complex reward shaping. Built on **Qwen/Qwen2.5-Coder-7B-Instruct**, it is presented as a **lightweight, execution-aligned RL model** and reports **59.97% execution accuracy** on **BIRD-dev** in single-sample generation, with **69.68%** under best-of-6 test-time scaling [2507.06013].

## 1. Task definition and motivation

The problem setting is **Text-to-SQL**: mapping a natural-language question to an SQL query that is not merely syntactically valid, but **semantically correct and executable** against the target database. The work emphasizes that large language models often generate SQL that appears plausible yet fails at execution time, especially under **complex schema layouts**, **multi-join queries**, **ambiguous natural-language questions**, and cases where **database-specific schema semantics** matter. The main benchmark is **BIRD-SQL**, whose training split contains **9,428 training examples** spanning **95 databases** and **37 domains** [2507.06013].

The motivation for a lightweight RL approach is framed against four families of prior methods: **supervised**, **instruction-tuned**, **hybrid multi-stage pipelines**, and **reward-shaped** systems. The reported concerns are **high compute cost**, **inference latency** from multi-stage reasoning or multi-agent systems, **fragile supervision** from intermediate reasoning labels, **poor generalization** when SFT overfits to a reasoning style, and **reward engineering complexity** with associated reward-hacking risks. Several internal attempts are described before the final system: a hybrid reasoning-plus-generation pipeline, an agentic multi-agent pipeline, supervised finetuning on distilled 32B reasoning traces, self-generated-data SFT, and finally RL fine-tuning. The paper’s central thesis is that if the task objective is execution correctness, the model should optimize **execution success directly**.

This framing also clarifies what CogniSQL-R1-Zero is not. It is not primarily a reasoning-trace distillation system, nor an instruction-following SQL generator, nor a multi-model orchestration stack. It replaces a hybrid pipeline that used **LLaMA 3.1 8B** as a reasoning engine and **CodeStral 22B** for SQL generation—reported at about **75% execution accuracy on 200 sampled queries**—with **a single 7B model** trained end-to-end with RL.

## 2. Model backbone, prompting, and systems configuration

CogniSQL-R1-Zero is based on **Qwen/Qwen2.5-Coder-7B-Instruct**, a **7B-parameter transformer decoder**. Adaptation is done with **PEFT / low-rank adapters** rather than full fine-tuning, reducing memory usage. Training is run on **4 NVIDIA A100 GPUs**, each with **40 GB VRAM**, using **DeepSpeed ZeRO-2**, **gradient accumulation**, and **VLLM** for faster multi-sample evaluation [2507.06013].

The reported efficiency numbers are explicit. **ZeRO-2** gives about a **2.96× speedup** relative to a single-GPU setup for an epoch: **single A100: ~18h 22m 35s** versus **4× A100 with ZeRO-2: ~6h 12m 50s**. This systems profile is integral to the paper’s claim that strong Text-to-SQL performance can be obtained under comparatively modest compute.

Each training prompt includes four elements: **DDL statements with table/column descriptions**, **external knowledge**, the **natural-language question**, and an enforced output format with the tags:

```text
<reasoning> ... </reasoning>
<answer> ... </answer>
```

Prompts longer than **3000 tokens** are filtered out to fit a **5000-token context window**. The structured prompt is intended to reduce ambiguity and to help the model associate schema elements with the question and the required output format. The paper later reports that prompt design materially affects both convergence and final accuracy, and that removing `<reasoning>` tags weakens reward propagation.

## 3. Reinforcement learning formulation and reward design

The optimization method is **Group Relative Policy Optimization (GRPO)**. For each prompt, the model samples a group of candidate SQL outputs and uses the best candidate’s reward to drive policy improvement. The reported hyperparameters are **group size** $G=6$, **sampling temperature** $T=0.9$, **learning rate** $\eta=10^{-5}$, **KL penalty** $\beta=0.001$, and **clip ratio** $\epsilon=0.2$ [2507.06013].

The reward design contains four components. The first is the **format reward** $R_f$, which requires exact compliance with the prescribed tag structure:

$$
R_f(o)=
\begin{cases}
1, & \text{if } o \text{ conforms to } \langle\text{reasoning}\rangle\langle/\text{reasoning}\rangle\langle\text{answer}\rangle\langle/\text{answer}\rangle,\\
0, & \text{otherwise}.
\end{cases}
$$

The second is the **soft format reward** $R_{sf}$, which gives partial credit when the basic tag structure matches a regex:

$$
R_{sf}(o)=
\begin{cases}
0.5, & \text{if basic tag structure matches regex},\\
0, & \text{otherwise}.
\end{cases}
$$

The third and dominant term is the **correctness reward** $R_c$, defined by executing the generated SQL and comparing its result to the ground truth:

$$
R_c(o)=
\begin{cases}
2, & \text{if } \mathrm{Exec}(o)=\mathrm{Exec}(q_{\mathrm{gt}}),\\
0, & \text{otherwise}.
\end{cases}
$$

Execution errors or timeouts also receive zero reward. The fourth term is a **length reward** $R_l$, which penalizes overly long outputs:

$$
R_l(o)=
\begin{cases}
-0.5, & \text{if } |o|>k,\\
0, & \text{otherwise}.
\end{cases}
$$

The total reward is

$$
R_{\mathrm{total}}(o)=\alpha_f R_f(o)+\alpha_{sf}R_{sf}(o)+\alpha_c R_c(o)+\alpha_l R_l(o),
$$

with the explicit design choice that

$$
\alpha_c \gg \{\alpha_f,\alpha_{sf},\alpha_l\},
$$

so that execution correctness dominates. In ablations, the reported weighting emphasizes correctness over formatting, with **$\alpha_c=2$**, **$\alpha_f=1$**, and **$\alpha_l=-0.5$**; equal weighting led to models that produced well-formed but incorrect SQL.

The group objective is defined through the maximum reward in the sampled set:

$$
R_{\max}(q)=\max_{i=1,\dots,G} R_{\mathrm{total}}(o_i),
$$

and the expected GRPO objective is

$$
\mathcal{J}_{\mathrm{GRPO}}(\theta)=
\mathbb{E}_{\substack{q\sim P(Q)\\ \{o_i\}_{i=1}^G\sim \pi_{\theta_{\mathrm{old}}}(\cdot\mid q)}}
\left[R_{\max}(q)\right].
$$

The clipped surrogate objective is written as

$$
\frac{1}{G}\sum_{i=1}^G \frac{1}{|o_i|}\sum_{t=1}^{|o_i|}
\left\{
\min\Bigl[
r_i^{\mathrm{ratio}}\hat{A}_{i,t},
\mathrm{clip}(r_i^{\mathrm{ratio}},1-\epsilon,1+\epsilon)\hat{A}_{i,t}
\Bigr]
-\beta\, D_{\mathrm{KL}}\bigl[\pi_\theta(\cdot\mid q)\,\|\,\pi_{\mathrm{ref}}(\cdot\mid q)\bigr]
\right\},
$$

where $r_i^{\mathrm{ratio}}$ is the probability ratio between current and old policy, $\pi_{\mathrm{ref}}$ is a fixed reference policy, $\hat{A}_{i,t}=R_{\mathrm{total}}(o_i)-b$ is the advantage estimate, and $b$ is the group baseline. The paper’s rationale for GRPO is that it **avoids a separate value critic**, **reduces memory overhead**, **stabilizes training via group baselines**, and is suitable for **sparse rewards**.

## 4. Training workflow and released datasets

Two training variants are described. The final system is **Pure-RL / R1-Zero**: no supervised warm-up, direct GRPO training on **Qwen2.5-Coder-7B-Instruct**, six candidates sampled per prompt, reward computation, and updates using the group-relative advantage. The alternative is **Cold-start + RL**, which first performs a short supervised phase on **500 random BIRD examples** for **3,000 steps**, then switches to GRPO RL. The cold-start variant improves early stability but ends up slightly below the pure RL model. Evaluation is performed on **BIRD-dev every 1,000 RL steps**, with early stopping after no improvement over **3 evals**. Convergence occurs at about **34K RL steps** for pure RL and **30K steps** for the cold-start variant [2507.06013].

The paper releases two datasets intended to support efficient and interpretable Text-to-SQL research. The first is a **Positive-Sampling Corpus** of **36,356 examples**. It is constructed from the **9,428 BIRD training prompts** by sampling **6 candidate SQLs per prompt** from **Qwen-7B-Coder** at temperature **0.9**, executing them, and retaining only correct ones. The result is **36,356 high-precision SQL examples**, each paired with a reasoning trace or chain of thought. The stated significance is that these are **execution-verified positive examples**, useful for **alignment-driven RL**, usable with **any base LLM**, and closer to the intended execution objective than conventional supervision.

The second release is a set of **QWQ-32B reasoning traces**, reported as **5,024 examples** produced by prompting a 32B QWQ model to generate step-by-step reasoning followed by SQL, with only execution-matching outputs retained. One section later reports that filtering leaves **4,928 reasoning-SQL pairs**. The paper presents these traces as useful for **explicit stepwise reasoning supervision**, **interpretability**, and both supervised and RL fine-tuning. A plausible implication is that the discrepancy between **5,024** and **4,928** reflects successive filtering stages rather than a conceptual inconsistency.

## 5. Empirical performance on BIRD-dev

The primary metric is **execution accuracy (Ex%)** on **BIRD-dev**. The headline result is **59.97% execution accuracy** in single-sample generation. In the post-training comparison, **No post-training (Qwen2.5-7B-Coder)** achieves **52.02**, **1-step RL (Rejection Sampling)** reaches **57.04**, and **CogniSQL-R1-Zero** reaches **59.97** [2507.06013].

| Setting | Single-sample Ex% | Best-of-6 Ex% |
|---|---:|---:|
| No post-training (Qwen2.5-7B-Coder) | 52.02 | 67.25 |
| 1-step RL (Rejection Sampling) | 57.04 | 69.00 |
| CogniSQL-R1-Zero | 59.97 | 69.68 |

Best-of-6 test-time scaling substantially increases performance: **67.25%** for the untrained baseline, **69.00%** for 1-step RL, and **69.68%** for CogniSQL-R1-Zero. The paper describes this as one of its most practical findings, quantifying the best-of-6 gain as about **+9.71% absolute** over single-sample performance.

The reported baseline comparisons are unusually broad. Under **10B parameters**, CogniSQL-R1-Zero at **59.97** exceeds **Think2SQL-7B: 56.1**, **SFT CodeS-7B: 57.17**, and **Qwen2.5-Coder-7B-Instruct: 50.9**. In the **10B–30B** range, it exceeds **Codestral-22B: 52.7**, **Qwen2.5-14B-Instruct: 56.7**, and **SFT CodeS-15B: 58.47**. Among **30B and above / unknown size**, it exceeds **GPT-4 Baseline: 46.35**, **Mistral Baseline: 53.52**, **DeepSeek Baseline: 56.13**, and **SuperSQL: 58.50**. The paper therefore characterizes the result as a **7B RL-trained model** outperforming much larger systems, including **GPT-4**, **Mistral 123B**, and **DeepSeek-Coder 236B**, on BIRD-dev execution accuracy.

## 6. Ablations, interpretation, limitations, and relation to R1-Zero-style work

The ablations are used to argue that **execution-aligned RL**, rather than reasoning-style imitation alone, drives the reported gains. A particularly strong result is the comparison between SFT and RL: a baseline around **52%**, SFT on distilled traces dropping to about **46%**, self-generated-data SFT reaching about **57.3%**, and RL reaching **59.97%** [2507.06013]. This directly supports the paper’s claim that supervised finetuning can overfit to supervision style and fail to optimize the actual end metric.

Several additional design findings are reported. Prompt structure matters: adding **schema DDL**, **external knowledge**, and explicit **reasoning/answer tags** helps convergence and accuracy, and removing `<reasoning>` tags weakens reward propagation. Hyperparameter trade-offs matter: among **group sizes $G\in\{4,6,8\}$** and **temperatures $T\in\{0.6,0.9\}$**, the best tradeoff is **$G=6$** and **$T=0.9$**; too large a group increases GPU stalls, while higher temperature increases invalid SQL. **KL penalty scheduling** improves accuracy from **58.4% to 59.97%**, an **about +1.6% absolute gain**. For PEFT, ranks **16, 32, 64, 128** were tested, with **rank 64** identified as the best tradeoff because it fit in memory and converged efficiently.

The reported failure modes are concentrated in **complex multi-join queries**, **nested subqueries**, and a small fraction of **malformed SQL or runtime errors**. The limitations section is correspondingly narrow and concrete: the method is evaluated primarily on **BIRD-dev**, so broader cross-dataset generalization is not fully demonstrated; remaining errors cluster in hard multi-join and nested-subquery cases; best-of-6 inference improves accuracy but can increase latency by up to **6×**; and the reward remains relatively simple, so more fine-grained execution rewards may help on harder cases.

A common misconception would be that CogniSQL-R1-Zero is simply a formatting trick or a chain-of-thought imitation system. The ablations argue against both interpretations. Equal reward weighting produced better-formatted but incorrect SQL, and distilled-trace SFT underperformed RL. The paper’s interpretation is that **training objective alignment matters more than raw size** for Text-to-SQL. In a broader research context, **Table-R1-Zero** is described as **highly relevant and arguably a direct conceptual predecessor**: it applies **RLVR with GRPO** and **verifiable rewards** to table reasoning rather than SQL generation, showing that an **R1-Zero style recipe** can work for **table-grounded structured reasoning** with a **7B-scale open LLM** [2505.23621]. This suggests that CogniSQL-R1-Zero belongs to a broader class of structured-reasoning systems in which verifiable rewards, strict output formats, and compact backbones are sufficient to induce strong task-aligned behavior without frontier-scale inference models.

Source: https://www.emergentmind.com/topics/cognisql-r1-zero