Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Running Total With Window Functions

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

Your question is Running Total With Window Functions. Start with the requirements and the one table 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

Sparksoft wants to show each user's cumulative spend over time in its analytics view. Write a SQL query to calculate a running total of order amounts for every user, ordered by purchase date.

Requirements

  1. Return one row per order.
  2. Show user_id, order_date, order_amount, and the cumulative spend as running_total.
  3. Reset the running total for each user.
  4. Order the final result by user_id, then order_date, then order_id.

Schema

user_orders
ColumnTypeDescription
order_idPKINTUnique identifier for each order
user_idINTIdentifier of the user who placed the order
order_dateDATEDate when the order was created
order_amountDECIMAL(10,2)Order value, may be NULL for incomplete records
channelVARCHAR(20)Order source channel
Tablesuser_orders
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results