Self-Contrastive Loop of Thought Method for Text-to- SQL Based on Large Language Model (2025)

Abstract: Text-to-SQL is a task with excellent prospects and challenges, and it aims to convert natural language queries (NL) into corresponding structured query language (SQL) statements. The main challenge of this task is how to efficiently transform unstructured data and structured data. In recent years, the emergence of large language models (LLMs) has further promoted the development of this field. However, current LLM-based text-to-SQL methods rely on specific few-shot example construction, resulting in poor performance across domains. To solve this problem, we propose a text-to-SQL method of self-contrastive loop of thought structure. This method designs the LLM inference process as a loop structure based on the comparison of positive and negative examples. The model optimizes the generated results through continuous verification and error correction, greatly improving accuracy and reducing dependence on few-shot example construction. The experimental results on SPIDER and BIRD datasets show that this method can generate SQL with higher precision without relying on few-shot example construction.

## introduction the goal of text-to-sql is to generate sql based on natural language. this technique can generate the corresponding sql statements by verbal description. due to its broad application prospect and challenge, this task has attracted wide attention @xcite @xcite . in the early stages, such research typically achieved text-to-sql through rule-based designs and pre-trained models. design methods improved the quality of sql generation by reinforcing the alignment between text and database schemas, such as rat-sql @xcite , sdsql @xcite , lgesql @xcite , resd-sql @xcite . additionally, some studies employed various pre-training strategies, such as tabert @xcite , grappa @xcite , gap @xcite , and miga @xcite , these models enhanced ability to capture the complex relationship between natural language and sql structures. together, these efforts improve the performance of text-to-sql. with the development of llm, recent studies have demonstrated the superior ability of llm in complex tasks @xcite @xcite . in the text-to-sql field, the llm-based approach exceeds previous work on multiple datasets without any fine-tuning or training @xcite @xcite . based on the advanced performance of llm, prompt-based methods have further promoted the progress of text-to-sql. typical work such as din-sql @xcite , dail-sql @xcite , ta-sql @xcite and mac-sql @xcite . these methods are based on the few-shot prompt method, combined with chain-of-thought technology to decompose the task to improve llm performance. although the prompt-based method has made significant progress, it still has some problems. first, the performance depends on the quality of the few-shot examples, which can lead to llm not achieving the potential performance. second, most of this work uses a linear thinking workflow to guide llm generation, which comprises the search space. third, this work improves the ability to deal with complex problems through detailed decomposition tasks. however, it may introduce an additional risk of hallucination, which can adversely affect the results. to solve the above problems, we propose a textto-sql method based on a self-contrastive loop of thought structure (scl). the llm generation process is constructed as a discriminant loop structure based on the contrast of positive and negative examples so that the model can improve the accuracy of the results in the continuous self-correction. this reduces the dependence on the construction of few-shot examples, and the generation relies only on the self-contrast between positive and negative examples. with this approach, the llm can better focus on outputs similar to positive examples and exclude erroneous results similar to negative examples, reducing the inference burden of the llm. on this basis, we summarize the contributions of this paper as follows. ## analysis to show the details of scl-sql, we conducted a detailed analysis of the parts of scl-sql, including hyper-parameter experiments and result analysis, where all analysis is based on the bird development set, and defaults are used for parameters not mentioned. ## conclusion we proposed a text-to-sql method based on a selfcontrastive loop of thought to solve the problem of llm' dependence on few-shot example construction in text-to-sql tasks. by designing the generation process as a discriminant loop structure based on a comparison of positive and negative examples, we significantly improved the accuracy and generalization of llm and reduced the dependence on small samples. experimental results show that the proposed method performs well on multiple data sets, and the generated sql queries have higher accuracy. the contribution of this paper also includes the design of a question split strategy, execution, logical verification mechanism, and introduction of the "loop" structure to optimize the self-verification and error-correcting ability of llm in text-to-sql tasks.