Natural SQL: Making SQL Easier to Infer from Natural Language Specifications

Yujian Gan, Xinyun Chen, Jinxia Xie, Matthew Purver, John R. Woodward, John H. Drake, Qiaofu Zhang

Abstract

Translating human questions into complex SQL queries is a significant challenge for artificial intelligence due to the structural mismatch between natural language and relational database logic. This paper introduces NatSQL, an intermediate representation (IR) designed to bridge this divide. By removing boilerplate keywords and flattening hierarchical structures, NatSQL simplifies the mapping process for text-to-SQL models. Experimental results on the Spider benchmark show that NatSQL achieves a state-of-the-art execution accuracy of 73.3%, with notable improvements in handling highly complex queries.

Introduction

The field of "Text-to-SQL" aims to democratize data access by allowing non-technical users to query databases using everyday language. However, standard SQL is optimized for relational efficiency, not linguistic clarity. A user might ask, "Which films have more than 5 actors?" but a valid SQL query requires explicit JOIN conditions, GROUP BY clauses, and HAVING filters—elements rarely present in the user's mental model or spoken request. Previous intermediate representations, such as SemQL, attempted to simplify this but often struggled with complex nested structures or lost critical schema information. NatSQL addresses these bottlenecks by aligning its structure more closely with the linear and implicit nature of human speech.

The NatSQL Approach

NatSQL's design philosophy centers on stripping away the "boilerplate" parts of SQL that are difficult for AI models to predict from text.

Comparison of SQL and various intermediate representations, highlighting NatSQL's simplification by removing boilerplate clauses.

Key innovations include:

  1. Boilerplate Removal: NatSQL dispenses with FROM, JOIN ON, GROUP BY, and HAVING clauses. These are treated as inference problems rather than prediction problems, as they can be reconstructed using the database schema.
  2. Hierarchical Flattening: Instead of deep nesting, NatSQL unifies subqueries and set operators (like UNION or INTERSECT) into the WHERE clause using simplified operators such as and, or, except, and sub.
  3. Schema Linking with Placeholders: To handle cases where a user implies a column (e.g., "number of visitors" referring to visitor_id), NatSQL uses a special @ placeholder. A deterministic compiler then identifies the correct primary or foreign keys to fill these gaps.

Bridging Linguistic Variation

A core strength of NatSQL is its ability to handle the inherent ambiguity of natural language. For instance, the word "and" can represent either a logical conjunction or a set intersection depending on the context. NatSQL unifies these concepts, allowing the compiler to determine the correct SQL keyword based on the underlying database structure.

NatSQL's ability to map different natural language phrasings to a unified intermediate representation and subsequently the same SQL query.

As shown above, different phrasings of the same underlying intent—whether focusing on the rental fee or the inventory count first—are mapped to a consistent NatSQL representation. This reduces the search space for neural models and ensures that linguistic variations do not lead to divergent, incorrect SQL outputs.

Evaluation and Results

The researchers evaluated NatSQL by integrating it into top-performing models like RAT-SQL and GAP, testing them against the Spider benchmark. Spider is a cross-domain dataset featuring complex queries across 200 different databases.

  • State-of-the-Art Performance: The RAT-SQL+GAP+NatSQL configuration achieved 73.3% execution accuracy on the Spider hidden test set, marking a 2.2% absolute improvement over previous benchmarks.
  • Complexity Gains: The most significant impact was observed in "Extra Hard" queries, where NatSQL improved exact match accuracy by an average of 4.74% across various architectures.
  • Executability: Unlike some IRs that focus solely on structural matching, NatSQL was designed to generate fully executable code, including the correct filtering values.

Conclusion

NatSQL demonstrates that the key to improving Text-to-SQL performance lies in better alignment between the model's output space and the user's input language. By treating database-specific boilerplate as an inference task rather than a translation task, NatSQL allows AI models to focus on the core logic of the user's request. Future work will look to expand NatSQL's support for multiple set operators and further refine its ability to handle underspecified human questions across diverse database dialects.

Summary updated

BibTeX
@inproceedings{gan2021natural,
  author = {Yujian Gan and Xinyun Chen and Jinxia Xie and Matthew Purver and John R. Woodward and John H. Drake and Qiaofu Zhang},
  title = {{Natural SQL: Making SQL Easier to Infer from Natural Language Specifications}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2021},
  url = {https://openreview.net/forum?id=DbxIHZreI-},
}
Figure