Case File: The Late List
The dispatch office has a database of every parcel and five questions that people ask every morning. In a real SQLite database in this page, answer them as views: the late parcels of one city, the five heaviest parcels, the parcels with no driver, the failed or late parcels of a city (where AND and OR must be bracketed), and the list of cities served. The database is generated for you alone.
Sabi Logistics · Query 1
The Late List
Customers phone every morning to ask about late parcels, and the answer lives in a spreadsheet that nobody trusts
Emeka Support lead · Phone call
Emeka. Every morning my team gets the same five questions, and every morning somebody exports the parcel database into a spreadsheet and filters it by hand. Which parcels are late in our busiest city? Which are the heaviest, because those need the big van? Which have no driver assigned yet, which is the one that really gets us shouted at? Which are in trouble, failed or late, in that same city? And which cities do we even serve, for the new price sheet?
I would like the answers to be views in the database, so that anybody can ask them, and they are always up to date. Last week one of my team wrote the trouble list and it contained parcels from every city. I could not see why. Can you answer all five, and tell me what went wrong in that one?
The brief
What you need to know first.
- A database table is a grid: each row is one thing (a parcel) and each column is a fact about it (its city, its weight). SQL is the language for asking questions of tables. The database in this page is SQLite, a real database engine that runs in your browser. Nothing you do can harm anything.
- SELECT chooses columns and FROM chooses the table:
SELECT id, city FROM parcels;. WHERE keeps only the rows that satisfy a condition:WHERE status = 'late' AND city = 'Lagos'. Text values go in single quotes. - ORDER BY sorts:
ORDER BY weight_kg DESCputs the heaviest first (ASCis the default). Add a second key to settle ties, so that the answer is the same every time:ORDER BY weight_kg DESC, id. LIMIT 5 keeps the first five rows of the result. - DISTINCT removes repeated rows:
SELECT DISTINCT city FROM parcelslists each city once. - NULL is not a value, it is “unknown” or “nothing here”. A parcel with no driver has
driver_idNULL.driver_id = NULLis never true, for anything. UseIS NULL(andIS NOT NULL). - AND and OR. AND binds tighter than OR, like multiplication before addition.
a AND b OR cmeans(a AND b) OR c. When you want “in this city, and (failed or late)”, put brackets round the OR. This is the classic mistake behind a list with parcels from every city. - Views.
CREATE VIEW name AS SELECT ...;saves a query under a name. Anybody can thenSELECT * FROM name. A view stores no data: it runs the query each time, so it is never out of date. The checks read your views, so the view name and the column order must match the tasks. - The starter creates each view with an empty answer (
WHERE 0is never true). Replace the SELECT of each one, press Run to see the last result, and Check my work to test every view.
The lab gives you a company’s parcels, generated for you alone.
Preparing your database…
Emeka: “That is the five answers, and I can see the trouble list was missing its brackets. Now my team wants to add the customer’s name to the list, but the customer’s name is in a different table. And one of them said that views are slow. Are they?”
🔒 The debrief unlocks when you close the case. Finish the lab above.
The debrief
Emeka: “So the name needs a join, which is the next thing to learn, and a view is only as slow as the query inside it.”
What a senior analyst would do next. Check every query you write by looking at a few rows of the result and counting them against a number you know. Keep the queries in views (or saved files) with a comment saying what they answer. Learn the habit of writing the brackets whenever you mix AND and OR, even where they are not strictly needed, because the next reader will not remember the precedence rules. Be careful with NULL in every comparison and every count: COUNT(driver_id) ignores NULLs, COUNT(*) does not. And never trust a result you have not tried to break: ask whether a row that should be there is missing, and whether a row that should not be there has crept in.
The trap. A WHERE with AND and OR and no brackets. The query runs, returns rows, looks plausible, and answers a different question.
Write the note for the support team
Write the note for the support team: what each of the five views answers and how it is written (the filter, the sort, the limit, the distinct), why the trouble list returned parcels from every city when the brackets were missing, why you test for no driver with IS NULL and not with an equals sign, what a view is and why it never goes out of date, and how you checked each answer.
🧪 Want to practise this on your own computer? See how to set up your lab →