GoodThought NGO has been a catalyst for positive change, focusing its efforts on education, healthcare, and sustainable development to make a significant difference in communities worldwide. With this mission, GoodThought has orchestrated an array of assignments aimed at uplifting underprivileged populations and fostering long-term growth.
This project offers a hands-on opportunity to explore how data-driven insights can direct and enhance these humanitarian efforts. In this project, you'll engage with the GoodThought PostgreSQL database, which encapsulates detailed records of assignments, funding, impacts, and donor activities from 2010 to 2023. This comprehensive dataset includes:
Assignments: Details about each project, including its name, duration (start and end dates), budget, geographical region, and the impact score.Donations: Records of financial contributions, linked to specific donors and assignments, highlighting how financial support is allocated and utilized.Donors: Information on individuals and organizations that fund GoodThought’s projects, including donor types.
Refer to the below ERD diagram for a visual representation of the relationships between these data tables:
You will execute SQL queries to answer two questions, as listed in the instructions. Good luck!
-- highest_donation_assignments
WITH cte AS (
SELECT dt.assignment_id,
d.donor_type,
ROUND(SUM(dt.amount), 2) AS rounded_total_donation_amount
FROM donations AS dt
LEFT JOIN donors AS d
ON dt.donor_id = d.donor_id
GROUP BY dt.assignment_id, d.donor_type
--ORDER BY rounded_total_donation_amount DESC
)
SELECT a.assignment_name,
a.region,
c.rounded_total_donation_amount,
c.donor_type
FROM assignments AS a
INNER JOIN cte AS c
ON a.assignment_id = c.assignment_id
--GROUP BY a.assignment_name, a.region, c.donor_type, c.rounded_total_donation_amount
ORDER BY c.rounded_total_donation_amount DESC
LIMIT 5;1 hidden cell
-- top_regional_impact_assignments
WITH CTE1 AS (
SELECT assignment_id,
COUNT(*) AS num_total_donations
FROM donations
GROUP BY assignment_id
),
CTE2 AS(
SELECT a.assignment_id,
a.impact_score,
ROW_NUMBER() OVER(PARTITION BY a.region ORDER BY a.impact_score DESC) AS rank
FROM assignments AS a
RIGHT JOIN CTE1 AS c
ON c.assignment_id = a.assignment_id
GROUP BY a.assignment_id, a.impact_score
)
SELECT a.assignment_name,
a.region,
a.impact_score,
c1.num_total_donations
FROM assignments AS a
RIGHT JOIN CTE2 AS c2
ON c2.assignment_id = a.assignment_id
RIGHT JOIN CTE1 AS c1
ON c1.assignment_id = c2.assignment_id
WHERE c2.rank = 1
ORDER BY a.region
SELECT assignment_id,
COUNT(*) AS num_total_donations
FROM donations
GROUP BY assignment_id
SELECT assignment_id,
impact_score,
ROW_NUMBER() OVER(PARTITION BY region ORDER BY impact_score DESC) AS rank
FROM assignments
GROUP BY assignment_id, impact_score