Using Large Language Model(LLM) to write SQL query

Sumeet More
2 min readJun 25, 2023

--

Photo by Glenn Carstens-Peters on Unsplash

Disclaimer: This article is based on my current knowledge and understanding. If you have any query or see any improvement, please mention in the comment section

If you are developing next generation analytics platform, you might want to add flexibility that end users can search with natural language plus let them query with different permutations and combinations. If we develop this with traditional approach, we will have to write lot of queries upfront or tune query engine to meet above feature but here is where AI (LLM) can really shine.

I will assume that reader is aware of LLM but no worries if you are not aware, here is the link . Let’s understand how LLM can help us to achieve above feature.

Biggest question for us will be how will LLM will understand which table to look , which column out of that table is useful for the user’s query. Answer is metadata, provide metadata to LLM about our database — metadata can be description of each table in database, columns in each table etc. One can even train LLM over custom data.

Demo:

In this demo, I have developed full stack windows app. However, data layer/query layer/ queries are written by AI(LLM) and executed dynamically by application. This is extremely powerful

Note: If you are going to use this, please run generated SQL by LLM through security module of your application so that you can be safe from SQL injection.

~ Happy Coding

References:

--

--

Sumeet More
Sumeet More

Written by Sumeet More

Software Engineer 2 at Microsoft | Backend Engineer and Architect| Blockchain & ML enthusiast | C#,.NET Core, Rust, Javascript and Go

Responses (1)