Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top 3 Per Category SQL

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

Your question is Top 3 Per Category SQL. 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

Cotality Property Insights needs a ranked view of active properties for market analysis. Write a PostgreSQL query that returns the top three properties by estimated market value within each active property category.

Requirements

  1. Join property_listings to property_categories using category_id.
  2. Include only active listings with a non-null estimated market value and active categories.
  3. Rank properties independently within each category by estimated market value descending. Break ties with listing_id ascending.
  4. Return only ranks 1 through 3, ordered by category name and rank.

Schema

property_categories
ColumnTypeDescription
category_idPKINTUnique property category identifier
category_nameVARCHAR(100)Display name of the property category
is_activeBOOLEANWhether the category is active in Cotality Property Insights
property_listings
ColumnTypeDescription
listing_idPKINTUnique property listing identifier
category_idINTReferenced property category identifier
addressVARCHAR(200)Property street address
estimated_market_valueNUMERIC(12,2)Estimated market value of the property
valuation_dateDATEDate on which the estimate was produced
listing_statusVARCHAR(30)Current listing status
Tablesproperty_categoriesproperty_listings
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results