Sitemap

๐Ÿ’ป SQL PRACTICE WORKSHEET (SQLite + Python)

6 min readApr 30, 2026

--

๐ŸŽฏ Objective

Learn how to use:

  • SELECT, FROM, WHERE
  • AND, OR, LIKE
  • INSERT, DELETE, UPDATE
  • ORDER BY
  • UNION
  • JOIN
  • DROP
  • TRUNCATE (not supported in SQLITE) would be DELETE

๐Ÿ› ๏ธ PART 1 โ€” SETTING UP (VS CODE + PYTHON)

Step 1: Create your project

  1. Open VS Code
  2. Create a new folder (e.g. sql-practice)
  3. Inside it, create:
  • main.py
  • practice.sql

Step 2: Add this Python code (main.py)

import sqlite3
def run_sql_file_with_output(filename, db_name='data.db'):
conn = sqlite3.connect(db_name)
cursor = conn.cursor()
with open(filename, 'r') as f:
sql_commands = f.read().split(';')
for command in sql_commands:
clean_command = command.strip()
if clean_command:
cursor.execute(clean_command)

results = cursor.fetchall()
if results:
print(f"--- Results for: {clean_command[:30]}... ---")
for row in results:
print(row)
print("-" * 30)
conn.commit()
conn.close()
if __name__ == "__main__":
run_sql_file_with_output('practice.sql', 'my_app.db')

Step 3: Run your file

In terminal:

python main.py

๐Ÿ—„๏ธ PART 2 โ€” DATABASE SETUP

๐Ÿงช Task 1: Create an EMPTY table

Example:

CREATE TABLE Country(
name VARCHAR(255),
continent VARCHAR(255),
area INT,
population INT,
gdp INT,
capital VARCHAR(255)
);

๐Ÿ” Your Task:

Create a new table called City with:

  • name
  • country
  • population

๐Ÿงช Task 2: Insert data

Example:

INSERT INTO Country(name, continent, area, population, gdp, capital)
VALUES ('Brazil','South America',8515767,214000000,1920090000000,'Brasilia');

๐Ÿ” Your Task:

Insert 3 countries of your choice

๐Ÿงช Task 3: Insert full dataset

Copy this into your file:

INSERT INTO Country(name, continent, area, population, gdp, capital)
VALUES
('Brazil','South America',8515767,214000000,1920090000000,'Brasilia'),
('Canada','North America',9984670,40000000,2137000000000,'Ottawa'),
('Germany','Europe',357022,83000000,4456000000000,'Berlin'),
('India','Asia',3287263,1428000000,3737000000000,'New Delhi'),
('Japan','Asia',377975,125000000,4204000000000,'Tokyo'),
('Nigeria','Africa',923768,223000000,477400000000,'Abuja'),
('Australia','Oceania',7692024,26000000,1693000000000,'Canberra'),
('France','Europe',551695,65000000,3030000000000,'Paris'),
('Mexico','North America',1964375,129000000,1810000000000,'Mexico City'),
('South Africa','Africa',1221037,61000000,405900000000,'Pretoria');

๐Ÿงช Task 4: DROP table

Example:

DROP TABLE Country;

โš ๏ธ Deletes everything permanently.

๐Ÿ” Your Task:

Drop your City table

๐Ÿ” PART 3 โ€” SELECT BASICS

๐Ÿงช Task 5: Basic SELECT

Example:

SELECT name, continent FROM Country;

๐Ÿ” Your Task:

Select:

  • name
  • population

๐Ÿงช Task 6: WHERE

Example:

SELECT name FROM Country WHERE continent = 'Europe';

๐Ÿ” Your Task:

Find all countries in Asia

๐Ÿงช Task 7: AND / OR

Example:

SELECT name FROM Country 
WHERE continent = 'Europe' AND gdp > 3000000000000;

๐Ÿ” Your Task:

Find countries:

  • in Africa OR Asia

๐Ÿงช Task 8: LIKE

Example:

SELECT name FROM Country WHERE name LIKE 'C%';

๐Ÿ” Your Task:

Find countries ending in โ€œaโ€

โœ๏ธ PART 4 โ€” MODIFYING DATA

๐Ÿงช Task 9: UPDATE

Example:

UPDATE Country 
SET population = 85000000
WHERE name = 'Germany';

๐Ÿ” Your Task:

Update population of France

๐Ÿงช Task 10: DELETE

Example:

DELETE FROM Country WHERE name = 'Mexico';

๐Ÿ” Your Task:

Delete one country of your choice

๐Ÿ“Š PART 5 โ€” SORTING & COMPARISONS

๐Ÿงช Task 11: ORDER BY

Example:

SELECT name, gdp 
FROM Country
ORDER BY gdp DESC;

๐Ÿ” Your Task:

Sort countries by population (ascending)

๐Ÿงช Task 12: Comparisons

Example:

SELECT name FROM Country WHERE gdp > 3000000000000;

๐Ÿ” Your Task:

Find countries with population greater than 100 million

๐Ÿ”— PART 6 โ€” JOIN

๐Ÿงช Setup second table

CREATE TABLE Currency(
country VARCHAR(255),
currency VARCHAR(255)
);
INSERT INTO Currency VALUES
('Brazil','Real'),
('Canada','Dollar'),
('Germany','Euro'),
('India','Rupee');

๐Ÿงช Task 13: JOIN

Example:

SELECT Country.name, Currency.currency
FROM Country
JOIN Currency ON Country.name = Currency.country;

๐Ÿ” Your Task:

Display:

  • country name
  • capital
  • currency

๐Ÿ”€ PART 7 โ€” UNION

๐Ÿงช Example:

SELECT name FROM Country WHERE continent = 'Europe'
UNION
SELECT name FROM Country WHERE continent = 'Asia';

๐Ÿ” Your Task:

Union:

  • Africa
  • North America

๐Ÿง  PART 8 โ€” CHALLENGE (MEDIUM)

๐Ÿงช Task 14:

Find countries with GDP above average

SELECT name 
FROM Country
WHERE gdp > (SELECT AVG(gdp) FROM Country);

๐Ÿ” Your Task:

Find countries with population below average

โš ๏ธ PART 9 โ€” TRUNCATE (IMPORTANT)

SQLite does NOT support TRUNCATE, but equivalent is:

DELETE FROM Country;

๐Ÿง  PART 10 โ€” NESTED SELECT (SUBQUERIES)

๐ŸŽฏ What is a Nested SELECT?

A nested SELECT is a query inside another query.

Get Icodewithbenโ€™s stories inย yourย inbox

Join Medium for free to get updates fromย thisย writer.

๐Ÿ‘‰ SQL runs the inner query first, then uses the result in the outer query.

๐Ÿงช Task 15: Compare against an average

โœ… Example:

SELECT name, gdp
FROM Country
WHERE gdp > (
SELECT AVG(gdp)
FROM Country
);

๐Ÿ” What happens?

  1. Inner query runs:
SELECT AVG(gdp) FROM Country;
  1. Outer query uses that value:
WHERE gdp > average

๐Ÿ” Your Task:

Find countries with population above the average population

๐Ÿงช Task 16: Using a nested SELECT for matching data

โœ… Example:

Find countries in the same continent as Germany

SELECT name, continent
FROM Country
WHERE continent = (
SELECT continent
FROM Country
WHERE name = 'Germany'
);

๐Ÿ” Your Task:

Find countries in the same continent as:

  • Brazil

๐Ÿงช Task 17: Nested SELECT with MAX

โœ… Example:

Find the country with the highest GDP

SELECT name, gdp
FROM Country
WHERE gdp = (
SELECT MAX(gdp)
FROM Country
);

๐Ÿ” Your Task:

Find the country with the lowest population

๐Ÿงช Task 18: Nested SELECT with IN

โœ… Example:

Find countries that appear in another table

SELECT name
FROM Country
WHERE name IN (
SELECT country
FROM Currency
);

๐Ÿ” Your Task:

Show countries that:

  • have a currency listed

๐Ÿง  Why do we use nested SELECT?

Use them when:

โœ… You donโ€™t know the value beforehand

Instead of:

WHERE gdp > 3000000000000

You calculate it:

WHERE gdp > (SELECT AVG(gdp) FROM Country)

โœ… You need data from another query

Example:

  • โ€œFind countries in the same continent as Germanyโ€

โœ… Makes queries dynamic

If data changes โ†’ your query still works

๐Ÿ”ฅ FINAL CHALLENGE (NESTED SELECT)

Write a query to:

  • Show country name and population
  • Only include countries with population above the average
  • Sort highest to lowest

๐Ÿง  Extension (Harder)

Write a query to:

  • Show countries with GDP higher than the average GDP of their continent

๐Ÿ‘‰ (This one really tests understanding)

๐Ÿ” Your Task:

Clear the entire table

๐Ÿงฉ FINAL CHALLENGE

Write ONE query to:

  • Show country name and GDP
  • Only for Europe
  • GDP > 2 trillion
  • Sorted highest to lowest

TASKS, attempt to do all these commands

You will need to have this data to make this work, so insert this data too

INSERT INTO Country (name, continent, population) VALUES
('France', 'Europe', 65000000),
('Germany', 'Europe', 83000000),
('Nigeria', 'Africa', 206000000),
('Kenya', 'Africa', 54000000),
('Egypt', 'Africa', 102000000),
('Eswatini', 'Eswatini', 1160000),
('Eritrea', 'East Africa', 3500000),
('China', 'Asia', 1400000000),
('Japan', 'Asia', 126000000),
('India', 'Asia', 1380000000),
('Nepal', 'Asia', 29000000),
('Bhutan', 'Asia', 770000),
('Brazil', 'South America', 212000000),
('Venezuela', 'South America', 28000000),
('New Zealand', 'Oceania', 5000000),
('Belize', 'North America', 420000),
('Canada', 'North America', 38000000),
('Mexico', 'North America', 128000000),
('Estonia', 'Europe', 1300000),
('Switzerland', 'Europe', 8700000);
  • CREATE the table Country first THEN do these queries with the inserted data
  • List all country names and their populations.
  • Show the names of all countries in the continent โ€˜Asiaโ€™.
  • Retrieve the names of countries with a population greater than 40,000,000.
  • Find the names of countries whose continent starts with the letter โ€˜Eโ€™.
  • Show the names and populations of countries with a population less than 1,000,000.
  • List all countries in Africa, ordered by population from largest to smallest.
  • Display the names of countries whose name contains the letter โ€˜zโ€™.
  • Find all countries with populations between 10,000,000 and 50,000,000.
  • List the first 5 countries (by alphabetical order) in the database.
  • Show the countries not in Europe.

PPQ 2018:

Answers PPQ 2018

PPQ 2022-Q2- H446

Press enter or click to view image in full size

Press enter or click to view image in full size

Press enter or click to view image in full size

Press enter or click to view image in full size

Press enter or click to view image in full size

Press enter or click to view image in full size

Press enter or click to view image in full size

Press enter or click to view image in full size

--

--