Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL Window Functions for Top Customers

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

Your question is SQL Window Functions for Top Customers. 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

Genesys Cloud Analytics needs a ranked view of customers based on completed subscription revenue. Write a PostgreSQL query that aggregates revenue per customer and uses a window function to return the top 10 customers.

Requirements

  1. Join customers to orders using customer_id.
  2. Include only orders with status = 'completed'.
  3. Calculate total revenue per customer, treating a customer with only NULL revenue values as zero.
  4. Use a window function to assign the revenue rank, return the top 10 ranks, and order the final result from highest to lowest revenue. Break ties by customer_id.

Schema

customers
ColumnTypeDescription
customer_idPKINTUnique customer identifier
customer_nameVARCHAR(120)Customer account name
segmentVARCHAR(40)Customer segment
orders
ColumnTypeDescription
order_idPKINTUnique order identifier
customer_idINTReferences customers.customer_id
revenueNUMERIC(12,2)Revenue recognized for the order
statusVARCHAR(20)Order processing status
order_dateDATEDate the order was placed
Tablescustomersorders
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results