Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL Top N Customers by Revenue

EasySQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is SQL Top N Customers by Revenue. Start with the requirements and the two tables on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Sense wants to identify its highest-value customers using completed orders recorded in its platform. Write a PostgreSQL query to return the top 3 customers by revenue during Q1 2025.

Requirements

  1. Join customers to orders using customer_id.
  2. Include only orders with status completed and an order_date from 2025-01-01 through 2025-03-31.
  3. Calculate total revenue per customer, return the top 3 customers, and sort by revenue descending. Use customer_name as a deterministic tie-breaker.

Schema

customers
ColumnTypeDescription
customer_idPKINTEGERUnique customer identifier
customer_nameVARCHAR(100)Customer display name
account_statusVARCHAR(20)Current customer account status
orders
ColumnTypeDescription
order_idPKINTEGERUnique order identifier
customer_idINTEGERCustomer associated with the order
order_dateDATEDate the order was created
statusVARCHAR(20)Order processing status
order_totalNUMERIC(12,2)Revenue attributed to the order
Tablescustomersorders
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results