Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Trending in AI
Hot
Agentic AI EngineerForward-Deployed EngineerGenAI EngineerAI Architect
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
Start free
Optimize Complex Warehouse SQL
00:00
5 left

Optimize Complex Warehouse SQL

HardSQL · PostgreSQL

Problem

Optimize a slow-running SQL query containing multiple subqueries and complex joins on a distributed data warehouse like Snowflake or Redshift.

Using the supplied PostgreSQL schema, write an optimized query that calculates the requested monthly viewing metrics without changing the result. Restrict analysis to the latest and immediately preceding calendar month represented in the viewing data.

Output

  1. One row per plan and genre in the top two genres by current-month minutes.
  2. Columns: plan_name, genre, current_minutes, prior_minutes, percent_change, and genre_rank.
  3. Include only play events covered by an active subscription, order by plan_name, genre_rank, and genre.

Schema

viewers
ColumnTypeDescription
viewer_idPKINTUnique viewer identifier
display_nameVARCHAR(100)Viewer display name
titles
ColumnTypeDescription
title_idPKINTUnique title identifier
title_nameVARCHAR(150)Content title
genreVARCHAR(50)Primary content genre
subscriptions
ColumnTypeDescription
subscription_idPKINTUnique subscription identifier
viewer_idINTSubscribed viewer
plan_nameVARCHAR(40)Subscription plan
started_atTIMESTAMPSubscription start timestamp
ended_atTIMESTAMPSubscription end timestamp
viewing_events
ColumnTypeDescription
event_idPKINTUnique viewing event identifier
viewer_idINTViewer generating the event
title_idINTViewed title
viewed_atTIMESTAMPEvent timestamp
watch_secondsINTNumber of seconds watched
event_typeVARCHAR(20)Viewing event type
Tablesviewerstitlessubscriptionsviewing_events
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the four tables are in the Question tab.

Sign up free to run or submit. Log in
CodePostgreSQL
Sign up free to run your code. Your draft is saved.Run with a free accountLog inLn 1
Run your query to see results here.