Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Get started
SQL Window Functions for Top Customers
00:00
5 left

SQL Window Functions for Top Customers

MediumSQL · PostgreSQL

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.

Output details

  • Return columns in this order: customer_name, total_revenue, and revenue_rank.
  • Exclude customers with no completed orders.
  • Represent total_revenue as NUMERIC(12,2), retaining exactly two decimal places.
  • Order tied revenues by customer_id ascending.
  • Assign unique sequential revenue_rank values rather than sharing ranks among tied revenues.

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
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the two tables are in the Question tab.

You need to log in / sign up to run or submit.
CodePostgreSQL
Sign up free to run your codeLog inLn 1
Run your query to see results here.