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
-- Create a CTE to summarize donation amounts by assignments, donor types
WITH donor_cte AS (
SELECT
d1.assignment_id,
d2.donor_type,
SUM(d1.amount) AS total_donation
FROM donations AS d1
INNER JOIN donors AS d2
ON d2.donor_id = d1.donor_id
GROUP BY d1.assignment_id, d2.donor_type
)
-- Join tables and display top results ordering and limiting by rounded_total_donation_amount
SELECT
a.assignment_name,
a.region,
ROUND(ds.total_donation, 2) AS rounded_total_donation_amount,
ds.donor_type
FROM assignments AS a
INNER JOIN donor_cte AS ds
ON ds.assignment_id = a.assignment_id
ORDER BY rounded_total_donation_amount DESC
LIMIT 5;-- top_regional_impact_assignments
-- Create a CTE to calculate the total number of donations per assignment
WITH donations_cte AS (
SELECT
a.assignment_id,
COUNT(d.donation_id) AS num_total_donations
FROM assignments a
INNER JOIN donations d
ON d.assignment_id = a.assignment_id
GROUP BY a.assignment_id
),
-- Create a second CTE for ranking assignments within each region
score_cte AS (
SELECT
assignment_id,
assignment_name,
region,
impact_score,
ROW_NUMBER() OVER(PARTITION BY region ORDER BY impact_score DESC) AS assignment_rank
FROM assignments
)
-- Select and display the top assignments
SELECT
s.assignment_name,
s.region,
s.impact_score,
d.num_total_donations
FROM
score_cte AS s
INNER JOIN donations_cte AS d
ON d.assignment_id = s.assignment_id
WHERE
s.assignment_rank = 1
ORDER BY s.region ASC;