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
-- Joining tables and displaying top results
SELECT a.assignment_name,
a.region,
ROUND(SUM(amount),2) as rounded_total_donation_amount,
ds.donor_type
FROM assignments AS a
INNER JOIN donations AS d
ON a.assignment_id = d.assignment_id
INNER JOIN donors as ds
ON d.donor_id = ds.donor_id
GROUP BY a.assignment_name,a.region,ds.donor_type
ORDER BY rounded_total_donation_amount DESC -- Ordering by highest donations
LIMIT 5 -- limiting to top 5 assignments
-- top_regional_impact_assignments
WITH d AS( -- Making a CTE to calculate the total number of donations received for each assignment
SELECT assignment_id, COUNT(donation_id) as num_total_donations
FROM donations
GROUP BY assignment_id
),
a AS(-- Building a second CTE for ranking assignments within each region
SELECT a.assignment_name,
a.region,
a.impact_score,
d.num_total_donations,
ROW_NUMBER() OVER (PARTITION BY a.region -- PARTITION BY to group assignments by region
ORDER BY a.impact_score DESC) as rank_in_region -- ORDER BY sort the assignments by impact_score in descending order
FROM assignments a
JOIN d
ON a.assignment_id = d.assignment_id
WHERE d.num_total_donations > 0
)
-- Joining CTEs, selecting and displaying the top assignments
SELECT assignment_name,
region,
impact_score,
num_total_donations
FROM a
WHERE rank_in_region = 1
ORDER BY region ASC;