International Journal of Computer Science Languages Review Article
A Retrieval-Augmented Generation Framework for Accurate and Safe Text-to-SQL Translation Using Large Language Models
Abstract
The increasing reliance on data-driven decision-making has amplified the need for intuitive database querying mechanisms. However, accessing and managing relational databases often demands knowledge of Structured Query Language (SQL), making it challenging for users without technical expertise. Recent advancements in large language models (LLMs) have enabled natural language interfaces for database querying, yet these systems often suffer from hallucination, lack of schema awareness, and unreliable query execution. This paper proposes a novel retrieval-augmented generation (RAG)-based framework for text-to-SQL translation that integrates semantic retrieval, schema-aware prompting, and robust execution safeguards. The proposed system leverages vector similarity search using Facebook AI similarity search (FAISS) to retrieve relevant query examples and incorporates dynamic schema extraction to guide the LLM. Additionally, a multi-layer safety mechanism and an automated error correction loop are introduced to ensure reliable SQL execution. Experimental results indicate that the developed framework achieves higher query precision and better execution performance than conventional approaches that rely only on LLMs. The system achieves an accuracy of 86% and a success rate of 92%, highlighting its effectiveness in real-world database interaction scenarios.
Keywords
References (13)
- Zhong V, Xiong C, Socher R. Seq2SQL: generating structured queries from natural language using reinforcement learning [Preprint]. arXiv. 2017 Aug 31. arXiv:1709.00103. doi:10.48550/arXiv.1709.00103.
- Xu X, Liu C, Song D. SQLNet: generating structured queries from natural language without reinforcement learning [Preprint]. arXiv. 2017 Nov 13. arXiv:1711.04436. Available from: https://arxiv.org/abs/1711.04436
- Wang B, Shin R, Liu X, Polozov O, Richardson M. RAT-SQL: relation-aware schema encoding and linking for text-to-SQL parsers. In: Proceedings of the 58th Annual Meeting of the Association for Computational Linguistics. Association for Computational Linguistics; 2020 Jul. p. 7567–78. doi:10.18653/v1/2020.acl-main.677.
- Lewis P, Perez E, Piktus A, Petroni F, Karpukhin V, Goyal N, et al. Retrieval-augmented generation for knowledge-intensive NLP tasks. Adv Neural Inf Process Syst. 2020;33:9459-74.
- Brown T, Mann B, Ryder N, Subbiah M, Kaplan JD, Dhariwal P, et al. Language models are few-shot learners. Adv Neural Inf Process Syst. 2020;33:1877-901.
- Devlin J, Chang MW, Lee K, Toutanova K. BERT: pre-training of deep bidirectional transformers for language understanding. In: Proceedings of the 2019 Conference of the North American Chapter of the Association for Computational Linguistics: Human Language Technologies, Volume 1 (Long and Short Papers). Minneapolis (MN): Association for Computational Linguistics; 2019 Jun. p. 4171–86. doi:10.18653/v1/N19-1423.
- Johnson J, Douze M, Jégou H. Billion-scale similarity search with GPUs. IEEE Trans Big Data. 2021;7(3):535-47. doi:10.1109/TBDATA.2019.2921572.
- OpenAI. API platform documentation [Online]. OpenAI. Available from: https://developers.
- openai.com/api/docs
- Google. Google AI for Developers [Online]. Google. Available from: https://ai.google.dev/
- Groq. Groq is the premier neocloud for fast inference [Online]. Groq. Available from: https://groq.com/
- Goyal D, Gautam A. Introduction to LangChain framework. In: Malik M, Sharma P, Hooda S, editors. Textual Intelligence: Large Language Models and Their Real-World Applications. John Wiley & Sons; 2025. p. 253-85. doi:10.1002/9781394287499.ch11.
- Richards T. Streamlit for Data Science. Birmingham (UK): Packt Publishing; 2023.