Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Extract Trends With Window Functions

MediumSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is Extract Trends With Window Functions. Start with the requirements and the two tables on the right.

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.

Problem

Extract and clean transaction data using grouping, sorting, and window functions to identify user behavior trends.

Use the provided user and transaction records. Include only completed transactions with a positive, non-null amount and a matching user.

Output

  1. One row per user and transaction month.
  2. Columns: month, user_id, user_name, transaction_count, total_amount, average_amount, and monthly_rank.
  3. Rank users by monthly total amount, descending, with ties sharing a rank.
  4. Sort by month ascending, monthly_rank ascending, and user_id ascending.

Schema

users
ColumnTypeDescription
user_idPKINTUnique user identifier
user_nameVARCHAR(100)User display name
segmentVARCHAR(30)User segment
transactions
ColumnTypeDescription
transaction_idPKINTUnique transaction identifier
user_idINTUser who made the transaction
transaction_tsTIMESTAMPTransaction timestamp
amountNUMERIC(10,2)Transaction amount
statusVARCHAR(20)Transaction processing status
channelVARCHAR(20)Transaction channel
Tablesuserstransactions
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results