SQL practice: Top N Per Group

Chapter 26 · 12 questions · included in full access

  1. Question 1 · easy

    Find the single highest-paid employee in each branch.

    Solve it in your browser
  2. Question 2 · easy

    Find each customer's most recent order.

    Solve it in your browser
  3. Question 3 · medium

    Find the top 2 most expensive products in each category.

    Solve it in your browser
  4. Question 4 · medium

    Split customers into 4 buckets by total spend and show bucket 1.

    Solve it in your browser
  5. Question 5 · medium

    Find the best-selling product in each branch, by total quantity.

    Solve it in your browser
  6. Question 6 · medium

    Find each branch's single highest-value order.

    Solve it in your browser
  7. Question 7 · medium

    Find the 2nd most expensive product in each category.

    Solve it in your browser
  8. Question 8 · medium

    Find each customer's first ever order.

    Solve it in your browser
  9. Question 9 · medium

    Use the duplicate-finding trick on order_items, partitioning by order_id and product_id. Are there any duplicates?

    Solve it in your browser
  10. Question 10 · medium

    Find the top 3 employees by revenue handled. Does this one need the pattern at all?

    Solve it in your browser
  11. Question 11 · hard

    Find the highest-paid employee in each role.

    Solve it in your browser
  12. Question 12 · hard

    Run Step 2 of the chapter's example but remove PARTITION BY p.category. What does rn = 1 give you now, and why?

    Solve it in your browser

These questions are part of full access.

The first chapters of every track are free. Full access is a one-time payment.

See pricing