Founding rate: RM5 off Basic, forever — applied automatically. See pricing

Menu

Chapter 02 · 7 min · Basic

What is the question, really?

The request that reaches you almost never contains a number. It contains a feeling somebody wants confirmed or denied: is the promo working, are we growing, is the new courier better.

Your first job is not to write SQL. It is to turn that sentence into something with edges — a thing being measured, the group it is measured over, and the stretch of time it covers. Do that badly and every query afterwards is precise, fast, and answering a question nobody asked.

This chapter takes one real request, writes two completely defensible definitions of it, and runs both. They point in opposite directions. Neither is a mistake.

The dataset

Fourteen orders. promo_code is RAYA10 when the customer used the promo and null when they did not. The promo ran from 5 to 9 March — two orders carry the code outside that window, because support honoured it late for two complaining customers. That detail is not a trick; it is the kind of thing you find out by asking, and it changes the answer.

Schema

orders
order_idint
amount_myrnumeric(7,2)
statustext
promo_codetext
ordered_ondate

Example data

orders

The request

It arrives as a message, usually at 5pm: "is the RAYA10 promo working?"

Four words of subject, and not one of them is measurable. Before you touch the keyboard, force the request through four boxes:

  • What is being measured? Orders? Revenue? Average order value? New customers?
  • Out of what? Every number that means anything is a numerator over a denominator.
  • Over which rows? All orders, or only the ones that actually completed?
  • Over what period? The promo ran 5 to 9 March. Does the window include the late honours?

Most arguments about numbers are not arguments about SQL. They are two people who filled those four boxes differently and never said so out loud.

Basic

The rest of this chapter is Basic

Learning SQL is free here, forever. This track is the paid half: what to actually do with a dataset once somebody hands you one, from the first look to a number you can defend.

See Basic plans

RM 25/mo · cancel anytime

Still to come in this chapter

  1. 02The easy number that answers nothing
  2. 03Reading one: every order in the table
  3. 04Reading two: the orders that actually happened, while the promo ran
  4. 05Ship the definition with the number
  5. 06What to send back at 5pm

Now practise it

Questions in the bank that drill this chapter's decision: