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

Seq2SQL: Generating Structured Queries from Natural Language using Reinforcement Learning

Victor Zhong, Caiming Xiong, Richard SocherarXiv 2017

Abstract

Translating natural language into structured queries like SQL is a fundamental challenge in creating accessible database interfaces. Seq2SQL addresses this by leveraging the inherent structure of SQL and employing deep reinforcement learning. By using execution results as a reward signal, the model overcomes the "order problem" where multiple valid SQL strings represent the same query. This paper also introduces WikiSQL, a large-scale dataset of over 80,000 examples, providing the necessary data to train robust neural semantic parsers.

Introduction

Relational databases store the world's structured information, yet accessing them typically requires technical expertise in SQL. Natural Language Interfaces (NLI) aim to bridge this gap. However, building these systems is hindered by the scarcity of large-scale training data and the limitations of traditional sequence-to-sequence models, which often fail to account for the flexible syntax of SQL.

The Seq2SQL Framework

Seq2SQL moves beyond generic translation by treating SQL generation as a structured prediction problem. The model is composed of three specialized sub-modules that map directly to the components of a standard SQL query:

  • Aggregation Classifier: Predicts the operator (e.g., COUNT, SUM, MIN, MAX, or NULL).
  • SELECT Column Pointer: Identifies the specific table column being queried.
  • WHERE Clause Decoder: Generates the filters and conditions for the query.
The Seq2SQL architecture decomposes the query generation task into three specialized modules: an aggregation classifier, a SELECT column pointer, and a WHERE clause decoder.

By using a pointer network, the model can directly reference tokens from the input question or table headers, effectively pruning the search space and ensuring the generated query remains grounded in the provided schema.

Reinforcement Learning for SQL Generation

A key innovation of Seq2SQL is the use of policy gradient reinforcement learning for the WHERE clause. In SQL, the order of conditions in a WHERE clause does not affect the result, yet traditional cross-entropy loss penalizes any deviation from a single ground-truth string.

To solve this "order problem," the model is trained by executing generated queries against a database and receiving a reward based on the result:

  • +1 for the correct execution result.
  • -1 for a valid SQL query that yields the wrong result.
  • -2 for an invalid SQL query.
The reinforcement learning pipeline uses execution results from a database as a reward signal to train the model, allowing it to learn from functional correctness rather than just string matching.

This execution-based supervision allows the model to learn that multiple syntactic representations can be semantically equivalent.

The WikiSQL Dataset

The paper introduces WikiSQL, a landmark dataset for the community. Consisting of 80,654 hand-annotated question-SQL pairs across 24,241 tables from Wikipedia, it is an order of magnitude larger than previous benchmarks like Geoquery or ATIS. WikiSQL provides the scale required for deep learning and forces models to generalize to unseen table schemas.

Results and Impact

Seq2SQL significantly outperformed previous state-of-the-art semantic parsers on the WikiSQL benchmark. Execution accuracy improved from 35.9% to 59.4%, while logical form accuracy rose from 23.4% to 48.3%. Ablation studies confirmed that the reinforcement learning component provided a critical performance boost, particularly in handling the complexity of the WHERE clause.

Conclusion

Seq2SQL demonstrates that combining structured prediction with reinforcement learning is a powerful approach for natural language to SQL translation. By focusing on execution correctness and leveraging large-scale data, the framework moves us closer to intuitive, language-based database interaction. Future research will explore extending these methods to more complex multi-table queries and ambiguous schemas.

Summary updated

BibTeX
@misc{zhong2017seq,
  author = {Victor Zhong and Caiming Xiong and Richard Socher},
  title = {{Seq2SQL: Generating Structured Queries from Natural Language using Reinforcement Learning}},
  year = {2017},
  eprint = {1709.00103},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/1709.00103},
}
Figure