Code24Lab

You are not signed in — the lab works, but your progress is not saved. Sign up

Lesson 1 of 21 is free. Unlock all 21 hands-on labs, the final exam and your certificate.

Buy course — €29.90
Read first

The SQL

SELECT order_id, region, rep, amount,
  ROW_NUMBER() OVER (PARTITION BY region
                     ORDER BY amount DESC, order_id) AS rn,
  RANK()       OVER (PARTITION BY region
                     ORDER BY amount DESC) AS rnk,
  DENSE_RANK() OVER (PARTITION BY region
                     ORDER BY amount DESC) AS drnk
FROM dbo.Orders;
Production rule: ROW_NUMBER() always needs a unique ORDER BY. Otherwise "the top row per group" can change between two runs of the same query.
Loading the lab…