SeQLean: Verifying SQL queries using Lean
Siddhartha Gadgil, Anirudh Gupta, Ajay Kumar Nair
cs.PL
Oct 25, 2026 · v1
cs.DB
TL;DR
Models SQL as relational algebra over Finsets in Lean and proves query equivalence with grind-based automation, Mathlib, and plausible counterexample search.
Abstract
We present SeQLean, a system that uses formal verification in Lean to increase the accuracy of translation from natural language to SQL. This is based on modelling SQL in Lean via relational algebras. Since we do not have a translation known to be correct, we cannot directly prove correctness. Instead, we use proof challenges: tests of internal consistency that are unlikely to be satisfied by an incorrect translation. The simplest proof challenge is based on generating two translations of the same natural language query into SQL, ideally with one being optimized for performance and the other for understandability. If two translations are equivalent, they must give the same answer for every database. This can be checked using proof automation in Lean. Using Lean allows us to have essentially arbitrary properties as proof challenges, and also check these (including equivalence) taking into account known properties of the underlying database system. Even for equivalence queries of an arithmetic nature, we show that Lean can be more powerful than traditional tools due to the availability of Mathlib theorems. A JSON API over HTTP allows the verifier to be integrated into agentic frameworks.
Problem
Natural-language-to-SQL translation by LLMs can be wrong, and no reference translation exists against which correctness could be proved. Accuracy needs to be increased without a known-correct specification.
Approach
SeQLean uses proof challenges, mainly checking that two structurally different LLM-generated SQL translations are equivalent for every database with a given schema. SQL is modelled in Lean via relational algebra, with relations as Finsets of typed tuples. Equivalence is proved with automation built on Lean tactics such as grind, with Mathlib for arithmetic aggregates, and refuted via counterexamples found with the plausible tactic. Integrity constraints can be stated as arbitrary Lean propositions, an LLM tier attempts the pairs left open, and a JSON/HTTP API supports integration into agentic frameworks.
Results
On the Calcite corpus the system resolves 112 of 397 pairs (28%). On the Literature corpus it resolves 42 of 64 pairs (66%), proving equivalences and refuting non-equivalent pairs with kernel-checked results. It also proves nonlinear and unbounded arithmetic identities that bounded SMT-based checkers cannot handle.
| Corpus | Pairs | Proved (sql_equiv) | Refuted (sql_equiv) | +LLM Proved | +LLM Refuted | Net Resolved |
|---|
| Calcite | 397 | 110 | 0 | 2 | 0 | 112 (28%) |
| Literature | 64 | 26 | 8 | 1 | 7 | 42 (66%) |
Pairs resolved by the deterministic prover (sql_equiv) and the additional LLM tier