TL;DR
This paper delivers a systematic benchmark of prompt engineering and supervised fine-tuning (SFT) strategies for Large Language Model (LLM)-based Text-to-SQL systems across both proprietary and open-source models. Leveraging these empirical findings, the authors introduce DAIL-SQL, a token-efficient framework that balances prompt brevity with question-query mapping, achieving a state-of-the-art 86.6% execution accuracy on the Spider benchmark.
What Problem Does This Paper Address?
While LLMs have become the de facto paradigm for translating natural language questions into database queries (Text-to-SQL), the literature has lacked a systematic understanding of prompt design. Key decisions—how to format schemas, select few-shot demonstrations, structure prompt examples, and integrate task instructions—have largely relied on ad-hoc engineering. Furthermore, the potential of open-source LLMs and the trade-off between prompt length (API token costs/latency) and execution accuracy have remained underexplored.
Methods
The authors decompose Text-to-SQL prompt engineering into three modular decisions and investigate supervised fine-tuning:
- Question Representation: Evaluates five schema representations, from simple table-column lists (
Basic Prompt, $BS_P$) and natural language descriptions (Text Representation, $TR_P$) to DDL statements (Code Representation, $CR_P$, usingCREATE TABLEwith types and keys) and structured instructional formats (Alpaca SFT, $AS_P$; OpenAI Demonstration, $OD_P$). - In-Context Example Selection: Compares random selection against question similarity ($QTS_S$), masked question similarity ($MQS_S$), query similarity ($QRS_S$), and their proposed DAIL Selection ($DAIL_S$). $DAIL_S$ ranks candidates by masked natural language similarity while filtering for query skeleton similarity derived from a preliminary model’s predicted SQL query.
- Example Organization: Evaluates Full-Information ($FI_O$, repeating full database schemas per example), SQL-Only ($SO_O$, listing only SQL queries), and DAIL Organization ($DAIL_O$), which retains question–SQL pairs while stripping redundant database schemas from demonstration examples to maximize token efficiency.
- The DAIL-SQL Pipeline: Combines $CR_P$ representation, $DAIL_S$ example selection, and $DAIL_O$ example formatting. It can optionally be coupled with self-consistency voting.
- Supervised Fine-Tuning (SFT): Evaluates parameter fine-tuning across open-source models (LLaMA, Vicuna, CodeLLaMA) on Spider training data, isolating prompt gradients and training exclusively on SQL target tokens.
Datasets
- Spider: The standard cross-domain Text-to-SQL benchmark consisting of 8,659 training instances and 1,034 development instances across 200 databases. Evaluation was performed on the
Spider-devsplit. - Spider-Realistic: A challenging 508-example subset of Spider-dev with manually rewritten questions that remove explicit schema mentions while preserving the target SQL queries.
Key Insights and Ideas
- Schema as Code is Superior: Representing database structure via SQL DDL statements ($CR_P$) consistently outperforms natural language text schema formatting, especially for code-specialized models like CodeLLaMA.
- Small Prompt Tweaks Cause Large Swings: Adding explicit negative constraints (the rule implication
"Complete sqlite SQL query only and with no explanation") yielded up to a 6% boost in exact-set-match and a 3% boost in execution accuracy across models. Foreign key inclusion also consistently helped OpenAI models navigate table joins. - Dual-Similarity Retrieval Outperforms Unimodal Selection: In-context learning acts as analogical reasoning; selecting examples based on both masked question similarity and anticipated query skeleton similarity ($DAIL_S$) outperforms selecting purely on question or syntax similarity.
- Prompt Bloat Hurts: Retaining full database schemas in every few-shot demonstration ($FI_O$) causes extreme token bloat with negligible accuracy gains over $DAIL_O$ when using powerful LLMs like GPT-4.
- Fine-Tuning Degrades In-Context Learning in Open-Source LLMs: SFT turns modest open-source models into capable zero-shot SQL generators (matching or approaching
text-davinci-003). However, once fine-tuned on zero-shot templates, open-source models suffer catastrophic performance drops when given few-shot demonstration examples.
Key Results
- Leaderboard SOTA: DAIL-SQL (equipped with self-consistency) reached 86.6% execution accuracy (EX) on the Spider benchmark, outperforming the prior state-of-the-art DIN-SQL (85.3%). DAIL-SQL without self-consistency achieved 86.2% EX.
- Zero-Shot Representations: On Spider-dev, OpenAI Demonstration Prompt ($OD_P$) paired with GPT-3.5-Turbo scored 75.5% EX, while Code Representation ($CR_P$) scored 70.7% EX. Adding foreign keys to $OD_P$ with GPT-3.5-Turbo yielded 78.4% EX.
- Open-Source Zero-Shot Scaling: Pre-trained base LLaMA scaled from 9.1% EX (7B) to 34.0% EX (33B). CodeLLaMA-34B reached 68.5% EX with $CR_P$ representation, significantly outperforming LLaMA-2-Chat-70B (44.0% EX).
- Open-Source SFT: Fine-tuning boosted LLaMA-13B from 26.9% to 68.6% EX in zero-shot Spider-dev evaluation, and LLaMA-33B to 69.1% EX.
- Token Efficiency: DAIL-SQL matches or exceeds the accuracy of full-schema few-shot prompts while reducing average prompt token consumption by thousands of tokens compared to approaches like DIN-SQL.
Limitations and Future Work
- Dataset Scale and Realistic Complexity: Spider and Spider-Realistic feature relatively small database schemas; performance may shift under enterprise schemas with hundreds of tables and columns.
- SFT Few-Shot Degradation: The authors note the open challenge of why fine-tuned open-source models lose in-context learning capability and overfit to zero-shot query templates.
- Query Execution Cost: Evaluation metrics focus purely on correctness (execution and exact match) rather than generated query execution efficiency or computational complexity on large-scale DBMSs.
Summary updated