Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Pivot Daily to Weekly SQL

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

Your question is Pivot Daily to Weekly 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

Airbnb's Search analytics team wants a weekly view of search activity for active listings. Write a PostgreSQL query that pivots daily listing search views into weekday columns for the weeks beginning March 3 and March 10, 2025.

Requirements

  1. Include every active listing for both weeks, including listings with no recorded metrics.
  2. Return one row per listing and week, with separate search-view columns for Monday through Sunday.
  3. Use conditional aggregation and return missing daily values as zero.
  4. Order results by listing_id and week_start.

Schema

listings
ColumnTypeDescription
listing_idPKINTUnique Airbnb listing identifier
listing_nameVARCHAR(120)Listing display name
neighborhoodVARCHAR(80)Listing neighborhood
is_activeBOOLEANWhether the listing is currently active
listing_daily_metrics
ColumnTypeDescription
metric_idPKINTUnique daily metric identifier
listing_idINTReferenced Airbnb listing
metric_dateDATEDate of the Search metric
search_viewsINTListing views generated from Airbnb Search
Tableslistingslisting_daily_metrics
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results