Generating-SQL-Queries-with-AI
Generating-SQL-Queries-with-AI
Generating-SQL-Queries-with-AI
Learning objectives
Leverage generative AI models to create efficient queries for your data set.
Improve the accuracy and performance of data retrieval and analysis tasks.
Introduction
BI analysts juggle with large volumes of data, writing intricate queries to access processed data
from database tables according to specific requirements. These activities are time-consuming and
require meticulous attention to detail for efficient data retrieval and analysis. However, consider a
BI analyst who leverages generative AI tools and agents to streamline the creation of optimized
queries. This allows them to focus on more strategic tasks, such as interpreting data insights and
supporting decision-making processes.
Processed data stored in a database table can be accessed using queries tailored to your
requirements. Since queries are a crucial part of a data professional's workflow, mastering the skill
of writing efficient queries is essential.
In this lab, you will learn how to leverage generative AI platforms to create optimized queries for
your data, provided you supply the model with sufficient context.
Note: The prompts given in this lab are for samples only. You can write prompts
based on your requirements to generate responses. However, you may get a different
response even if you use prompts from this lab.
In this exercise, you'll leverage OpenAI's AI-powered assistant, ChatGPT. ChatGPT is a powerful
1 of 11 1/3/2025, 12:05 PM
Generating-SQL-Queries-with-AI https://generative-ai-elevate-your-business-intelligence-f75b7d40f993...
language model designed to assist both consumers and businesses. It understands and processes
text, audio, video, and images. ChatGPT helps professionals streamline their tasks, and BI analysts
are no exception.
By using ChatGPT, BI analysts can simplify their workflow, create optimized queries, analyze data
efficiently, and focus more on strategic decision making and insights.
In this lab, you will leverage ChatGPT to create efficient SQL queries tailored to your specific data
analysis needs. You will start by providing a detailed description of your data set, including
attributes such as age, gender, and chest pain type, to give ChatGPT the necessary context. Using
this context, ChatGPT will then generate SQL queries for various data analysis tasks. For example,
you can prompt ChatGPT to create queries for obtaining age distribution, performing gender
analysis, determining the frequency of chest pain types, and investigating the distribution of heart
disease within different age groups. This process will enable you to efficiently generate and execute
SQL queries, enhancing your ability to analyze and interpret data as a BI analyst.
In this step, you will provide the model with a description of your data set to generate efficient and
readily usable queries tailored to your requirements for fetching the data.
For this lab, you will use the Heart Disease data set from the UCI ML library, available publicly
under the CCA 4.0 International license.
Note: You can download the data set and run the generated queries using any SQL querying
system.
You can access the ChatGPT platform using the link https://chatgpt.com/; right-click to open in a
new tab, and log in to ChatGPT. If you are a first-time user, set up OpenAI's ChatGPT account to
sign up.
Paste the following text in the input box to provide ChatGPT with the appropriate context for the
data.
In this lab, we will use the Heart Disease data set from the UCI Machine Learning Repository. This
dataset includes various features such as age, gender, chest pain type, cholesterol levels, and more.
The primary goal is to classify the presence of heart disease.
2 of 11 1/3/2025, 12:05 PM
Generating-SQL-Queries-with-AI https://generative-ai-elevate-your-business-intelligence-f75b7d40f993...
--Value 4: asymptomatic
--Value 0: normal
--Value 1: upsloping
--Value 2: flat
--Value 3: downsloping
num (the predicted attribute) - diagnosis of heart disease (angiographic disease status)
In this lab, we will use the Heart Disease data set from the UCI Machine Learning Repository. This
dataset includes various features such as age, gender, chest pain type, cholesterol levels, and more.
The primary goal is to classify the presence of heart disease.
3 of 11 1/3/2025, 12:05 PM
Generating-SQL-Queries-with-AI https://generative-ai-elevate-your-business-intelligence-f75b7d40f993...
--Value 0: normal
--Value 1: having ST-T wave abnormality (T wave inversions and/or ST
elevation or depression of > 0.05 mV)
--Value 2: showing probable or definite left ventricular hypertrophy by
Estes’ criteria
--Value 1: upsloping
--Value 2: flat
--Value 3: downsloping
num (the predicted attribute) - diagnosis of heart disease (angiographic disease status)
4 of 11 1/3/2025, 12:05 PM
Generating-SQL-Queries-with-AI https://generative-ai-elevate-your-business-intelligence-f75b7d40f993...
Select Enter on your keyboard or the top arrow sign on the screen to provide the input.
Once you have set the context, ChatGPT will have enough background to generate SQL queries for
your prompts. Consider the following prompts, asking ChatGPT to generate SQL queries for
different tasks:
Write an SQL query to find the minimum, maximum, and average age of patients in the data set.
Select Enter on your keyboard or the top arrow sign on the screen to obtain the response.
5 of 11 1/3/2025, 12:05 PM
Generating-SQL-Queries-with-AI https://generative-ai-elevate-your-business-intelligence-f75b7d40f993...
Write an SQL query to count the number of male and female patients in the data set.
Select Enter on your keyboard or the top arrow sign on the screen to obtain the response.
6 of 11 1/3/2025, 12:05 PM
Generating-SQL-Queries-with-AI https://generative-ai-elevate-your-business-intelligence-f75b7d40f993...
Write an SQL query to determine the frequency of each type of chest pain (typical angina, atypical
angina, nonanginal pain, asymptomatic) among patients.
Select Enter on your keyboard or the top arrow sign on the screen to obtain the response.
7 of 11 1/3/2025, 12:05 PM
Generating-SQL-Queries-with-AI https://generative-ai-elevate-your-business-intelligence-f75b7d40f993...
Write an SQL query to investigate the distribution of the target variable (presence or absence of
heart disease) within different age groups (e.g., 20-30, 30-40, etc.).
Select Enter on your keyboard or the top arrow sign on the screen to obtain the response.
8 of 11 1/3/2025, 12:05 PM
Generating-SQL-Queries-with-AI https://generative-ai-elevate-your-business-intelligence-f75b7d40f993...
9 of 11 1/3/2025, 12:05 PM
Generating-SQL-Queries-with-AI https://generative-ai-elevate-your-business-intelligence-f75b7d40f993...
Try yourself
Write clear and descriptive prompts to leverage the full potential of ChatGPT.
Try to generate queries for the data set for the following prompts:
Cholesterol range:
Determine the age range (youngest and oldest) for male and female patients separately.
Find the maximum heart rate achieved during exercise for different age groups (e.g., 30-40, 40-50,
etc.).
Calculate the percentage of patients with fasting blood sugar greater than 120 mg/dl.
Find the ratio of patients with abnormal resting electrocardiographic results to those with normal
results.
Count the number of patients with reversible thalassemia detected by thallium stress testing.
Calculate the average age of patients who experienced chest pain during diagnosis.
Summary
Congratulations on completing the hands-on lab Generating SQL Queries with AI.
In this lab, you've leveraged ChatGPT, an AI chatbot, to create personalized SQL queries for
efficiently extracting insights from large data sets. ChatGPT assists BI analysts in generating SQL
10 of 11 1/3/2025, 12:05 PM
Generating-SQL-Queries-with-AI https://generative-ai-elevate-your-business-intelligence-f75b7d40f993...
queries for various business intelligence tasks, enhancing their ability to retrieve data quickly and
accurately.
11 of 11 1/3/2025, 12:05 PM