Find Top-Funded Villages and Their Top Projects
AdobeSoftware Engineer · Online AssessmentLast 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
- Year Boundary: Filter records strictly to calendar year 2024:
funded_at >= '2024-01-01 00:00:00'andfunded_at < '2025-01-01 00:00:00'. - 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.
- 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.
- Output Columns:
village_id(INT)village_name(VARCHAR)village_total_funding(DECIMAL/NUMERIC)project_id(INT)project_name(VARCHAR)project_total_funding(DECIMAL/NUMERIC)
- Ordering: Sort the output deterministically by
village_id ASC, thenproject_id ASC.
Example
Input:
villages:
| village_id | village_name |
|---|---|
| 1 | Greenfield |
| 2 | Riverdale |
| 3 | Oakridge |
projects:
| project_id | village_id | project_name |
|---|---|---|
| 101 | 1 | Solar Well |
| 102 | 1 | Road Repair |
| 103 | 2 | Clinic Expansion |
| 104 | 2 | School Library |
| 105 | 3 | Irrigation Canal |
project_fundings:
| funding_id | project_id | amount | funded_at |
|---|---|---|---|
| 1 | 101 | 3000.00 | 2024-03-15 10:00:00 |
| 2 | 101 | 2000.00 | 2024-06-01 09:00:00 |
| 3 | 102 | 5000.00 | 2024-08-10 12:00:00 |
| 4 | 103 | 6000.00 | 2024-04-20 11:00:00 |
| 5 | 104 | 4000.00 | 2024-09-05 14:00:00 |
| 6 | 105 | 9000.00 | 2023-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_id | village_name | village_total_funding | project_id | project_name | project_total_funding |
|---|---|---|---|---|---|
| 1 | Greenfield | 10000.00 | 101 | Solar Well | 5000.00 |
| 1 | Greenfield | 10000.00 | 102 | Road Repair | 5000.00 |
| 2 | Riverdale | 10000.00 | 103 | Clinic Expansion | 6000.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?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.