Please line them up, and just bring me three.
Today you learn two new words. One puts your answer in order. The other says how many rows you want. With them you can ask for things like “the 3 cheapest drinks”.
In what order? How many?
Lesson 1 gave you three questions. Today adds two more, each with its own colour.
Name the column to sort by, like ORDER BY price. Without it, the order of the rows is not guaranteed.
Give a number, like LIMIT 3. You get the first 3 rows and the rest are left behind.
ASC walks up the stairs, DESC walks down
After the column name you can add one small word to choose the direction. Think of a staircase.
ORDER BY price is the same as ORDER BY price ASC. So you only need to type DESC when you want biggest first.Some Friendly Waiters Offer Lemonade
Now there are five words, and they always go in the same order. The first letter of each word in this sentence gives you the order.
You can skip WHERE, ORDER BY or LIMIT when you don't need them. But the ones you use always stay in this order.
Every piece has a meaning
Please press Next (or click any piece) and watch the sentence grow underneath. The new pieces from today are in teal and green.
Say each one out loud
Swap each word for what you say, and the code becomes a normal sentence again.
| Write | Say it as | Example | Why it's there |
|---|---|---|---|
| Putting rows in order | |||
| ORDER BY | “sorted by…” | ORDER BY price | Two words that always go together. Says which column decides the order. |
| ASC | “smallest first” | ORDER BY price ASC | Up the stairs: small to big, A to Z. This is what you get if you write nothing. |
| DESC | “biggest first” | ORDER BY price DESC | Down the stairs: big to small, Z to A. |
| , | “then by…” | ORDER BY type, price | Sort by type first. When two rows have the same type, sort those by price. |
| Choosing how many | |||
| LIMIT | “only the first…” | LIMIT 3 | Always the very last word. Keeps that many rows from the top. |
| 3 | “three” | LIMIT 3 | A whole number with no quotes, because it is a number, not text. |
SELECT item, price shows item and price. After ORDER BY it means “then by”: ORDER BY type, price sorts by type, then by price when the types are the same.Playground: watch the rows line up
Please type a query and press Run. Watch the rows slide into order, then see LIMIT keep only the first few.
- 1
- 2
- 3
- 4
- 5
The database works in this order: FROM, WHERE, SELECT, ORDER BY, LIMIT. Faded columns stay until the end, so you can still sort by them.
Five quick questions
Answering from memory is the fastest way to make something stick. Wrong answers explain themselves, so please don't worry.
Your pocket card
Please take a screenshot of this card. Read it once tomorrow and once next week.
SELECTWhat do you want to see?FROMWhere is it?WHEREWhich ones?ORDER BYIn what order?LIMITHow many?- Order to write them: Some Friendly Waiters Offer Lemonade.
ASCwalks up (small to big, A to Z). It is the default.DESCwalks down (big to small, Z to A).- Sort first, then cut: ORDER BY before LIMIT.
- In ORDER BY, the comma means “then by”.
SQL Café is a free course by MASSH AI. Want SQL or data training for your team? Please get in touch.