Organizations: School of Computer Science, Beijing University of Posts and Telecommunications, China. · College of Computing and Data Science, Nanyang Technological University, Singapore. · State Key Laboratory of Networking and Switching Technology, Beijing University of Posts and Telecommunications, China.
Efficient human-database interaction is a core problem in software engineering. Text-to-SQL (Text2SQL) tackles this by mapping natural language questions to executable SQL queries, enabling automatic SQL synthesis and reliable interaction. Although large language models (LLMs) have advanced significantly, current approaches struggle with poor transferability to open-source LLMs, limited robustness against logic and function errors in complex queries, and inefficiencies in structured search. We introduce SQL-o1, a self-reward-driven heuristic search framework built on an agent-based architecture to enhance model reasoning capabilities. SQL-o1 leverages Monte Carlo Tree Search (MCTS) for structured, multi-step exploration and incorporates a dynamic pruning strategy to accelerate inference without sacrificing accuracy. On the Spider and BIRD benchmarks, SQL-o1 achieves a 10.8-point execution accuracy improvement on the complex BIRD dataset, surpassing even GPT-4-based models. It exhibits strong few-shot generalization and robust cross-model transferability across open-source LLMs. Our code is available at https://github.com/ShuaiLyu0110/SQL-o1.
Figures & tables
Figure 1. The figure illustrates the differences between end-to-end Text-to-SQL methods and heuristic dynamic search with self-reward. While end-to-end approaches directly map queries to answers via LLM inference, the proposed framework explores candidate paths with simulation, evaluates them using self-reward and execution results, and applies Back-propagation to guide more accurate SQL generation. Comparison of Text2SQL approaches A diagram illustrating the key differences between end-to-end Text2SQL methods and the proposed Heuristic Dynamic Search with Self-Reward approach, highlighting the architectural and procedural distinctions.
Figure 2. Distributions of SQL clauses and error types across databases in Spider and BIRD. While schema linking errors are common, logic and function errors are notably more frequent and persistent—highlighting the limitations of single-pass decoding in handling complex reasoning. Bar charts showing SQL clause distribution and error type frequency across databases Two side-by-side bar charts analyzing SQL characteristics and error patterns in Spider and BIRD datasets. The left chart shows the distribution of different SQL clauses (SELECT, WHERE, GROUP BY, etc.) across queries. The right chart compares the frequency of various error types, with schema linking errors being common but logic and function errors showing higher prevalence and persistence, demonstrating the challenges of complex reasoning in single-pass decoding approaches.
Method
Optimization
Search / Inference Granularity
Reward / Feedback
Adaptive Pruning
Reasoning-SQL ( Pourreza et al., 2025 )
GRPO
Sequence-level reasoning
Partial rewards
No
SQL-R1 ( Ma et al., 2025 )
SFT + GRPO
Sequence-level reasoning
Multi-component rewards
No
CSC-SQL ( Sheng and Xu, 2025 )
GRPO
Complete-SQL sampling/revision
Execution feedback
No
MCTS-SQL ( Yuan et al., 2025 )
Inference-time MCTS
Complete-SQL refinement
Refinement feedback
No
Alpha-SQL ( Li et al., 2025 )
Zero-shot MCTS
High-level SQL actions
Execution consistency
No
SQL-o1
PSG-based SFT+MCTS
Partial SQL prefixes
Self-reward+online agreement
Yes
Table 1. Design-space comparison between SQL-o1 and Text-to-SQL reasoning and search methods.
Error Type
Spider (%)
BIRD (%)
Description
Schema Linking
26.50
12.8
Wrong or missing match between question and schema.
Join Errors
16.8
39.7
Incorrect or missing join conditions between tables.
Ignoring Evidence
7.8
5.8
Missing key information or constraints from the question.
Logic and Function Errors
48.9
41.7
Logical or functional mistakes in query construction.
Table 2. Distribution and explanation of defined error types across benchmarks. Summary of error type distribution A table showing the percentage distribution of four types of errors (Schema Linking, Join Errors, Ignoring Evidence, Logic and Function Errors) across Spider and BIRD benchmarks, along with brief descriptions of each error type.
Figure 3. An overview of the implementation of the SQL-o1 framework. SQL-o1 framework implementation overview A diagram illustrating the complete implementation pipeline of the SQL-o1 framework, showing the various processing stages from input to final SQL query output.
Figure 4. An example illustrates MCTS search guided by self-reward. An example diagram of MCTS search process A detailed diagram showing the Monte Carlo Tree Search process guided by self-reward mechanism, illustrating the selection, expansion, simulation, and Back-propagation stages in SQL query generation. The diagram demonstrates how the algorithm explores different decoding paths to optimize the final SQL output.
Model / Method
Params
Spider
BIRD
Dev-EX
Dev-TS
Test-EX
Dev-EX
Dev-VES
Prompting with Closed-Source LLMs
GPT-4-based Methods
GPT-4 ( Achiam et al., 2023 )
-
72.9
64.9
-
46.4
49.8
DIN-SQL ( Pourreza and Rafiei, 2023 )
-
82.8
74.2
85.3
50.7
58.8
DAIL-SQL ( Gao et al., 2023 )
-
83.5
76.2
86.6
54.8
56.1
Table 3. We compare the performance of different models and methods on the Spider and BIRD datasets. To ensure fairness, we report ROUTE results only with open-source models of the same parameter scale. Bold numbers represent the best performance.
Model/Method
SYN-EX
SYN-TS
REALI.-EX
REALI.-TS
DK-EX
AVG.
SQL-PaLM (FS)
74.6
67.4
77.6
72.4
66.5
71.7
SQL-PaLM (FT)
70.9
66.4
77.4
73.2
67.5
71.1
SENSE-7B
72.6
64.9
82.7
75.6
77.9
74.7
ROUTE + Llama3-8B
77.4
70.2
80.9
72.6
74.6
75.1
Ours: SQL-o1 + Llama3-8B
77.6 ±0.7
69.2 ±0.9
82.7 ±0.6
72.8 ±0.8
78.7 ±1.0
76.2 ±0.6
Table 4. Evaluation results of SQL-o1 and previous methods on Spider-based robustness benchmarks, including SYN, REALISTIC, and DK. TS results for DK are omitted due to compatibility issues. SQL-o1 robustness evaluation results This table presents the evaluation results of the proposed SQL-o1 method and previous approaches on Spider-based robustness benchmarks, including SYN, REALISTIC, and DK (Database-Knowledge) test sets. Execution accuracy (EX) and Test Suite accuracy (TS) metrics are reported where available. TS results for the DK benchmark are omitted due to compatibility issues with the test suite framework. The SQL-o1 method with Llama3-8B backbone achieves the highest average performance (76.2%) across all benchmarks, demonstrating superior robustness compared to existing methods including SQL-PaLM (both few-shot and fine-tuned variants), SENSE-7B, and ROUTE + Llama3-8B. Notably, SQL-o1 achieves the best performance on SYN-EX (77.6%), REALISTIC-EX (82.7%), and DK-EX (78.7%) benchmarks, while maintaining competitive performance on the remaining metrics.
Table 9
Figure 5. The transferability results on different open-source LLMs on Spider and BIRD. Comparative bar charts showing model transferability performance Two scatter plots placed side by side, labeled (a) and (b), comparing the transferability performance of two large language models, CodeLlama-7B and DeepSeek-Coder-7B, on the Spider and BIRD benchmarks. SQL-o1 consistently achieves state-of-the-art performance.
Figure 6. SQL-o1 reduces Logic and Function Errors across databases in the Spider benchmark. SQL-o1 error reduction Bar chart showing SQL-o1 significantly reduces both Logic Errors and Function Errors across various database complexity levels in the Spider benchmark. Demonstrates improved accuracy in SQL generation through structured decoding.
Figure 7. SQL-o1 balances execution accuracy and efficiency across benchmarks.
Figure 8. Analysis of exploration weight and search depth effects on Spider and BIRD datasets.
Hyperparameter Name
Spider
BIRD
Initial Fine-tuning Stage
SFT Type
LoRA
LoRA
Batch Size
32
32
Learning Rate
1e-5
1e-5
Training Epochs
2
2
Progressive SQL Generation Data Ratio
22.7%
22.7%
Table 7. Hyperparameter Settings for Spider and BIRD datasets.
Figure 9. Performance comparison of SQL-o1 and Fully Supervised Fine-Tuned (FSFT) Llama3-8B on different datasets and sample sizes.
Figure 10. Effect of MCTS iterations on execution accuracy and inference efficiency. Two side-by-side charts showing MCTS iteration effects Left chart shows iteration effects on Spider-Dev dataset, right chart shows effects on BIRD-Dev dataset, both measuring execution accuracy and inference efficiency across different iteration counts.
λ
EX
Avg. Time (s)
Pruned Paths (%)
1.0
78.5
4.18
42.1
0.95
80.7
4.21
27.7
0.9
82.6
4.32
20.8
0.85
82.4
5.24
17.1
0.8
81.8
8.21
14.6
0.6
80.3
7.38
7.3
Table 8. Impact of the hyperparameter λ on inference efficiency
Methods
Easy
Medium
Hard
Extra
All
Prompting-based methods
C3-SQL + ChatGPT ( Dong et al., 2023 )
92.7
85.2
77.6
62.0
82.0
DAIL-SQL + GPT-4 + Self-consistency
91.5
90.1
75.3
62.7
83.6
SuperSQL + GPT-4 ( Li et al., 2024a )
94.4
91.3
83.3
68.7
87.0
MCS-SQL + GPT-4 ( Lee et al., 2025 )
94.0
93.5
88.5
72.9
89.5
Fine-tuning-based methods
Table 9. Performance (EX) comparison under different hardness levels on the Spider development set. Bold numbers represent the highest performance among fine-tuning-based methods.
Methods
Simple
Moderate
Challenging
All
Prompting-based methods
C3-SQL + ChatGPT ( Dong et al., 2023 )
58.9
38.5
31.9
50.2
DAIL-SQL + GPT-4
62.5
43.2
37.5
54.3
DAIL-SQL + GPT-4 + Self-consistency
63.0
45.6
43.1
55.9
SuperSQL + GPT-4 ( Li et al., 2024a )
66.9
46.5
43.8
58.5
MAC-SQL + GPT-4 ( Wang et al., 2024 )
65.7
52.7
40.3
59.4
Table 10. Performance (EX) comparison under different hardness levels on the BIRD development set. Bold numbers represent the highest performance among fine-tuning-based methods.
Methods
LLMs
SPIDER-Dev-EX
BIRD-Dev-EX
BIRD-Dev-VES
Proprietary reference systems
CHASE-SQL ( Pourreza et al., 2024 )
Gemini 1.5 Pro
87.6
73.1
73.0
XiYan-SQL ( Gao et al., 2024 )
GPT-4
–
73.3
–
Same-backbone open-source comparisons
ROUTE+MCP
Qwen2.5-7B
78.3
49.7
52.8
ROUTE+MCP
Qwen2.5-14B
80.0
56.3
57.6
Table 11. Scaling comparison of SQL-o1 with same-backbone open-source baselines and representative proprietary Text-to-SQL systems. Bold numbers represent the highest performance.
Methods
Base Model
Average Token Consumption
Total Cost ($)
Cost per 1K Tokens ($)
Spider-Dev (EX)
XiYan-SQL
Multiple models
6,000
156.91
0.02529
89.6
CSC-SQL ( n=64 )
Qwen2.5-Coder-7B
9,600
6.16
0.00062
69.2
ROUTE
Qwen2.5-14B
14,531
3.28
0.00022
87.3
Ours: SQL-o1
Qwen2.5-7B
9,683
3.24
0.00032
84.7
Ours: SQL-o1
Qwen2.5-14B
15,768
3.48
0.00021
88.4
Table 12. Comparison of model performance and inference cost on the Spider development set. Comparison of model performance and inference cost. This table compares XiYan-SQL, CSC-SQL, ROUTE, and SQL-o1 in terms of base model, average token consumption, total inference cost, average cost per token, and execution accuracy on the Spider development set.
Figure 11. Proportions of SQL-o1 error types on BIRD (EPE: Execution-Path Errors; REI: Reward Evaluation Inconsistency; CPN: Correct Path Not Discovered; OE: Other Errors). A bar chart showing the distribution of different error types. A detailed bar chart titled "Proportions of SQL-o1 Error Types". It shows four categories: Execution Errors (EPE), Reward Inconsistency (REI), Missed Correct Path (CPN), and Other (OE), with EPE being the most common.
Figure 12. The prompt template for Text-to-SQL in the fine-tuning phase. The structure of a Text2SQL fine-tuning prompt The figure illustrates the template used to create prompts for fine-tuning a Text2SQL model. It likely shows a structured text format containing a natural language instruction or question, the corresponding ground truth SQL query, and potentially special tokens or separators used to format the input for the language model.