InterviewPrepKit

Home / SQL / Query Basics

Top Orders by Amount

easy
Solving tips
  • Combine ORDER BY with LIMIT to return a top-N slice of the rows.
  • Add a deterministic tie-breaker column to ORDER BY so ties resolve predictably.
  • PostgreSQL uses LIMIT; SQL Server uses TOP and Oracle uses FETCH FIRST for the same idea.

You are given an orders table and need the highest-value orders for a dashboard’s leaderboard.

Schema

CREATE TABLE orders (
  id          INTEGER PRIMARY KEY,
  customer_id INTEGER,
  order_date  DATE,
  amount      NUMERIC(10,2)
);

Sample data:

idcustomer_idorder_dateamount
10112023-01-05250.00
10222023-01-06500.00
10312023-01-10500.00
10432023-02-01120.00
10522023-02-15800.00
10642023-03-01300.00
10732023-03-05300.00

Task

Return the id and amount of the three orders with the largest amount. Order by amount in descending order, and when two orders have the same amount, break the tie by id in ascending order.

Expected output

idamount
105800.00
102500.00
103500.00
Your workspace Not runnable by design — this is your interview scratchpad. Saved on this device.