A December SQL Practice Calendar, One Small Query a Day
December is busy, but many learners like a small daily challenge in the run-up to the new year. This calendar gives one short SQL task per day, each taking a few minutes. Skip days freely; it is meant to be relaxing.
Set up once
Create a tiny dataset in SQLite. Save this as december.sql and run sqlite3 december.db < december.sql:
CREATE TABLE friends (id INTEGER PRIMARY KEY, name TEXT NOT NULL, city TEXT);
CREATE TABLE gifts (
id INTEGER PRIMARY KEY,
friend_id INTEGER REFERENCES friends(id),
idea TEXT NOT NULL,
bought INTEGER DEFAULT 0
);
INSERT INTO friends (name, city) VALUES ('Ana','Lisbon'),('Ben','Leeds'),('Chi','Lagos'),('Dev','Lisbon');
INSERT INTO gifts (friend_id, idea) VALUES (1,'Book'),(1,'Scarf'),(2,'Board game'),(3,'Tea set');
The names are made up. Feel free to change them.
Week 1: reading data
- Show every row in
friends. - Show only names, not ids or cities.
- Show friends who live in Lisbon.
- Sort friends by name, A to Z.
- Count how many friends you have with
COUNT(*). - Show gift ideas that are not bought yet.
- Rest day: reread your week's queries and add a comment to each with
--.
Week 2: changing data
- Add a new friend with INSERT.
- Mark the scarf as bought with UPDATE. Remember the WHERE clause.
- Add a gift idea for your new friend.
- Delete one gift idea you no longer like.
- Try an UPDATE without WHERE inside a transaction, look at the result, then undo it:
BEGIN;
UPDATE gifts SET bought = 1;
SELECT * FROM gifts;
ROLLBACK;
- Count bought and unbought gifts with
GROUP BY bought. - Rest day: rebuild the database from your file and run week two again.
Week 3: linking tables
- Show each gift with its friend's name using INNER JOIN.
- Show every friend, even those without gifts, using LEFT JOIN.
- Find friends with no gift ideas: LEFT JOIN plus
WHERE gifts.id IS NULL. - Count gift ideas per friend.
- Show friends with more than one gift idea using HAVING.
- Try inserting a gift for friend id 99. Did SQLite stop you? Foreign keys are only enforced after
PRAGMA foreign_keys = ON;. Turn it on and try again. - Rest day.
Week 4: from code
- Connect to
december.dbfrom a short Python script and print all friends. - Ask the user for a city and show friends there, using a
?placeholder, never string pasting. - Insert a new gift idea from the script and commit it.
- Take the day off.
If you get stuck
| Day | Helpful page |
|---|---|
| 1-6 | Filtering and sorting with WHERE and ORDER BY |
| 15-19 | INNER and LEFT JOIN step by step |
| 22-24 | Connecting a Python script to SQLite |
Keep your answers
Save each day's query in a file named by date. By the end of the month you will have a small, personal reference sheet written in your own words, which is worth more than any cheat sheet you could download.
A free learning page for beginners. Practise on a test database, never on real customer data, and check the official documentation for your database version.