SQL tutorial for beginners with Chatgpt

SQL tutorial for beginners with Chatgpt

Free SQL tutorial for Beginners with Chatgpt - Free & Fast

Watch the video on my channel, or read the article below

We'll dive into a detailed 1-month study plan, tackle some SQL tasks from data analysts/scientist/engineer interviews, and even discuss the best strategies for learning SQL effectively. And you will know WHY I regret not having ChatGPT.

To begin our SQL learning journey, let's create a study plan. Remember, consistency is key when it comes to mastering a new skill. So, how can we create a 1-month study plan divided into 2-3 day splits for each topic.

Here is our prompt:

I want to learn SQL, create an advanced detailed monthly plan for studying SQL with 2-3 days topic splits


Certainly! Here's an advanced and detailed monthly study plan for learning SQL with 2-3 day splits for each topic:

Month 1: Introduction to SQL and Basic Queries

Week 1:

  • Day 1: Introduction to Databases and SQL
  • Day 2-3: Installing and Setting Up SQL Environment
  • Day 4-5: Basic SQL Queries (SELECT, FROM, WHERE, ORDER BY)
  • Day 6: Practice Exercise: Retrieving Data from a Single Table

Week 2: Joins and Data Manipulation

  • Day 7: Inner Join and Outer Join
  • Day 8: Self-Joins and Cross-Joins
  • Day 9-10: Subqueries and Nested Queries
  • Day 11: Practice Exercise: Combining Data from Multiple Tables

Week 3: Filtering and Sorting Data

  • Day 12: Filtering Data with WHERE, IN, LIKE, and BETWEEN
  • Day 13: Sorting Data with ORDER BY
  • Day 14-15: Aggregating Data with GROUP BY and HAVING
  • Day 16: Practice Exercise: Analyzing and Filtering Data

Month 2: Advanced SQL Concepts and Database Design

Week 4: Advanced Queries and Functions

  • Day 17-18: Advanced Joins (LEFT JOIN, RIGHT JOIN, FULL JOIN)
  • Day 19: Set Operations (UNION, INTERSECT, EXCEPT)
  • Day 20: Conditional Statements (CASE, IF-ELSE)
  • Day 21: Practice Exercise: Complex Query Scenarios

Week 5: Database Design and Optimization

  • Day 22-23: Database Normalization (1NF, 2NF, 3NF)
  • Day 24: Indexing and Performance Optimization
  • Day 25-26: Stored Procedures and Functions
  • Day 27: Practice Exercise: Designing a Database Schema

Week 6: Advanced Database Concepts and Final Project

  • Day 28: Transactions and Concurrency Control
  • Day 29: Database Security and User Permissions
  • Day 30-31: Recap and Final Project

Throughout the month, allocate time for practice exercises, real-world examples, and interactive learning resources. Consider using SQL learning platforms, online tutorials, and practice websites to reinforce your understanding.

Remember, regular practice and hands-on experience are essential for mastering SQL. Good luck with your SQL learning journey!

okay, great, as you can see it’s quite robust. well, somehow it didn’t split it by the weeks correctly, but the daily split is quite accurate and I now do regret that I didn’t have such a tool when starting with SQL

The only thing I’d suggest is that it’s more beginner focused, in case you want to extend, try also



Certainly! Here's an outline of advanced SQL topics you can explore:

  1. Joins:
  2. Inner join
  3. Left join
  4. Right join
  5. Full join
  6. Cross join
  7. Self join
  8. Non-equi joins
  9. Subqueries and Derived Tables:
  10. Subqueries in SELECT, WHERE, and FROM clauses
  11. Correlated subqueries
  12. Scalar subqueries
  14. Derived tables
  15. Aggregation and Grouping:
  16. Aggregate functions (SUM, AVG, COUNT, MIN, MAX)
  17. GROUP BY clause
  18. HAVING clause
  19. Rollup and Cube operators
  20. Grouping sets
  21. Window Functions:
  22. OVER clause
  23. PARTITION BY clause
  24. ROWS/RANGE clause
  25. Ranking functions (ROW_NUMBER, RANK, DENSE_RANK)
  26. Aggregate functions with window frames
  27. Common Table Expressions (CTEs):
  28. Recursive CTEs
  29. Hierarchical queries
  30. Inline and named CTEs
  31. WITH clause
  32. Conditional Expressions:
  33. CASE statement
  34. COALESCE and NULLIF functions
  35. IF-ELSE logic
  36. Conditional aggregation
  37. Advanced Data Manipulation:
  39. UPDATE statement with joins
  40. DELETE statement with joins
  41. MERGE statement (UPSERT)
  42. Views and Materialized Views:
  43. Creating and using views
  44. Updating views
  45. Materialized views for performance optimization
  46. Indexed views
  47. Stored Procedures and Functions:
  48. Creating and calling stored procedures
  49. Parameters and return values
  50. Control flow and branching
  51. Error handling and transactions
  52. Indexing and Performance Optimization:
  53. Index types (B-tree, Hash, Bitmap, etc.)
  54. Indexing strategies
  55. Query optimization techniques
  56. Analyzing query execution plans
  57. Database Security:
  58. User management and access control
  59. Role-based security
  60. Encryption and data protection
  61. Auditing and logging
  62. Advanced Database Concepts:
  63. Data normalization (1NF, 2NF, 3NF)
  64. Denormalization techniques
  65. Database replication
  66. Partitioning and sharding

Remember to dive deeper into each topic, practice writing queries, and explore advanced SQL features specific to the database management system you're using. Hands-on experience with real-world scenarios and datasets will further enhance your understanding of these advanced SQL concepts.

The plan looks great, but how can we dive deep into each topic?

Let’s start with Day 1. I’d suggest using such a prompt structure :

I want to become a good Data Analyst, explain to me extensively the topic of "Introduction to Databases and SQL”


Certainly! "Introduction to Databases and SQL"

 is a fundamental topic in data analytics that forms the basis for understanding how databases work and how SQL (Structured Query Language) is used to interact with them. Let's dive into an extensive explanation of this topic:

  1. Databases: A database is an organized collection of structured data that is stored and managed for efficient retrieval and manipulation. It provides a structured way to store, organize, and manage large amounts of data. Databases can be classified into different types, such as relational databases, NoSQL databases, or object-oriented databases.
  2. Relational Databases: Relational databases are the most common type of databases used in data analytics. They store data in tables consisting of rows and columns, where each column represents a specific attribute or field, and each row represents a record or entry. These tables are related to each other through keys, establishing relationships between the data.
  3. SQL: SQL, or Structured Query Language, is a programming language used for managing and manipulating data in relational databases. It provides a standardized way to communicate with databases, allowing users to retrieve, insert, update, and delete data. SQL is not only used to interact with data but also to define and manage the structure of databases, such as creating tables, defining relationships, and setting constraints.
  4. Basic SQL Queries: SQL queries are statements written in SQL that instruct the database to perform specific operations. Here are some fundamental SQL queries:
  5. SELECT: Retrieves data from one or more tables based on specified conditions.
  6. INSERT: Inserts new data into a table.
  7. UPDATE: Modifies existing data in a table.
  8. DELETE: Removes data from a table.
  9. CREATE: Creates a new table, view, or other database objects.
  10. ALTER: Modifies the structure of an existing table.
  11. DROP: Deletes a table, view, or other database objects.
  12. Querying with SQL: To retrieve data from a database, you use the SELECT statement along with various clauses to filter, sort, and group the data. Some commonly used clauses include:
  13. WHERE: Filters data based on specific conditions.
  14. ORDER BY: Sorts the result set based on one or more columns.
  15. GROUP BY: Groups the result set based on one or more columns.
  16. HAVING: Filters data based on conditions after grouping.

By combining these clauses, you can perform complex queries to extract valuable insights from your data.

  1. Database Management Systems (DBMS): A Database Management System (DBMS) is software that enables the creation, management, and administration of databases. It provides an interface to interact with databases and handles tasks such as data storage, security, backup, and recovery. Popular DBMSs include MySQL, PostgreSQL, Oracle, and Microsoft SQL Server.

Understanding the basics of databases and SQL is crucial for data analysts as it allows them to extract and manipulate data efficiently. It forms the foundation for more advanced topics in data analytics, such as data modeling, data warehousing, and data manipulation.

To enhance your understanding, consider practicing SQL queries, working with sample databases, and exploring online tutorials and exercises. Remember, hands-on experience and continuous learning are key to becoming proficient in data analytics.

So as you see, prompting each topic in such a manner will give you the information needed.

Here are a few more examples.

Let’s tackle the next one - WHERE CLAUSE

I want to learn SQL, explain to me the topic of "Filtering Data with WHERE Clause” comprehensively


Certainly! The topic "Filtering Data with WHERE Clause

" is a fundamental aspect of SQL that allows you to extract specific subsets of data from a database based on certain conditions. Here's a detailed explanation of filtering data with the WHERE clause:

  1. Purpose of the WHERE Clause: The WHERE clause is used in SQL queries to filter rows from a table based on specific conditions. It allows you to define conditions that must be met by the data in order to be included in the query result.
  2. Syntax of the WHERE Clause: The WHERE clause is typically used in conjunction with the SELECT statement. Here's the basic syntax:

SELECT: Specifies the columns you want to retrieve from the table.

FROM: Specifies the table from which you want to retrieve the data.

WHERE: Specifies the condition(s) that the data must meet.

3. Using Comparison Operators: The WHERE clause employs comparison operators to define conditions. Some commonly used operators include:

=: Equals

<> or !=: Not equal to

<: Less than

>: Greater than

<=: Less than or equal to

>=: Greater than or equal to

4. Combining Conditions: You can use logical operators (AND, OR, NOT) to combine multiple conditions in the WHERE clause. This allows for more complex filtering of data.

AND: Requires that all conditions must be true.

OR: Requires that at least one condition must be true.

NOT: Negates a condition, making it false.


  • Retrieve all customers from the "Customers" table where the "Country" is 'USA':

SELECT * FROM Customers WHERE Country = 'USA';

  • Retrieve products from the "Products" table where the price is greater than or equal to 50:

SELECT * FROM Products WHERE Price >= 50;

  • Retrieve employees from the "Employees" table whose salary is between 50000 and 70000:

SELECT * FROM Employees WHERE Salary BETWEEN 50000 AND 70000;

5. Additional Operators: Apart from comparison operators, the WHERE clause also supports other operators like IN, LIKE, and IS NULL to handle more specific filtering scenarios.

IN: Matches a value against a list of specified values.

LIKE: Matches a value against a specified pattern using wildcard characters.

IS NULL: Matches rows where a column is NULL (empty).

The WHERE clause is a powerful tool that allows you to retrieve specific subsets of data from a database based on conditions. By mastering the filtering capabilities of the WHERE clause, you can efficiently extract the data you need for analysis and reporting in SQL.

As you can see, it not only gave you plain text, but actual SQL queries to start eyeing those.

But you need to learn by doing, right? Okay, let’s update our previous prompt a bit

I want to learn SQL topic of "Filtering Data with WHERE Clause”, give me a task with actual data without a solution


Certainly! Here's a task for you to practice filtering data with the WHERE clause: Task: Filtering Customers by Age Range

Description: You are given a "Customers" table with columns such as "CustomerID", "CustomerName", "Age", and "City". Your task is to retrieve the details of customers within a specific age range. For this task, consider the age range from 25 to 40 years.

Example Input Data:

Customers table:

CustomerID CustomerName Age City
1 John 32 New York
2 Sarah 28 London
3 Michael 42 San Francisco
4 Emma 35 Paris
5 Alex 23 Sydney

Task: Write an SQL query to retrieve the details of customers within the age range of 25 to 40 years.

Now it's your turn to solve the task and write the SQL query to filter the customers based on the given age range. Good luck!

Great! Now I’d suggest you write your solution somewhere else (like in Sublime, Pycharm, any text editor, whatever) and let’s go back to check

Is this solution correct?
FROM Orders
WHERE OrderDate BETWEEN '2022-01-01' AND '2022-03-31';


Yes, the solution you provided is correct! The SQL query:

SELECT * FROM Orders WHERE OrderDate BETWEEN '2022-01-01' AND '2022-03-31';

will retrieve all columns (*) from the "Orders" table where the "OrderDate" is between '2022-01-01' and '2022-03-31'.

This query effectively filters the orders based on the specified date range. Well done!

You see how nicely it’s formatted and included a short explanation under it.

Okay, the last one, more advanced. Let’s test how it’s gonna perform:

I'm a newbie in SQL, explain to me comprehensively the topic "Advanced Joins (LEFT JOIN, RIGHT JOIN, FULL JOIN)”


Certainly! Let's explore the topic of "Advanced Joins (LEFT JOIN, RIGHT JOIN, FULL JOIN)" comprehensively, along with examples:

  1. Joins and their Purpose: Joins in SQL are used to combine data from multiple tables based on a related column between them. They allow you to retrieve a comprehensive view of the data by connecting tables with common information.
  2. Basic Join: INNER JOIN The INNER JOIN returns only the rows that have matching values in both tables being joined. It selects records where the join condition is met.
  3. LEFT JOIN: The LEFT JOIN retrieves all records from the left table (the "left" side of the JOIN keyword) and matching records from the right table. If there is no match, NULL values are returned for the columns of the right table.


Consider two tables, "Customers" and "Orders":

Customers table:

CustomerID CustomerName
1 John
2 Sarah
3 Michael

Orders table:

OrderID CustomerID OrderDate
101 1 2022-01-01
102 2 2022-02-01
103 4 2022-03-01

To perform a LEFT JOIN to retrieve all customers and their orders (if any), you would use the following query:

SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
ON Customers.CustomerID = Orders.CustomerID;

The result would be:

CustomerName OrderID
John 101
Sarah 102
Michael NULL

As you can see, the LEFT JOIN retrieves all customers, and for the customers who have orders, it includes the order details. For customers without orders, NULL values are displayed for the order-related columns.

  1. RIGHT JOIN: The RIGHT JOIN is the opposite of the LEFT JOIN. It retrieves all records from the right table (the "right" side of the JOIN keyword) and matching records from the left table. If there is no match, NULL values are returned for the columns of the left table.


To perform a RIGHT JOIN to retrieve all orders and their corresponding customers (if any), you would use the following query:

SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
ON Customers.CustomerID = Orders.CustomerID;

The result would be:

CustomerName OrderID
John 101
Sarah 102
NULL 103

The RIGHT JOIN retrieves all orders, and for the orders that have matching customers, it includes the customer details. For orders without a matching customer, NULL values are displayed for the customer-related columns.

  1. FULL JOIN: A FULL JOIN returns all rows from both tables, including unmatched rows from either table. If a row has no match in the opposing table, NULL values are returned for the columns of the table without a match.


To perform a FULL JOIN to retrieve all customers and orders, regardless of matching records, you would use the following query:

SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
ON Customers.CustomerID = Orders.CustomerID;

The result would be:

CustomerName OrderID
John 101
Sarah 102
Michael NULL
NULL 103

Many visual examples and code, so you can proceed with that.

However, in case you are stuck and don’t understand what’s going on, you can always use “Child prompting” :) Yeah, that famous “explain to a child” postfix to any of your questions”

Let’s rephrase the last one if you didn’t get it correct:

I'm a newbie in SQL, explain to me comprehensively the topic "Advanced Joins (LEFT JOIN, RIGHT JOIN, FULL JOIN)” like to a child


Sure! Let's explain the topic of "Advanced Joins (LEFT JOIN, RIGHT JOIN, FULL JOIN)" in a simple and comprehensive way, as if explaining it to a child:

Imagine you have two sets of toys. One set has different types of animals, and the other set has different types of houses. You want to connect the animals with their corresponding houses based on their similarities.

A LEFT JOIN is like taking all the animals from the animal set and matching them with the houses from the house set. If an animal doesn't have a corresponding house, we still keep the animal but leave the house empty.

A RIGHT JOIN is similar, but this time we take all the houses from the house set and match them with the animals from the animal set. If a house doesn't have a corresponding animal, we keep the house but leave the animal empty.

Now, a FULL JOIN is like combining both sets of toys. We take all the animals and all the houses, matching them together based on their similarities. If an animal doesn't have a house, we keep it with an empty house, and if a house doesn't have an animal, we keep it with an empty animal.

In summary, LEFT JOIN keeps all animals and matches them with houses if possible, RIGHT JOIN keeps all houses and matches them with animals if possible, and FULL JOIN combines both sets, keeping all animals and all houses together.

Joins in SQL work similarly but with tables of data instead of toys. They help us connect data from different tables based on their relationships, just like matching animals with houses. By using these types of joins, we can get a complete picture of the data and find connections between different sets of information.

So it’s using the example of toys. Also, if you don’t like the output, just ask to remake, like “Could you use another analogy?”

How to get real-world examples?

First, I want to show you the capabilities of Chatgpt in generating its own tasks + solutions

Secondly, I will share with you a couple of real SQL questions that I got at different Data Engineering interviews, whether it was live coding sessions or home assignments, and we’ll test how accurately it answers those.

Part 1

I suggest using the following formula when you want to practice: you assign a role, define the complexity level of the task and ask to provide the example of input data.

I am being interviewed for a Data Analyst role, give me a medium complexity SQL interview task along with a detailed description and example of input data


Certainly! Here's a medium complexity SQL interview task suitable for a Data Analyst role 

along with a detailed description and an example of input data:

Task: Analyzing Product Sales

Description: You are given three tables: "Products," "Orders," and "Customers." Your task is to write SQL queries to analyze product sales data and provide insights about customer orders and product performance. The tables have the following structure:

Table: Products

  • product_id (integer)
  • product_name (string)
  • category (string)
  • price (decimal)

Table: Orders

  • order_id (integer)
  • customer_id (integer)
  • product_id (integer)
  • order_date (date)
  • quantity (integer)

Table: Customers

  • customer_id (integer)
  • first_name (string)
  • last_name (string)
  • city (string)
  • country (string)

Example Input Data:

Table: Products +------------+--------------+------------+-------+ | product_id | product_name | category | price | +------------+--------------+------------+-------+ | 1 | iPhone 12 | Electronics| 999.99| | 2 | MacBook Pro | Electronics| 1999.99| | 3 | Kindle | Electronics| 149.99| | 4 | Running Shoes| Sports | 79.99 | +------------+--------------+------------+-------+

Table: Orders +----------+-------------+------------+------------+---------+ | order_id | customer_id | product_id | order_date | quantity| +----------+-------------+------------+------------+---------+ | 1 | 1001 | 1 | 2023-05-01 | 2 | | 2 | 1002 | 3 | 2023-05-02 | 1 | | 3 | 1001 | 2 | 2023-05-03 | 1 | | 4 | 1003 | 4 | 2023-05-04 | 3 | | 5 | 1002 | 1 | 2023-05-05 | 2 | +----------+-------------+------------+------------+---------+

Table: Customers +-------------+------------+-----------+----------+---------+ | customer_id | first_name | last_name | city | country | +-------------+------------+-----------+----------+---------+ | 1001 | John | Doe | New York | USA | | 1002 | Jane | Smith | London | UK | | 1003 | Mark | Johnson | Sydney | Australia| +-------------+------------+-----------+----------+---------+

Now, using the provided data, write SQL queries to solve the following tasks:

  1. Retrieve the total number of orders.
  2. Calculate the total sales revenue.
  3. Find the top-selling product (based on total quantity sold).
  4. Calculate the average quantity of products sold per order.
  5. Retrieve the details of customers who have purchased products from the "Electronics" category.

Remember to use appropriate SQL syntax and consider table joins, aggregations, and filtering based on the given tasks.

Note: You can refer to the provided example input data to write your SQL queries and solve the tasks.

Now, you wrote your answer and want to compare if those match:

now please give a solution to the above query


Certainly! Here's a solution to the sales performance analysis query:

  SUM(o.OrderQuantity * p.Price) AS TotalSalesRevenue,
  AVG(o.OrderQuantity * p.Price) AS AverageOrderValue
  Orders o
  Products p ON o.ProductID = p.ProductID
  TotalSalesRevenue DESC;

This query joins the "Orders" and "Products" tables on the common "ProductID" column. It calculates the total sales revenue by multiplying the order quantity with the price of each product and sums it up for each category. The average order value is calculated similarly using the AVG aggregation function. The result is then grouped by the product category and sorted in descending order of total sales revenue.

The output will display the category, total sales revenue, and average order value for each product category, sorted in descending order of total sales revenue.

Example Output:

Category TotalSalesRevenue AverageOrderValue
Electronics 525 175
Clothing 200 100
Books 20 20

In the output, the "Category" column represents the product category, the "TotalSalesRevenue" column represents the total sales revenue for each category, and the "AverageOrderValue" column represents the average value of orders for each category.

So you can generate as many questions as you want to practice. (Good rule of thumb is like 2-3 sql tasks a day for 10 days, to get the fluency)

Part 2

Okay, so now sharing a couple of real SQL questions that I got at different Data Engineering interviews.


To finish a class, students must pass four exams (exam ids: 1,2,3 and 4).

Given a table exam_scores containing the data about all of the exams that students took, form a new table to track the scores for each student.

Note: Students took each exam only once.


For the given input:

student_id student_name exam_id score
100 Anna 1 71
100 Anna 2 72
100 Anna 3 73
100 Anna 4 74
101 Brian 1 65

the expected output should be:

student_name exam_1 exam_2 exam_3 exam_4
Anna 71 72 73 74


exam_scores table

Column Type
student_id INTEGER
student_name VARCHAR
exam_id INTEGER


Column Type
student_name VARCHAR
exam_1 INT
exam_2 INT
exam_3 INT
exam_4 INT

Let’s copy it as is, without any adjustments and let’s see what it’s gonna say


WITH analyst_scientist AS (
    LAG(position_name) OVER (PARTITION BY user_id ORDER BY start_date) AS previous_position
  FROM user_experiences
  WHERE position_name IN ('Data Analyst', 'Data Scientist')
  (COUNT(DISTINCT CASE WHEN position_name = 'Data Scientist' AND previous_position = 'Data Analyst' THEN user_id END) * 100.0 /
  COUNT(DISTINCT CASE WHEN position_name = 'Data Scientist' THEN user_id END)) AS percentage
FROM analyst_scientist
GROUP BY position_name;

Yeap, it’s correct! Let’s move on


Write a query to identify customers who placed more than three transactions each in both 2019 and 2020.


transactions table

Column Type
user_id INTEGER
created_at DATETIME
product_id INTEGER
quantity INTEGER

users table

Column Type


Column Type
customer_name VARCHAR


  u.name AS customer_name
  users u
  transactions t ON u.id = t.user_id
  YEAR(t.created_at) IN (2019, 2020)
  u.id, u.name
  COUNT(DISTINCT CASE WHEN YEAR(t.created_at) = 2019 THEN t.id END) > 3
  AND COUNT(DISTINCT CASE WHEN YEAR(t.created_at) = 2020 THEN t.id END) > 3;

Awesome, it’s also the correct one. And the last one!


Write a SQL query to create a histogram of the number of comments per user in the month of January 2020.

Note: Assume bin buckets class intervals of one.

Note: Comments by users that were not created in January 2020 should be counted in a “0” bucket



users table

Columns Type
created_at DATETIME
neighborhood_id INTEGER

comments table

Columns Type
user_id INTEGER
created_at DATETIME


Column Type
comment_count INTEGER
frequency INTEGER


WITH comment_counts AS (
    u.id AS user_id,
    COUNT(c.user_id) AS comment_count
    users u
    comments c ON u.id = c.user_id
               AND EXTRACT(YEAR_MONTH FROM c.created_at) = '202001'
  COUNT(*) AS frequency

Prompting Strategy

When interacting with ChatGPT, it's essential to use clear and specific prompts. Instead of asking, 'How does SQL work?', try 'Explain the concept of SELECT statements in SQL.'

Clear not equals too short.

For better prompting use PROMPTER method:

P - PERSONA: specify role + expertise

R - REFERENCE: provide context

O - OBJECTIVE: define task

M - MARGINS: set clear conditions

P - PERSPECTIVE: request multiple outputs

T - THROUGHPUT: request clear outputs

E - EXAMPLES: show output examples

R - REFINE: Adjust instructions

Before we conclude, let's discuss a few drawbacks of using ChatGPT while studying SQL.

Firstly, ChatGPT might not catch syntax errors in your SQL queries, so it's crucial to test your code independently.

Additionally, while ChatGPT can provide explanations, it may not always offer deep insights into advanced SQL concepts. (especially if it’s vendor-related syntax, like Snowflake or BigQuery) Therefore, it's important to consult reliable SQL resources and seek guidance from experienced professionals

And there you have it, dears! A comprehensive plan to learn SQL for free with ChatGPT. Remember to stick to the study plan, practice the SQL tasks, use clear prompts, and be aware of the limitations.