How to write SQL queries using AI
đ§ Knowledge Series #55: Get the data you need without having to be an SQL pro
đThe Knowledge Series is available for paid subscribers. Get full ongoing access to 50+ explainers and tutorials to grow your technical knowledge at work. New guides added every month.
Hi everyone đ,
I've always been a firm believer that learning SQL is an essential skill for anyone who wants to be able to work with data without having to wait for other people to do the heavy lifting for them. But since the rise of new AI tools in the last 18 months or so, I'm not so sure.
In the briefing we mentioned that LinkedIn has launched its own internal SQL tool which lets employees query databases in natural language and the results so far are promising. 95% of users rated query accuracy as "Passes" or better and 40% of users rated query accuracy as "Very Good" or "Excellent".
The most used feature, though, was a âFix with AIâ feature which looks at an SQL query, debugs it and suggests fixes. Proving that a hybrid approach still seems to be the most effective way to use AI, for now at least.
And thatâs what this Knowledge Series is focused on: writing SQL queries with the help of AI so that you can quickly get the data you need without necessarily being a pro at writing SQL queries.
Coming up:
Whatâs SQL and why is it useful? A quick recap
5 useful SQL queries worth knowing for product teams
How to write these 5 useful SQL queries quickly using AI tools tools including ChatGPT, Perplexity, Claude and dedicated data analysis tools
How to build useful Claude Artifacts for working with data and SQL
Other ways to use AI with SQL including data visualization, debugging and more
Tools you can use and terminology you need to know when working with AI-powered SQL
Whatâs SQL and why is it useful? A quick recap
If youâre not familiar with SQL or itâs been a little while since you last used it, SQL is a database-specific programming language that is used by engineers, data scientists, product managers and other tech professionals who want to interact with databases in some way.
SQL stands for structured query language .
SQL retrieves information from a database. This process is known as âqueryingâ a database.
Whatâs querying?
Querying is like saying to your database: âhey database, show me all the customers on this specific pricing planâ and the database returns a result, known as a âresult setâ. The result is the answer to your query.
SQL isnât just used for writing reports; itâs commonly used in backend development along with programming languages since it allows engineers to integrate SQL queries within their applications.
Itâs become trendy over the past few years for companies to adopt NoSQL databases, too, but for data that requires a more rigid, trustworthy structure, SQL is still the go-to choice for most teams who need a database.
An SQL database might be designed up front by an engineer who assesses the business and designs a set of tables based on requirements. This is referred to as designing the schema of the database and weâll talk more about this when we look at how best to use AI to craft your own SQL queries.
How to write 5 useful SQL queries using AI
In these 5 examples, weâll assume that weâre writing SQL queries for reporting purposes. Iâve chosen these examples since they cover some of the most common (and useful) things youâd need to do with SQL. But since weâre also using natural language as our primary input, once youâve got used to the basic queries you can move onto more complex ones - even if youâre not an SQL pro.
Getting set up - telling ChatGPT / Perplexity about our database
For this first part weâll use ChatGPT and then weâll move onto Perplexity for some other examples to mix things up a bit.
First up, we need to tell ChatGPT about the structure of our database. If youâre only interested in learning how to use AI for writing basic queries and youâre not too bothered about hooking this up to a live database or matching a specific database that you use every day for now then this isnât a necessary step and you can skip right ahead to the SQL examples.
Note: if you havenât worked very closely with SQL or databases in the past, the schema is just a fancy word for the overall structure / design of a database
Since ChatGPT doesn't have an inherent knowledge of your database schema, pasting in relevant parts of the schema can significantly improve response accuracy. For example, if your database has tables with similar names or multiple related columns, providing the schema helps ChatGPT generate a more precise query.
Hereâs a simple prompt we could use to get us started: