Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Extract Trends With Window Functions
00:00
5 left

Extract Trends With Window Functions

MediumSQL · PostgreSQL

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
Interviewer

Your question is Extract Trends With Window Functions. Start with the requirements and the two 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
You need to log in / sign up to run or submit.Ln 1
Run your query to see results here.