Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
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
Optimize Slow Analytical SQL
00:00
5 left

Optimize Slow Analytical SQL

HardSQL · PostgreSQL

Problem

Optimize a slow-running SQL query involving multi-table joins, aggregations, and subqueries on a distributed analytical data warehouse at Apple. Use the provided tables to identify device models with at least two active customers during January 2025 and calculate their net completed revenue after refunds.

Output

  1. One row per eligible country and device model.
  2. Columns: country, device_model, active_customers, net_revenue, and revenue_rank.
  3. Include only the top two models per country, ordered by country, revenue_rank, and device_model.

Schema

customers
ColumnTypeDescription
customer_idPKINTCustomer identifier
countryVARCHAR(50)Customer country
devices
ColumnTypeDescription
device_idPKINTDevice identifier
customer_idINTOwning customer
device_modelVARCHAR(80)Apple device model
sessions
ColumnTypeDescription
session_idPKINTSession identifier
customer_idINTCustomer starting the session
device_idINTDevice used for the session
session_dateDATESession date
transactions
ColumnTypeDescription
transaction_idPKINTTransaction identifier
customer_idINTPurchasing customer
device_idINTDevice associated with the transaction
transaction_dateDATETransaction date
amountNUMERIC(12,2)Gross transaction amount
statusVARCHAR(20)Transaction status
refunds
ColumnTypeDescription
refund_idPKINTRefund identifier
transaction_idINTRefunded transaction
refund_amountNUMERIC(12,2)Refund amount
Tablescustomersdevicessessionstransactionsrefunds
Interviewer

Your question is Optimize Slow Analytical SQL. Start with the requirements and the five 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.