Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Monthly KPI SQL With Joins

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

Your question is Monthly KPI SQL With Joins. Start with the requirements and the four 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

The Nature Conservancy needs a monthly operational KPI view for regional management reporting. Write a PostgreSQL query that combines program activity and finance data without double-counting either source.

Requirements

  1. Return every region for January through March 2024, including months with no activity or spending.
  2. Calculate active programs, completed deliverables, completed operational hours, and actual spend by region and month.
  3. Compare each month's spend with the prior month using LAG.
  4. Return zero for missing monthly activity or finance values and order by region and month.

Schema

regions
ColumnTypeDescription
region_idPKINTUnique regional identifier
region_nameVARCHAR(100)Name of the operating region
programs
ColumnTypeDescription
program_idPKINTUnique program identifier
region_idINTOwning region
program_nameVARCHAR(150)Program name
program_activities
ColumnTypeDescription
activity_idPKINTUnique activity identifier
program_idINTRelated conservation program
activity_dateDATEDate of the activity
activity_statusVARCHAR(30)Operational status
hoursNUMERIC(10,2)Hours recorded for the activity
finance_transactions
ColumnTypeDescription
transaction_idPKINTUnique finance transaction identifier
program_idINTProgram charged for the transaction
transaction_dateDATETransaction posting date
amountNUMERIC(12,2)Transaction amount
expense_categoryVARCHAR(60)Finance expense category
Tablesregionsprogramsprogram_activitiesfinance_transactions
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results