Data Analyst

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.

50 min

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 DESC puts the heaviest first (ASC is 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 parcels lists each city once.
  • NULL is not a value, it is “unknown” or “nothing here”. A parcel with no driver has driver_id NULL. driver_id = NULL is never true, for anything. Use IS NULL (and IS NOT NULL).
  • AND and OR. AND binds tighter than OR, like multiplication before addition. a AND b OR c means (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 then SELECT * 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 0 is 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…

🔒 The debrief unlocks when you close the case. Finish the lab above.

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.

0 words

🧪 Want to practise this on your own computer? See how to set up your lab →

📝

Knowledge Check

1 of 3

Why does WHERE driver_id = NULL return no rows?

Report a problem

About: /lessons/case-the-late-list