Katkat

Find Top-Funded Villages and Their Top Projects

AdobeSoftware Engineer · Online Assessment

Last updated

Tests the ability to write a multi-level SQL aggregation with window functions, exact date filtering, tie handling, and deterministic sorting.

Problem Statement

You are tasked with auditing rural development initiatives. You are given three tables: villages, projects, and project_fundings.

Write a SQL query that finds every village tied for the highest total funding disbursed in calendar year 2024 (from 2024-01-01 00:00:00 up to but not including 2025-01-01 00:00:00). For each winning village, find the project (or projects, in case of a tie) that received the highest total funding in 2024 within that village.

Schema

CREATE TABLE villages (
    village_id INT PRIMARY KEY,
    village_name VARCHAR(100) NOT NULL
);

CREATE TABLE projects (
    project_id INT PRIMARY KEY,
    village_id INT NOT NULL,
    project_name VARCHAR(100) NOT NULL
);

CREATE TABLE project_fundings (
    funding_id INT PRIMARY KEY,
    project_id INT NOT NULL,
    amount DECIMAL(12, 2) NOT NULL,
    funded_at TIMESTAMP NOT NULL
);

Requirements

  1. Year Boundary: Filter records strictly to calendar year 2024: funded_at >= '2024-01-01 00:00:00' and funded_at < '2025-01-01 00:00:00'.
  2. Aggregation: Projects may receive multiple funding disbursements in 2024; sum them to determine each project's 2024 total. Sum all project totals in a village to get the village total.
  3. Preserve Ties:
    • If multiple villages tie for the highest total 2024 funding, include all of them.
    • Within each winning village, if multiple projects tie for the highest funding in 2024, include all of them.
  4. Output Columns:
    • village_id (INT)
    • village_name (VARCHAR)
    • village_total_funding (DECIMAL/NUMERIC)
    • project_id (INT)
    • project_name (VARCHAR)
    • project_total_funding (DECIMAL/NUMERIC)
  5. Ordering: Sort the output deterministically by village_id ASC, then project_id ASC.

Example

Input:

villages:

village_idvillage_name
1Greenfield
2Riverdale
3Oakridge

projects:

project_idvillage_idproject_name
1011Solar Well
1021Road Repair
1032Clinic Expansion
1042School Library
1053Irrigation Canal

project_fundings:

funding_idproject_idamountfunded_at
11013000.002024-03-15 10:00:00
21012000.002024-06-01 09:00:00
31025000.002024-08-10 12:00:00
41036000.002024-04-20 11:00:00
51044000.002024-09-05 14:00:00
61059000.002023-12-31 23:59:59

Explanation:

  • Oakridge (Village 3) received 9000.00 in 2023, so its 2024 total is 0.
  • Greenfield (Village 1): Project 101 received 5000.00, Project 102 received 5000.00. Total village funding = 10,000.00.
  • Riverdale (Village 2): Project 103 received 6000.00, Project 104 received 4000.00. Total village funding = 10,000.00.
  • Both Village 1 and Village 2 tie for the top spot at 10,000.00.
  • In Village 1, Projects 101 and 102 tie for top project (5000.00 each).
  • In Village 2, Project 103 is the sole top project (6000.00).

Output:

village_idvillage_namevillage_total_fundingproject_idproject_nameproject_total_funding
1Greenfield10000.00101Solar Well5000.00
1Greenfield10000.00102Road Repair5000.00
2Riverdale10000.00103Clinic Expansion6000.00

Hints

Hint 1

First, filter funding records to 2024 and aggregate funding at the project level.

Hint 2

Use window functions like `SUM() OVER (PARTITION BY village_id)` to calculate village totals, or compute village sums in a separate CTE.

Hint 3

Apply `DENSE_RANK()` or `RANK()` over village totals to identify top villages, and within those villages, rank projects partitioned by village.

Follow-up questions

  • How would you index these tables to optimize this query if `project_fundings` contains hundreds of millions of rows?
  • If the requirement changed to return the top 3 projects per top village instead of rank 1, how would `RANK()` vs `DENSE_RANK()` vs `ROW_NUMBER()` alter the result set on ties?
  • How would the query change if we also needed to include villages that had zero funding in 2024 in cases where no village received any funding at all?