Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
SQL Month-over-Month Change
00:00
5 left

SQL Month-over-Month Change

MediumSQL · PostgreSQL

Problem

Write a SQL query using window functions to compute each user's month-over-month change in transaction volume.

Use the users and transactions tables. Treat transaction volume as the number of transactions in each recorded calendar month, and compare each month with the user's previous recorded month.

Output

  1. One row per user and recorded transaction month.
  2. Columns: user_id, user_name, month_start, transaction_volume, previous_month_volume, and month_over_month_change.
  3. For the first recorded month, previous volume and change should be NULL.
  4. Exclude transactions that do not match a user. Order by user_id, then month_start.

Schema

users
ColumnTypeDescription
user_idPKINTUnique user identifier
user_nameVARCHAR(100)User display name
transactions
ColumnTypeDescription
transaction_idPKINTUnique transaction identifier
user_idINTUser associated with the transaction
transaction_dateDATEDate on which the transaction occurred
Tablesuserstransactions
Interviewer

Your question is SQL Month-over-Month Change. 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.