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

SQL-Encoder: Improving NL2SQL In-Context Learning Through a Context-Aware Encoder

Mohammadreza Pourreza, Davood Rafiei, Yuxi Feng, Raymond Li, Zhenan Fan, Weiwei ZhangarXiv 2024 · DOI

Abstract

Natural Language to SQL (NL2SQL) systems often rely on In-Context Learning (ICL) by providing Large Language Models (LLMs) with few-shot examples. However, traditional example selection based on semantic similarity often fails to capture structural SQL logic. This paper introduces SQL-Encoder, a 1.3B parameter cross-encoder trained on a multi-faceted similarity metric encompassing question text, SQL skeletons, and schema linking. Our results demonstrate that SQL-Encoder significantly outperforms standard embedding models, providing up to an 8% accuracy boost for open-source models like CodeLlama.

1. Introduction

Large Language Models (LLMs) have shown remarkable proficiency in generating SQL queries from natural language questions when provided with relevant examples in the prompt. This technique, known as In-Context Learning (ICL), typically uses a retriever to find the most similar examples from a training set. However, most current retrievers rely solely on semantic similarity—the overlap of words in the questions—which often ignores the underlying structural complexity of the required SQL query.

2. The Limitations of Semantic Retrieval

The authors identify two primary weaknesses in current semantic-based retrieval for NL2SQL:

  1. The Cross-Domain Trap: Many systems retrieve examples from different databases (cross-domain). However, empirical evidence suggests that "in-domain" examples (from the same database) are significantly more effective, as they help the model understand specific schema quirks such as table names and join keys.
  2. Surface-Level Matching: Questions with similar vocabulary may require vastly different SQL structures, while questions with different wording might share the same logic (e.g., using a GROUP BY or a nested query). Standard embeddings from providers like OpenAI or Cohere often fail to capture this structural "DNA."

3. Methodology: The SQL-Encoder Framework

To address these issues, the researchers developed SQL-Encoder, a specialized model designed to predict the structural similarity between two NL2SQL tasks.

3.1 The "Triple Threat" Similarity Metric

The model is trained using a ground-truth similarity score derived from three distinct components:

  • Question Similarity: Traditional text-based overlap.
  • SQL Skeleton Similarity: SQL queries are converted into Abstract Syntax Trees (ASTs), and similarity is calculated using Tree Edit Distance to compare the "bones" of the queries.
  • Schema-Linking Similarity: This measures the overlap in the specific tables and columns referenced by the queries.
Figure 1: Illustration of how different similarity metrics (Question, Skeleton, and Schema-Linking) identify different "most similar" samples for a given input.

3.2 Model Architecture

SQL-Encoder utilizes a DeepSeek 1.3B base model configured as a cross-encoder. Unlike bi-encoders that process questions independently, the cross-encoder takes the database schema and both natural language questions as a single input. This allows the model to perform deep interaction between the questions and the schema, leading to a more context-aware similarity prediction.

4. Experimental Results

The model was evaluated on the BIRD and Spider datasets. SQL-Encoder consistently outperformed commercial embedding models in identifying structurally relevant examples.

4.1 Downstream Performance Gains

The most critical metric is the improvement in final SQL generation accuracy. By using SQL-Encoder to select examples, the researchers observed significant gains across various LLMs:

  • GPT-3.5-turbo: Achieved a 1-2% improvement.
  • CodeLlama-7B: Saw the most substantial boost, ranging from 4% to 8%.
  • CodeLlama-13B: Improved by 2-3%.
Figure 6: Execution accuracy (EX) comparison showing SQL-Encoder's performance against OpenAI, Cohere, and Zero-shot baselines across different model sizes.

5. Conclusion and Future Work

SQL-Encoder demonstrates that context-aware, structural similarity is superior to simple semantic matching for NL2SQL tasks. While the cross-encoder architecture introduces higher computational costs compared to standard vector searches, the accuracy gains—particularly for open-source models—are compelling. Future work will focus on optimizing the latency of the cross-encoder and integrating it into full Retrieval-Augmented Generation (RAG) pipelines specifically tuned for database logic.

Summary updated

BibTeX
@misc{pourreza2024sql,
  author = {Mohammadreza Pourreza and Davood Rafiei and Yuxi Feng and Raymond Li and Zhenan Fan and Weiwei Zhang},
  title = {{SQL-Encoder: Improving NL2SQL In-Context Learning Through a Context-Aware Encoder}},
  year = {2024},
  eprint = {2403.16204},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2403.16204},
}
Figure