Skip to content
Codewilds

Lesson 7 of 30 · Querying Basics

Boss: Build a SQL Report with WHERE, ORDER BY and LIMIT

Boss battlehard100 XP1 min readFree lesson

The Records Keeper blocks the way. Every test you pass deals damage — pass them all to defeat it and claim a loot chest.

Deep in the Archive Vaults sits the Records Keeper, a dusty spirit of ink and old ledgers that guards the registry of every hero in the realm. It lets no one pass until they hand over a report with exactly the right rows, the right columns and the right order — and today it wants a scouting report for the Silver Arrows.

Battle plan

This fight uses everything from Chapter 1 in a single query: choosing and renaming columns, a calculated column, WHERE with AND, OR and parentheses, IN, BETWEEN, IS NULL, sorting by two columns and LIMIT.

Here is the heroes table again:

id name class level gold guild_id joined
5 Elara Brightwell Healer 12 260 1 2025-02-14
9 Iris Dawnpetal Healer 7 95 NULL 2025-05-22
11 Kira Frostvale Mage 3 20 NULL 2025-07-15
15 Oona Fernwhistle Healer 20 400 2 2024-06-18
… … … … … … …

Two things that make this easier:

  • ORDER BY can use an alias you defined in SELECT, so you can sort by renown instead of repeating the calculation.
  • Clauses always come in the order SELECT … FROM … WHERE … ORDER BY … LIMIT.

Assignment

The Silver Arrows (guild 1) want to recruit Mages, Healers and Rogues. Write one query on heroes for the Keeper's scouting report.

Columns, in this order:

  1. name, renamed to hero
  2. class
  3. level
  4. renown — a hero's fame, calculated as level * 100 + gold

Rows — only heroes who:

  • are not Silver Arrows members. Heroes with no guild count as non-members, so they belong on the report,
  • are a Mage, Healer or Rogue,
  • have a level between 7 and 20, both included.

Order and size:

  • sorted by class from A to Z, and within the same class by renown, highest first,
  • only the first 5 rows.

For example, Oona Fernwhistle (Healer, level 20, 400 gold) has a renown of 20 × 100 + 400 = 2400.

Each passing test lands a hit on the Records Keeper. Make all eight pass to win the ledger.

Keep learning

Need a quick reminder? The SQL cheat sheet has every topic of Learn SQL on one page.

Lesson last updated on .

Write code ↓

The Records Keeper

HP ???

SQL+100 XP

Press Run to execute your code, or Submit to check it against the tests.