About Us Contact Us Privacy Policy Disclaimer Pakistan Teaching Jobs 2026-27

SQL for Data Science for Beginners: Free Courses, Practice Questions and a 4-Week Plan

SQL is the first skill to learn for data science, and the easiest one to prove. Before a data scientist builds a model, somebody has to get the data — and that somebody writes SQL. A recruiter can test your SQL in five minutes, in a browser, with no setup. This article gives you both halves of the skill: a table of free courses that were verified free on 9 November 2026, and 25 practice questions on a small bookshop database you build yourself. Every query in the bank was actually run in SQLite 3, and the expected answer is printed under each one — so you always know whether you got it right.

An extreme close-up of programming code on a computer screen, for learning SQL for data science

Photo by ThisIsEngineering on Pexels

What SQL is, in one paragraph

SQL (Structured Query Language) is the language you use to ask a database a question. A database stores information in tables — rows and columns, like a spreadsheet with rules. SELECT title FROM books WHERE price > 25; asks: "show me the titles of books that cost more than $25." That is almost the whole idea. The rest of SQL is variations on it: filter rows (WHERE), sort them (ORDER BY), combine tables (JOIN), summarise them (GROUP BY), and ask questions about questions (subqueries). Data scientists use SQL daily because the data lives in databases, not in CSV files somebody emailed them.

Why SQL before Python

Beginners usually ask whether to learn SQL or Python first. Learn SQL first, for three reasons. It is faster — you can write useful queries on day one, while Python takes weeks before it does anything with real data. It is more universal — every data job lists SQL, and the SQL you learn transfers between MySQL, PostgreSQL, SQLite and SQL Server almost unchanged. And it teaches you to think in tables, which is the mental model all of data science runs on. Python comes next — our free data science path puts Python in stage 2, after SQL.

The practice database

You will answer all 25 questions against one small database: an online bookshop with four tables. SQLite runs it — SQLite is built into Python, so there is nothing to install and no account to create. Open a Python prompt and paste the setup block below, then run the questions one at a time and compare your result with the expected output printed under each question.

CREATE TABLE customers (customer_id INTEGER PRIMARY KEY, first_name TEXT, last_name TEXT, country TEXT, signup_date TEXT);
CREATE TABLE books (book_id INTEGER PRIMARY KEY, title TEXT, author TEXT, genre TEXT, price REAL, published_year INTEGER);
CREATE TABLE orders (order_id INTEGER PRIMARY KEY, customer_id INTEGER, order_date TEXT, status TEXT);
CREATE TABLE order_items (item_id INTEGER PRIMARY KEY, order_id INTEGER, book_id INTEGER, quantity INTEGER, unit_price REAL);

INSERT INTO customers VALUES
 (1,'Ayesha','Khan','Pakistan','2025-03-14'),(2,'Omar','Farouk','Canada','2026-01-09'),
 (3,'Priya','Nair','India','2025-11-02'),(4,'Lucas','Meyer','Germany','2026-02-20'),
 (5,'Sara','Ahmed','Canada','2026-04-11'),(6,'Tom','Wilson','United Kingdom','2025-08-30'),
 (7,'Mei','Chen','Singapore','2026-05-05'),(8,'Noah','Smith',NULL,'2026-06-18');
INSERT INTO books VALUES
 (1,'Python for Data Analysis','Wes McKinney','Data Science',34.99,2022),
 (2,'Storytelling with Data','Cole Knaflic','Data Science',27.50,2015),
 (3,'SQL in 10 Minutes','Ben Forta','Databases',19.99,2020),
 (4,'Learning SQL','Alan Beaulieu','Databases',29.99,2020),
 (5,'The Pragmatic Programmer','Andrew Hunt','Programming',42.00,2019),
 (6,'Clean Code','Robert Martin','Programming',38.50,2008),
 (7,'Python Crash Course','Eric Matthes','Programming',31.95,2023),
 (8,'Practical Statistics for Data Scientists','Peter Bruce','Data Science',44.99,2020),
 (9,'Designing Data-Intensive Applications','Martin Kleppmann','Databases',49.99,2017),
 (10,'Automate the Boring Stuff with Python','Al Sweigart','Programming',24.95,NULL),
 (11,'Data Science from Scratch','Joel Grus','Data Science',32.00,2019);
INSERT INTO orders VALUES
 (101,2,'2026-01-15','delivered'),(102,2,'2026-03-02','delivered'),(103,5,'2026-04-20','shipped'),
 (104,1,'2025-12-05','delivered'),(105,3,'2026-02-11','cancelled'),(106,4,'2026-05-19','delivered'),
 (107,5,'2026-06-02','delivered'),(108,6,'2025-09-14','delivered'),(109,7,'2026-06-25','shipped'),
 (110,2,'2026-07-01','delivered');
INSERT INTO order_items VALUES
 (1,101,1,1,34.99),(2,101,3,2,19.99),(3,102,8,1,44.99),(4,103,2,1,27.50),
 (5,104,7,1,31.95),(6,105,4,1,29.99),(7,106,9,1,49.99),(8,107,1,1,34.99),
 (9,107,6,1,38.50),(10,108,5,2,42.00),(11,109,10,1,24.95),(12,110,2,3,27.50),
 (13,110,3,1,19.99),(14,102,6,1,38.50);

Two things to notice in the data before you start. Noah Smith has no country listed (NULL) — question 3 will show you what that does to a DISTINCT query. And one book has no publication year (NULL) — question 4 will show you what that does to sorting. Real data is messy like this; the practice database is honest about it.

Part 1: SELECT basics (questions 1–5)

Q1. List the title and author of every Data Science book.

SELECT title, author FROM books WHERE genre = 'Data Science';

Expected output — 4 rows: Python for Data Analysis (Wes McKinney); Storytelling with Data (Cole Knaflic); Practical Statistics for Data Scientists (Peter Bruce); Data Science from Scratch (Joel Grus). The WHERE clause filters rows before anything else happens — learn to read a query from the WHERE outwards.

Q2. List the title and price of books costing more than $25, most expensive first.

SELECT title, price FROM books WHERE price > 25 ORDER BY price DESC;

Expected output — 9 rows: Designing Data-Intensive Applications ($49.99); Practical Statistics for Data Scientists ($44.99); The Pragmatic Programmer ($42.00); Clean Code ($38.50); Python for Data Analysis ($34.99); Data Science from Scratch ($32.00); Python Crash Course ($31.95); Learning SQL ($29.99); Storytelling with Data ($27.50). Note that $25.00 itself would not match — > is strictly greater than.

Q3. List every country our customers come from, without duplicates.

SELECT DISTINCT country FROM customers;

Expected output — 7 rows: Pakistan, Canada, India, Germany, United Kingdom, Singapore, and one blank row. That blank row is Noah Smith's NULL country: DISTINCT treats NULL as a value of its own. In a real report you would usually filter it out with WHERE country IS NOT NULL.

Q4. List the titles and publication years of the five oldest books (ignoring books with no year).

SELECT title, published_year FROM books WHERE published_year IS NOT NULL ORDER BY published_year ASC, title ASC LIMIT 5;

Expected output — 5 rows: Clean Code (2008); Storytelling with Data (2015); Designing Data-Intensive Applications (2017); Data Science from Scratch (2019); The Pragmatic Programmer (2019). Two lessons here: SQLite sorts NULL first in ascending order, which is why the WHERE clause is needed; and the second sort key (title ASC) breaks the 2019 tie deterministically.

Q5. How many books are in the catalogue?

SELECT COUNT(*) AS book_count FROM books;

Expected output — 11. COUNT(*) counts rows; COUNT(published_year) would count only 10, because it skips NULL. When a question says "how many", check whether missing values should count.

Part 2: Filtering and sorting (questions 6–10)

Q6. List the title and price of books priced between $20 and $30.

SELECT title, price FROM books WHERE price BETWEEN 20 AND 30;

Expected output — 3 rows: Storytelling with Data ($27.50); Learning SQL ($29.99); Automate the Boring Stuff with Python ($24.95). BETWEEN includes both ends — a $20.00 or $30.00 book would match.

Q7. List the first name, last name and country of customers from Canada or the United Kingdom.

SELECT first_name, last_name, country FROM customers WHERE country IN ('Canada','United Kingdom');

Expected output — 3 rows: Omar Farouk (Canada); Sara Ahmed (Canada); Tom Wilson (United Kingdom). IN (...) is shorthand for a chain of OR conditions and is much easier to read when the list grows.

Q8. List the titles of books with "Python" in the title.

SELECT title FROM books WHERE title LIKE '%Python%';

Expected output — 3 rows: Python for Data Analysis; Python Crash Course; Automate the Boring Stuff with Python. The % signs are wildcards: anything (or nothing) may come before and after "Python". Without them, LIKE 'Python' would match only a title that is exactly "Python".

Q9. List the order ID and date of delivered orders placed on or after 1 January 2026.

SELECT order_id, order_date FROM orders WHERE status = 'delivered' AND order_date >= '2026-01-01';

Expected output — 5 rows: 101 (2026-01-15); 102 (2026-03-02); 106 (2026-05-19); 107 (2026-06-02); 110 (2026-07-01). Dates stored as YYYY-MM-DD text compare correctly as strings — one more reason to store dates in that format and never as 15/01/2026.

Q10. Which book has no publication year recorded?

SELECT title FROM books WHERE published_year IS NULL;

Expected output — 1 row: Automate the Boring Stuff with Python. Never write = NULL: in SQL, NULL = NULL is not true, it is NULL, so = NULL matches nothing. IS NULL is the only correct test.

A close-up of a computer screen displaying programming code in a dark room, for SQL practice questions

Photo by luis gomes on Pexels

Part 3: Aggregates and GROUP BY (questions 11–15)

Q11. What is the total revenue from all order items?

SELECT ROUND(SUM(quantity * unit_price), 2) AS total_revenue FROM order_items;

Expected output — $582.82. Note this counts the cancelled order 105 ($29.99) too, because order_items does not know the order was cancelled. Question 24 shows the corrected figure. Always ask what a total includes before you trust it.

Q12. What is the average book price in each genre?

SELECT genre, ROUND(AVG(price), 2) AS avg_price FROM books GROUP BY genre;

Expected output — 3 rows: Data Science ($34.87); Databases ($33.32); Programming ($34.35). The averages are close, which is itself a finding: genre tells you almost nothing about price in this little catalogue.

Q13. How many orders are in each status — but only for statuses with more than one order?

SELECT status, COUNT(*) AS n FROM orders GROUP BY status HAVING COUNT(*) > 1;

Expected output — 2 rows: delivered (7); shipped (2). The cancelled status (1 order) is filtered out by HAVING, which filters groups after GROUP BY — WHERE cannot do this job, because WHERE runs before grouping.

Q14. Which book is the most expensive, and what does it cost?

SELECT title, MAX(price) AS max_price FROM books;

Expected output — Designing Data-Intensive Applications ($49.99). A common beginner mistake is writing SELECT title, MAX(price) FROM books and expecting the title of the most expensive book — in SQLite that happens to work, but in most databases it is an error or returns a random title. The safe pattern is ORDER BY price DESC LIMIT 1.

Q15. How many customers signed up in 2026?

SELECT COUNT(*) AS signups_2026 FROM customers WHERE signup_date LIKE '2026%';

Expected output — 5. LIKE '2026%' matches any date starting with 2026. It works because the dates are stored as YYYY-MM-DD text; with real date columns you would use a year function instead.

Part 4: JOINs (questions 16–20)

Q16. List every order with the first and last name of the customer who placed it.

SELECT o.order_id, c.first_name, c.last_name FROM orders o JOIN customers c ON o.customer_id = c.customer_id ORDER BY o.order_id;

Expected output — 10 rows, one per order: 101 Omar Farouk; 102 Omar Farouk; 103 Sara Ahmed; 104 Ayesha Khan; 105 Priya Nair; 106 Lucas Meyer; 107 Sara Ahmed; 108 Tom Wilson; 109 Mei Chen; 110 Omar Farouk. The o and c are table aliases — they save typing and make the ON condition readable.

Q17. Which books did Omar Farouk (customer 2) order?

SELECT DISTINCT b.title FROM books b JOIN order_items i ON b.book_id = i.book_id JOIN orders o ON i.order_id = o.order_id WHERE o.customer_id = 2;

Expected output — 5 titles: Python for Data Analysis; SQL in 10 Minutes; Practical Statistics for Data Scientists; Storytelling with Data; Clean Code. Three tables, two joins: orders → items → books. DISTINCT is needed because Omar ordered Storytelling with Data in two different orders (102 and 110).

Q18. Which customers have never placed an order?

SELECT c.first_name, c.last_name FROM customers c LEFT JOIN orders o ON c.customer_id = o.customer_id WHERE o.order_id IS NULL;

Expected output — 1 row: Noah Smith. A LEFT JOIN keeps every customer even with no matching order, and the missing order shows up as NULL — so WHERE o.order_id IS NULL finds exactly the customers with no orders. This is the single most useful interview pattern in SQL.

Q19. How much has each customer spent in total, most first?

SELECT c.first_name, c.last_name, ROUND(SUM(i.quantity * i.unit_price), 2) AS total_spent FROM customers c JOIN orders o ON c.customer_id = o.customer_id JOIN order_items i ON o.order_id = i.order_id GROUP BY c.customer_id ORDER BY total_spent DESC;

Expected output — 7 rows: Omar Farouk ($260.95); Sara Ahmed ($100.99); Tom Wilson ($84.00); Lucas Meyer ($49.99); Ayesha Khan ($31.95); Priya Nair ($29.99); Mei Chen ($24.95). Noah Smith does not appear: an inner JOIN drops customers with no orders. (Question 18 is the fix for that.)

Q20. Which books have never been ordered?

SELECT b.title FROM books b LEFT JOIN order_items i ON b.book_id = i.book_id WHERE i.item_id IS NULL;

Expected output — 1 row: Data Science from Scratch. Same LEFT JOIN … IS NULL pattern as question 18, on a different pair of tables. If you understand these two questions, you understand the pattern — it appears in almost every SQL interview.

Eyeglasses reflecting computer code on a monitor, for SQL joins and window functions

Photo by Kevin Ku on Pexels

Part 5: Subqueries, CTEs and window functions (questions 21–25)

Q21. List the books priced above the average book price.

SELECT title, price FROM books WHERE price > (SELECT AVG(price) FROM books);

Expected output — 5 rows: Python for Data Analysis ($34.99); The Pragmatic Programmer ($42.00); Clean Code ($38.50); Practical Statistics for Data Scientists ($44.99); Designing Data-Intensive Applications ($49.99). The subquery runs first and returns one number (the average, about $34.26); the outer query then compares every price against it.

Q22. Which customers spent more than the average customer?

WITH spent AS (SELECT o.customer_id, SUM(i.quantity * i.unit_price) AS total FROM orders o JOIN order_items i ON o.order_id = i.order_id GROUP BY o.customer_id) SELECT c.first_name, c.last_name, ROUND(s.total, 2) AS total FROM spent s JOIN customers c ON s.customer_id = c.customer_id WHERE s.total > (SELECT AVG(total) FROM spent);

Expected output — 3 rows: Omar Farouk ($260.95); Sara Ahmed ($100.99); Tom Wilson ($84.00). The average spend is $83.26, so Tom clears it by 74 cents. A CTE (WITH …) names an intermediate result so the query reads top-to-bottom instead of inside-out — use one whenever a subquery appears twice.

Q23. Rank the books by price within each genre, most expensive first.

SELECT genre, title, price, ROW_NUMBER() OVER (PARTITION BY genre ORDER BY price DESC) AS price_rank FROM books;

Expected output — 11 rows. Data Science: Practical Statistics for Data Scientists ($44.99, rank 1); Python for Data Analysis ($34.99, 2); Data Science from Scratch ($32.00, 3); Storytelling with Data ($27.50, 4). Databases: Designing Data-Intensive Applications ($49.99, 1); Learning SQL ($29.99, 2); SQL in 10 Minutes ($19.99, 3). Programming: The Pragmatic Programmer ($42.00, 1); Clean Code ($38.50, 2); Python Crash Course ($31.95, 3); Automate the Boring Stuff with Python ($24.95, 4). PARTITION BY restarts the numbering for each genre — it is GROUP BY for window functions, except it keeps every row.

Q24. Show the daily revenue and a running total, excluding cancelled orders.

SELECT o.order_date, ROUND(SUM(i.quantity * i.unit_price), 2) AS day_total, ROUND(SUM(SUM(i.quantity * i.unit_price)) OVER (ORDER BY o.order_date), 2) AS running_total FROM orders o JOIN order_items i ON o.order_id = i.order_id WHERE o.status <> 'cancelled' GROUP BY o.order_date ORDER BY o.order_date;

Expected output — 9 rows, running total rising from $84.00 (2025-09-14) to $552.83 (2026-07-01). Compare with question 11's $582.82: the $29.99 difference is exactly the cancelled order. A running total is the window-function version of a cumulative sum, and it is the query behind every "growth over time" chart.

Q25. Which three genres earned the most revenue?

WITH genre_rev AS (SELECT b.genre, SUM(i.quantity * i.unit_price) AS revenue FROM books b JOIN order_items i ON b.book_id = i.book_id GROUP BY b.genre) SELECT genre, ROUND(revenue, 2) AS revenue FROM genre_rev ORDER BY revenue DESC LIMIT 3;

Expected output — 3 rows: Data Science ($224.97); Programming ($217.90); Databases ($139.95). Data Science wins on revenue even though its average price is ordinary — it simply sold more copies. That is the difference between an average and a total, and it is why analysts report both.

Watch out: SQL dialects differ

The queries above run in SQLite, and almost all of them run unchanged in MySQL and PostgreSQL. Four differences catch beginners:

  • RIGHT JOIN and FULL OUTER JOIN work in SQLite 3.39.0 and later (June 2022); older SQLite versions had neither, so LEFT JOIN with the table order swapped still works everywhere. MySQL has no FULL OUTER JOIN.
  • Concatenation differs. SQLite and PostgreSQL join text with || (first_name || ' ' || last_name); MySQL and SQL Server use CONCAT().
  • LIMIT vs TOP. SQLite, MySQL and PostgreSQL limit rows with LIMIT 5; SQL Server uses SELECT TOP 5.
  • Window functions need a modern version. ROW_NUMBER() and SUM() OVER (...) work in SQLite 3.25+, MySQL 8+ and PostgreSQL — but not in MySQL 5.7, which still runs on many shared hosts. If a window query fails on your system, check the version first.

Verified free courses at a glance

Course Provider Time Best for Certificate Free?
Python and SQL for Data Science Scaler 14h 46m Learning SQL + Python together Free certificate Yes
SQL using MySQL Scaler 4h 50m SQL from zero, with MySQL Free certificate Yes
SQL for Data Science Great Learning 4.5 hours Joins, subqueries, SQL + Python + Power BI Free (course) Yes
Intro to SQL Kaggle Learn ~3 hours Real BigQuery datasets Free certificate Yes
Introduction to Databases with SQL Harvard CS50 30 hours Thorough, academic SQL + design Free certificate Yes
Relational Database freeCodeCamp ~60 hours PostgreSQL projects, deepest free option Free certificate Yes
Intro to SQL Khan Academy ~8 hours Gentlest start, videos + exercises None Yes
SQL Tutorial Mode 30–40 hours SQL for analytics, window functions None Yes
SQLBolt SQLBolt 2–3 hours Fast interactive lessons, no account None Yes
Learn SQL Scrimba 3.8 hours Interactive video + coding Not stated Yes
Data Analytics Essentials Cisco Networking Academy ~30 hours Excel + SQL + Tableau + badge Digital badge Yes

One honest flag: DataCamp appears in many "free SQL" lists, but it is freemium — the first chapter of its courses is free and the rest is paid. It is a fine reference, but it is not on the free table above for that reason. If a course asks for a card before the first lesson, it is not free, whatever the headline says.

Your 4-week plan

Week Do this Done when…
1 Build the practice database above. Do questions 1–10, one at a time, without peeking at the answers. You can write SELECT … WHERE … ORDER BY from memory
2 Take one free course from the table (Kaggle or Scaler for speed, CS50 for depth). Do questions 11–15. You can explain GROUP BY vs WHERE vs HAVING to a friend
3 Do questions 16–20. Then re-do them with different data — change a price, add a customer, and check the answers change as expected. LEFT JOIN … IS NULL feels automatic
4 Do questions 21–25. Then move to our 60 SQL interview questions and answer 10 a day. You can rank, running-total and compare-to-average without notes

Frequently asked questions

Is SQL or Python more important for data science? SQL first, Python second. SQL gets the data; Python analyses it. Most data interviews test SQL before Python, and SQL takes days to become useful where Python takes weeks.

Can I learn SQL for free with a certificate? Yes — Scaler, Kaggle Learn, Harvard CS50 and freeCodeCamp all issue free certificates for their SQL courses (see the table). The certificate proves you finished; the practice bank above proves you can do it.

How long does it take to learn SQL for data work? The basics (this article's parts 1–3) take about a week at an hour a day. Joins and aggregation (part 4) take a second week. Window functions and CTEs (part 5) take a third. Four weeks, one hour a day, is a realistic plan — see the table above.

Which SQL dialect should I learn? Standard SQL, the kind in this article. It transfers between SQLite, MySQL and PostgreSQL almost unchanged. Learn one database well; the differences in the dialect box above are the only ones that will surprise you.

Do I need to install anything? No. SQLite is built into Python — open a Python prompt, type import sqlite3, and paste the setup block. When you are ready for a bigger database, the same queries run in MySQL or PostgreSQL.

Work through the 25 questions in order, without skipping the ones that look easy — question 3's NULL and question 11's cancelled order are the traps that catch people in real interviews. When all 25 answers match, move on to the 60 SQL interview questions, then start the Python half of the free data science path.

Post a Comment

Previous Post Next Post