Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL Window Function Ranking

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

Your question is SQL Window Function Ranking. 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

Chargebee QA needs to validate invoice-ranking results in a billing report. Write a PostgreSQL query that ranks each active customer's paid invoices issued in June 2026 by invoice amount.

Requirements

  1. Join customers to invoices and include only active customers.
  2. Include invoices with a non-null paid_at value and an invoice_date from 2026-06-01 through 2026-06-30.
  3. Return customer name, invoice number, invoice date, amount, and rank within that customer, highest amount first.
  4. Use RANK() so invoices with equal amounts receive the same rank. Sort final output by customer name, rank, then invoice number.

Schema

customers
ColumnTypeDescription
customer_idPKINTEGERUnique Chargebee customer identifier.
customer_nameVARCHAR(100)Display name of the customer.
is_activeBOOLEANWhether the customer account is active.
invoices
ColumnTypeDescription
invoice_idPKINTEGERUnique invoice identifier.
customer_idINTEGERCustomer associated with the invoice.
invoice_numberVARCHAR(50)Customer-facing Chargebee invoice number.
invoice_dateDATEDate the invoice was issued.
amountNUMERIC(12,2)Invoice amount in the account currency.
paid_atTIMESTAMPTimestamp when the invoice was paid.
Tablescustomersinvoices
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results