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: