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
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess 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
Build my plan
Optimize SQL Query Performance
00:00
5 left

Optimize SQL Query Performance

MediumSQL · PostgreSQL

Problem

How would you optimize a SQL query for performance?

Using the supplied PostgreSQL schema, write an optimized query that calculates billed totals for qualifying active accounts. The answer should be suitable for explaining how you would validate query performance and reduce unnecessary work.

Output

  1. One row per active account with completed invoices in the requested date range.
  2. Columns: region, account_id, account_name, total_billed, and account_rank.
  3. Sort by region ascending with NULL regions last, then account_rank ascending and account_id ascending.

Schema

accounts
ColumnTypeDescription
account_idPKINTUnique account identifier
account_nameVARCHAR(100)Account display name
regionVARCHAR(30)Account operating region
statusVARCHAR(20)Account lifecycle status
invoices
ColumnTypeDescription
invoice_idPKINTUnique invoice identifier
account_idINTReferenced account identifier
invoice_dateDATEInvoice issue date
statusVARCHAR(20)Invoice processing status
invoice_lines
ColumnTypeDescription
line_idPKINTUnique invoice line identifier
invoice_idINTReferenced invoice identifier
product_nameVARCHAR(100)Billed product or service
quantityINTQuantity billed
unit_priceNUMERIC(10,2)Price per billed unit
Tablesaccountsinvoicesinvoice_lines
Interviewer

Your question is Optimize SQL Query Performance. Start with the requirements and the three 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.