Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Window Function Revenue Ranking
00:00
5 left

Window Function Revenue Ranking

MediumSQL · PostgreSQL

Problem

Write a query to find the second-highest revenue-generating property in each market using a window function.

Use the markets, properties, and bookings tables. Include confirmed and completed bookings, exclude cancelled bookings, and treat tied second-highest revenues as valid results.

Output

  1. One row per qualifying property
  2. Columns: market_name, property_id, property_name, and total_revenue
  3. Sort by market_name, then property_id

Schema

markets
ColumnTypeDescription
market_idPKINTUnique market identifier
market_nameVARCHAR(100)Market name
properties
ColumnTypeDescription
property_idPKINTUnique property identifier
market_idINTMarket containing the property
property_nameVARCHAR(150)Property display name
bookings
ColumnTypeDescription
booking_idPKINTUnique booking identifier
property_idINTBooked property
statusVARCHAR(30)Booking status
revenueDECIMAL(12,2)Revenue attributed to the booking
Tablesmarketspropertiesbookings
Interviewer

Your question is Window Function Revenue Ranking. Start with the requirements and the three tables in the Question tab.

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.
CodePostgreSQL
You need to log in / sign up to run or submit.Ln 1
Run your query to see results here.