Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL Running Total and Rank

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

Your question is SQL Running Total and Rank. 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

Enverus Prism analysts need a basin-level view of active well performance. Write a PostgreSQL query that aggregates 2024 oil production, ranks wells within each basin, and calculates a running basin total.

Requirements

  1. Include only wells whose status is active and whose basin is not null.
  2. Aggregate oil production from production_monthly for calendar year 2024, retaining active wells with no matching production.
  3. Rank wells within each basin by total oil production in descending order. Wells with equal totals must receive the same rank.
  4. Calculate a cumulative oil total within each basin, ordered by total production descending and then well_id.

Schema

wells
ColumnTypeDescription
well_idPKINTEGERUnique well identifier
well_nameVARCHAR(100)Well name
basinVARCHAR(50)Production basin
operatorVARCHAR(100)Operating company
statusVARCHAR(20)Current well status
production_monthly
ColumnTypeDescription
production_idPKINTEGERUnique production record identifier
well_idINTEGERReferenced well identifier
production_monthDATEMonth represented by the production record
oil_bblINTEGERMonthly oil production in barrels
Tableswellsproduction_monthly
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results