▶ SQL Playground
Q9 MEDIUM · Window Functions

Top N customers by revenue

Find the top 5 customers by total revenue along with their rank. Practice the multi-step approach with CTEs.

Try this problem in the playground

The sample tables will be auto-loaded — write a query and run it instantly.

▶ Open Playground

Setup & sample data

The playground will create these tables and populate them automatically when you click "Open Playground" above:

Solutions

Multiple approaches — try each and understand the trade-offs.

Expected output