Skip to content
Open access

Validating LLM-Generated SQL Queries through Metamorphic Prompting

Jun 2026 · Proceedings of the ACM on Software Engineering · Vol 3, pp. 411 - 434 · 1 citation · ⚡ 1 influential · 72 references

TL;DR

Experimental results demonstrate that MRSQLGen consistently outperforms state-of-the-art hallucination detection techniques, achieving higher precision and recall in detecting hallucinated SQL queries.

Abstract

Large Language Models (LLMs) can translate natural language (NL) into SQL, enabling non-experts to query databases via conversational interfaces. However, the generated SQL often contains intent-violating hallucinations—queries that are syntactically valid and executable, yet semantically misaligned with the user’s question. These failures are especially risky in real-world settings where users cannot verify the correctness. In this paper, we propose MRSQLGen, a framework for detecting intent-violating hallucinations, built on the metamorphic prompting paradigm. MRSQLGen rewrites the input prompt using task-specific transformation rules derived from a hallucination taxonomy, and validates the generated SQL by checking behavioral consistency across multiple executions. Each transformation is associated with a metamorphic relationship (MR) that defines the expected relation between results; discrepancies are aggregated through a majority-vote strategy to robustly flag hallucinations without ground-truth SQL. We evaluate MRSQLGen on two benchmarks (Spider and Bird) using five representative LLMs, including GPT-4o. Experimental results demonstrate that MRSQLGen consistently outperforms state-of-the-art hallucination detection techniques, achieving higher precision and recall in detecting hallucinated SQL queries.

Read PDF

Similar papers

Open access Jul 2026

GradeSQL: Outcome reward models for intelligent Text-to-SQL generation from LLMs

As Large Language Models (LLMs) become foundational to next-generation Intelligent Information Systems, the bridge between natural language interfaces and structured database systems remains a critical bottleneck. While Text-to-SQL generation enables cooperative support for complex query formulation, ensuring the rel...

M. Tritto, G. Farano, Dario Di Palma et al. · 2 citations

Natural Language Interfaces for Databases: What Changes for Users?

A mixed-method, between-subjects user study comparing SQL-LLM, a GPT-4o-backed NLIDB, with Snowflake, a traditional SQL analytics platform finds that a verification burden survives the switch to natural language, so an NLIDB that hides the generated SQL would remove the very step users relied on to trust the answer.

Unknown authors · 0 citations
Preprint Aug 2026

TraceSQL: Traceable Answerability Estimation for Reference-Free Text-to-SQL Verification

Results show that SQL verification can be performed with a lightweight learned model while retaining feature-level evidence for inspecting and diagnosing its predictions, and feature attribution shows that the model relies on both semantic grounding and deterministic SQL-structure signals.

N. Shukla, Debasmita Panda, Srutanik Bhaduri et al. · 0 citations
Jul 2026

GPS-Relational: Generative Problem Solving with Relational Databases

Experimental results demonstrate that GPS-Relational constructs executable reference SQL across existing benchmark and complex generated queries, and that LLM execution accuracy degrades significantly as nesting depth increases up to 15 levels.

Goli Vaisi, P. Sheu · 0 citations

MedSQLX: Translation of Medical Queries into UDF-Centric SQL

An agentic framework that leverages Large Language Models (LLMs) for generating UDF-centric queries from natural language task descriptions in the medical domain is presented, demonstrating that structured tool orchestration with verification loops substantially improves generation quality.

Catlynh Nguyen · 0 citations

Two Surfaces of Ambiguity: Complementary Detection for Text-to-SQL

This work shows that introspection and sampling are complementary but not disjoint on BIRD-Interact-Lite, a Text-to-SQL benchmark with 300 tasks, annotated ambiguities, and an LLM-based user simulator, and proposes a proposed multi-label grammar that substantially narrows it without affecting downstream context.

Leonhard Liu, Patrick K. Erdelt, Two · 1 citation

We use cookies to run the site and, with your consent, for analytics and to show ads. See our Cookie Policy.