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
SQL Transform With Window Functions
00:00
5 left

SQL Transform With Window Functions

MediumSQL · PostgreSQL

Problem

Solve a medium-difficulty data transformation problem using optimized SQL queries and window functions.

Write a PostgreSQL query that evaluates completed transactions from January 1 through March 31, 2025. Include only accounts with at least two qualifying transactions. The query should calculate each account's qualifying transaction count and total revenue, then assign its revenue rank within its segment.

Output

  1. One row per qualifying account.
  2. Columns: account_id, account_name, segment, completed_transactions, total_revenue, and segment_rank.
  3. Sort by segment, ascending segment_rank, then ascending account_id.

Output details

  • Treat transaction status exactly completed as qualifying.
  • Include transactions dated 2025-01-01 and through 2025-03-31, excluding 2025-04-01 and later.
  • Assign equal total_revenue values the same segment_rank, leaving gaps after ties.
  • Treat a NULL revenue sum as 0.

Schema

accounts
ColumnTypeDescription
account_idPKINTUnique account identifier
account_nameVARCHAR(100)Account display name
segmentVARCHAR(50)Market segment assigned to the account
transactions
ColumnTypeDescription
transaction_idPKINTUnique transaction identifier
account_idINTAccount associated with the transaction
transaction_dateDATEDate the transaction occurred
statusVARCHAR(20)Transaction processing status
amountDECIMAL(12,2)Transaction revenue amount
Tablesaccountstransactions
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the two 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.