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

  1. Show every row in friends.
  2. Show only names, not ids or cities.
  3. Show friends who live in Lisbon.
  4. Sort friends by name, A to Z.
  5. Count how many friends you have with COUNT(*).
  6. Show gift ideas that are not bought yet.
  7. Rest day: reread your week's queries and add a comment to each with --.

Week 2: changing data

  1. Add a new friend with INSERT.
  2. Mark the scarf as bought with UPDATE. Remember the WHERE clause.
  3. Add a gift idea for your new friend.
  4. Delete one gift idea you no longer like.
  5. 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;
  1. Count bought and unbought gifts with GROUP BY bought.
  2. Rest day: rebuild the database from your file and run week two again.

Week 3: linking tables

  1. Show each gift with its friend's name using INNER JOIN.
  2. Show every friend, even those without gifts, using LEFT JOIN.
  3. Find friends with no gift ideas: LEFT JOIN plus WHERE gifts.id IS NULL.
  4. Count gift ideas per friend.
  5. Show friends with more than one gift idea using HAVING.
  6. 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.
  7. Rest day.

Week 4: from code

  1. Connect to december.db from a short Python script and print all friends.
  2. Ask the user for a city and show friends there, using a ? placeholder, never string pasting.
  3. Insert a new gift idea from the script and commit it.
  4. Take the day off.

If you get stuck

DayHelpful page
1-6Filtering and sorting with WHERE and ORDER BY
15-19INNER and LEFT JOIN step by step
22-24Connecting 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.