001Level 1

Chapter 7 · CTEs

11. Recursive CTE: Order Value Buckets

Hard40 XPE-Commerce

Use a recursive CTE to generate order value buckets of width 100: 0, 100, 200, ..., 900, plus a final 1000+ bucket. An order belongs to a bucket when total_amount >= bucket_start and total_amount < bucket_end. Return bucket_start, bucket_end (bucket_start + 100, or NULL for the 1000+ bucket), order_count, and total_revenue (rounded to 2 decimals, 0 for empty buckets). Sort by bucket_start.

Tables

  • PKidINT
  • FKcustomer_id→ customers.idINT
  • order_dateTEXT
  • statusTEXT
  • total_amountREAL

Need help?

Ask Sol

AI tutor — looks at your current query and nudges you, without spoiling the answer.

Solution

Try the hints first — solving it yourself is what makes it stick.

Peek at the solution?

You can still earn the XP afterwards by submitting a working query — but you'll learn more if you give it one more try.