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
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

Output details

  • Consider only bookings with status exactly confirmed or completed; exclude every other status.
  • Include properties with no qualifying bookings and report their total_revenue as numeric 0.
  • Return total_revenue as the unrounded sum of revenues, preserving full numeric precision.
  • Sort market_name ascending, then property_id ascending.

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
Sign up free to run your codeLog inLn 1
Run your query to see results here.