This project fine-tunes a T5-base transformer model to translate natural language questions into SQL queries, using the Spider dataset — a large-scale, cross-domain benchmark spanning 100+ real-world relational database schemas (people, colleges, sports, insurance, etc.).
Given a question and a database schema, the model generates a syntactically correct, schema-grounded SQL query — without ever having seen that exact question during training.
- Base model:
t5-base(encoder-decoder transformer), fine-tuned end-to-end (not just a classification head) - Input format:
translate English to SQL: <question> | schema: <table (col:type, col:type) | table (...)> - Training data: 8,659 examples from Spider + supplementary Text-to-SQL datasets, split 90/10 train/val
- Training: AdamW optimizer, gradient clipping, checkpointing on best validation loss, run for 7+ epochs
| Metric | Value (Epoch 7) |
|---|---|
| Train Loss | 0.0867 |
| Train Token Accuracy | 97.24% |
| Val Loss | 0.0881 |
| Val Token Accuracy | 97.58% |
Beyond token-level accuracy, the model was evaluated with exact-match testing on real validation examples across multiple schemas, correctly generating queries for:
- Simple
SELECTstatements COUNT(*)aggregatesMAX(...)/AVG(...)aggregates- Conditional
WHEREfilters
Digging past the headline accuracy number surfaced specific, explainable weaknesses rather than random failure:
- Zero-shot schemas: On database schemas with zero training examples, the model produces plausible-looking but incorrect SQL (e.g., hallucinated syntax). This reflects the difficulty of true zero-shot schema generalization, not a training bug.
- Multi-column SELECT + GROUP BY: The model occasionally drops the grouping column from the SELECT list when combined with aggregate functions.
- Table disambiguation: On schemas with closely related tables (e.g.,
coursevssection), the model sometimes queries the wrong-but-related table. - Complex multi-table JOINs: Like most base-size Text-to-SQL models, performance drops on Spider's "hard"/"extra-hard" queries requiring multiple JOINs and nested subqueries.
Token-level accuracy (97%+) does not equal query correctness — a handful of wrong tokens in a short SQL query (a missing column, a hallucinated table name) can make an otherwise "97% accurate" prediction functionally useless. Exact-match / execution-based evaluation is necessary to understand real-world performance, especially for structured generation tasks like Text-to-SQL.
Python · PyTorch · HuggingFace Transformers · T5 · Google Colab