Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Top Events Per City SQL
00:00
5 left

Top Events Per City SQL

MediumSQL · PostgreSQL

Problem

A ticket marketplace wants to identify the most searched events in each city. Write a SQL query to find the top three events per city based on ticket searches recorded during the thirty-day period ending on September 15, 2026.

Output

  1. Return one row per qualifying event with the columns city, event_id, event_name, search_count, and city_rank.
  2. Include only searches from August 16, 2026 through September 15, 2026, inclusive. Ignore searches whose event cannot be identified.
  3. Rank events independently within each city by descending search count. Break ties by ascending event_id, and return only the first three events per city.
  4. Sort the final results by city, then city_rank, then event_id.

Schema

events
ColumnTypeDescription
event_idPKINTUnique event identifier
event_nameVARCHAR(150)Name of the event
cityVARCHAR(80)City where the event takes place
ticket_searches
ColumnTypeDescription
search_idPKINTUnique ticket search identifier
event_idINTEvent searched for, when available
searched_atTIMESTAMPTimestamp when the search occurred
Tableseventsticket_searches
Interviewer

Your question is Top Events Per City SQL. Start with the requirements and the two 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.