LLM-based Contextual Query Generation on Relational Databases
Vilohit Tapashetti, Ashwinkumar U. Motagi, Farooque Azam · 2025
In this research paper, we propose an algorithm to incorporate advanced prompting engineering strategies for large language models (LLMs) to query SQL databases in response to user queries relevant to the database. Plan-based querying, mitigating middle-forgetting, optimized context retention and Chain-of-Thought (CoT) prompting have been incorporated into the algorithm to aid with higher quality precision and reliability of the SQL queries generated. The proposed algorithm achieves over an accuracy of 96% in user response relevance and query generation. Also, a 94% consistency rate in response coherence. Our work enhances the retrieval of data from SQL databases and furthers the quality of Retrieval Augmented Generation (RAG) on databases.