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
SQL: Top Companies by Revenue
00:00
5 left

SQL: Top Companies by Revenue

MediumSQL · PostgreSQL

Problem

Lumenalta needs a revenue leaderboard for fiscal year 2025. Write a PostgreSQL query that calculates each company's net settled revenue after refunds, ranks companies by revenue, and includes every company tied within the top three rank positions.

Requirements

  1. Include only transactions with status = 'SETTLED' dated from 2025-01-01 through 2025-12-31.
  2. Calculate net revenue as transaction gross amount minus all associated refunds. Treat missing refund totals as zero.
  3. Include companies with no qualifying transactions, assigning them revenue of zero.
  4. Use a tie-aware ranking method so ties are not arbitrarily truncated. Return all companies whose rank is at most 3.
  5. Return company name, net revenue, and rank, ordered by rank ascending and company name ascending.

Representative Sample Data

companies: Aster Systems, BluePeak Labs, Cedar Health, Delta Freight, Ember Retail, Fathom Media, Lumenalta, Northstar Energy.

revenue_transactions: settled gross amounts include Aster transactions of 1000 and 500, BluePeak transactions of 900 and 600, Cedar transactions of 1200 and 300, Delta of 1499, Ember of 1300, and Lumenalta of 1600. A pending BluePeak transaction and an out-of-period Aster transaction must be excluded.

revenue_adjustments: Lumenalta has 100 in refunds, Ember has 100 in refunds, and Aster, BluePeak, and Cedar have zero or null adjustments. Include an adjustment for an unknown transaction to test join behavior.

Schema

companies
ColumnTypeDescription
company_idPKINTUnique company identifier
company_nameVARCHAR(100)Company display name
revenue_transactions
ColumnTypeDescription
transaction_idPKINTUnique revenue transaction identifier
company_idINTCompany associated with the transaction
transaction_dateDATEDate the transaction was recorded
gross_amountNUMERIC(12,2)Gross transaction amount before refunds
statusVARCHAR(20)Settlement status
revenue_adjustments
ColumnTypeDescription
adjustment_idPKINTUnique adjustment identifier
transaction_idINTTransaction receiving the adjustment
adjustment_typeVARCHAR(20)Adjustment category
amountNUMERIC(12,2)Refund amount
Tablescompaniesrevenue_transactionsrevenue_adjustments
Interviewer

Your question is SQL: Top Companies by Revenue. 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.