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

Knowledge-to-SQL: Enhancing SQL Generation with Data Expert LLM

Zijin Hong, Zheng Yuan, Hao Chen, Qinggang Zhang, Feiran Huang, Xiao Huang

Abstract

Large Language Models (LLMs) often struggle with text-to-SQL tasks when user questions require domain-specific knowledge not explicitly present in the database schema. This paper introduces the Knowledge-to-SQL framework, featuring a specialized Data Expert LLM (DELLM). The DELLM generates auxiliary "expert knowledge"—such as arithmetic hints and domain logic—which is then refined using a novel Preference Learning via Database Feedback (PLDBF) approach. Experimental results on the BIRD and Spider benchmarks demonstrate significant performance gains across various base models, including GPT-4 and T5.

Introduction

While LLMs have shown remarkable proficiency in code generation, translating natural language to SQL remains challenging due to the "knowledge gap." Users often pose questions containing implicit assumptions or domain-specific terminology (e.g., "August 2012" mapping to a specific string format '201208') that are not defined in the schema. Traditionally, systems have relied on manual prompt engineering or LLM "guessing." This work proposes an automated "Data Expert" to provide the necessary reasoning context, enabling a "Coder LLM" to generate accurate SQL.

Methods: The Data Expert LLM (DELLM)

The core of the framework is the DELLM, a LLaMA-2-13b model trained to act as a bridge between the user and the database.

Framework Overview

The process begins with Table Reading, where relevant sub-tables are retrieved using semantic similarity. The DELLM then undergoes Supervised Fine-Tuning (SFT) on human-annotated "gold" knowledge.

The Knowledge-to-SQL framework and the DELLM training process.

Preference Learning via Database Feedback (PLDBF)

To ensure the generated knowledge is actually useful for SQL generation, the authors implement Preference Learning via Database Feedback (PLDBF). Using Direct Preference Optimization (DPO), the model is trained to prefer knowledge that leads to successful SQL execution. If the generated SQL returns correct data, the knowledge is labeled positive; otherwise, it is negative. This feedback loop allows the model to learn from the database itself rather than just mimicking text.

Experimental Results

The framework was evaluated on the BIRD benchmark (complex, knowledge-intensive) and the Spider benchmark (cross-domain).

Performance Gains

As shown in the results table, the DELLM consistently improves the execution accuracy (EX) and Valid Efficiency Score (VES) of various models.

Execution Accuracy (EX) and Valid Efficiency Score (VES) across different models on BIRD and Spider benchmarks.

On the BIRD benchmark, GPT-4's accuracy improved by 4.69%, while the T5-3B model saw a substantial +6.31% boost. The framework also demonstrated cross-domain generalization on the Spider dataset, providing a +1.71% improvement for GPT-3.5-Turbo despite being trained on BIRD.

Discussion and Conclusion

The Knowledge-to-SQL framework highlights that the primary bottleneck in text-to-SQL is often arithmetic reasoning and data condition mapping rather than syntax. By treating the database as a teacher through execution feedback, the DELLM effectively bridges this reasoning gap.

However, challenges remain. A significant portion of queries (61.5%) remained incorrect even with expert knowledge, suggesting that the base LLM's ability to integrate provided hints is a critical area for future improvement. Additionally, the extra generation step introduces inference latency, which may impact real-time applications. Future work will focus on handling "noisy" real-world metadata and improving the integration capabilities of smaller open-source models.

Summary updated

BibTeX
@inproceedings{hong2024knowledge,
  author = {Zijin Hong and Zheng Yuan and Hao Chen and Qinggang Zhang and Feiran Huang and Xiao Huang},
  title = {{Knowledge-to-SQL: Enhancing SQL Generation with Data Expert LLM}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2024},
  doi = {10.18653/v1/2024.findings-acl.653},
}
Figure