๐ป SQL PRACTICE WORKSHEET (SQLite + Python)
๐ฏ Objective
Learn how to use:
SELECT,FROM,WHEREAND,OR,LIKEINSERT,DELETE,UPDATEORDER BYUNIONJOINDROPTRUNCATE (not supported in SQLITE) would be DELETE
๐ ๏ธ PART 1 โ SETTING UP (VS CODE + PYTHON)
Step 1: Create your project
- Open VS Code
- Create a new folder (e.g.
sql-practice) - Inside it, create:
main.pypractice.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?
- Inner query runs:
SELECT AVG(gdp) FROM Country;- 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 > 3000000000000You 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







