download

Top two order amounts per customer, keeping ties

SQL~15 minReviewed 28 Oct 2026Free · No sign-in

Question

You have an orders table. For each customer, return every order whose amount is one of that customer's two highest distinct amounts. If two orders share an amount, return both. Sort by customer, then amount descending, then order id.

order_idcustomer_idamount
1011120.00
102180.00
1031120.00
104150.00
2012300.00
202290.00
301340.00
Show hints (2)
  1. "WHICH FUNCTION"

    You need to rank orders inside each customer. Which ranking function gives tied amounts the same rank and leaves no gap after them?

  2. "WHERE TO FILTER"

    Window functions are evaluated after WHERE, so you can't filter on the rank in the same query level. Wrap it in a CTE or a subquery.

Show checked explanation

Rank each customer's orders with DENSE_RANK(), then keep ranks 1 and 2 in an outer query.

"SOLUTION · SQL"
WITH ranked AS (
  SELECT order_id, customer_id, amount,
         DENSE_RANK() OVER (
           PARTITION BY customer_id
           ORDER BY amount DESC
         ) AS rnk
  FROM orders
)
SELECT order_id, customer_id, amount, rnk
FROM ranked
WHERE rnk <= 2
ORDER BY customer_id, amount DESC, order_id;
"OUTPUT"
order_id | customer_id | amount | rnk
101      | 1           | 120.00 | 1
103      | 1           | 120.00 | 1
102      | 1           | 80.00  | 2
201      | 2           | 300.00 | 1
202      | 2           | 90.00  | 2
301      | 3           | 40.00  | 1

Query run on PostgreSQL 16 against the data above. Checked by an editor other than the author.

Walkthrough

The three ranking functions only differ on ties. Here is customer 1, ordered by amount descending:

orderamountrow_numberrankdense_rank
101120.00111
103120.00211
10280.00332
10450.00443

ROW_NUMBER() <= 2 returns 101 and 103 and drops the 80.00 order. It counts rows, not amounts, and which of two tied rows gets 1 is arbitrary unless you add a tiebreaker.

RANK() <= 2 also drops 80.00: after a two-way tie the next rank is 3.

DENSE_RANK() <= 2 returns all three rows: two orders at the top amount and one at the second. That is what the task asks for.

Common mistakes

  • Writing WHERE DENSE_RANK() OVER (…) <= 2. PostgreSQL rejects window functions in WHERE; filter one level up.
  • Using ORDER BY amount DESC LIMIT 2. LIMIT applies to the whole result, not per customer.
  • Forgetting NULL. With DESC, PostgreSQL sorts nulls first, so a null amount would take rank 1. Add NULLS LAST or exclude nulls if that isn't intended.

Follow-up questions

  • "Now return exactly two rows per customer." Use ROW_NUMBER() with a deterministic tiebreaker: ORDER BY amount DESC, order_id.
  • "The table has hundreds of millions of rows." An index on (customer_id, amount DESC) lets a LATERAL top-N per customer read only a few index entries each.

What a strong answer covers

States the tie rule before writing code, names why DENSE_RANK fits, filters in an outer query, and mentions nulls without being asked.

Open in interactive practice deck →

Original OfferVibe question. Dataset and query written by the editors.
© 2026 OfferVibe · Text CC BY-NC-SA 4.0 · code MITFree to share and adapt with credit, not for commercial use. Code samples are free for any use.