World Life Expectancy Exploratory (SQL)
Updated: Mar 5, 2025
In this project, I cleaned and analyzed world life expectancy data to ensure accuracy and reliability for stakeholders. Below is a step-by-step breakdown of how I cleaned the data, making it easy to understand for beginners in SQL and data analysis.
Link To World Life Expectancy Data Cleaning Code: https://github.com/mkitchens5/Merrill-Portfolio/blob/main/World%20Life%20Exp..%20Data%20Cleaning%20Script.sql
Link To World Life Expectancy Exploratory Code: https://github.com/mkitchens5/Merrill-Portfolio/blob/main/World%20Life%20Expectancy%20Project%20(Exploratory%20Data%20Analysis).sql
World Life Expectancy Data Cleaning Process
Step 1: Checking for Duplicates
Before analyzing the data, we first checked for any duplicate records, as they can cause misleading results.
Finding Duplicate Entries
We used the CONCAT(Country, Year) function to check for repeated records of the same country in the same year.
SELECT Country, Year,
CONCAT(Country, Year) AS CountryAndYear,
COUNT(CONCAT(Country, Year)) AS Duplicate_Checker
FROM bakery.worldlifexpectancy
GROUP BY Country, Year, CONCAT(Country, Year)
HAVING Duplicate_Checker > 1;This helps us identify if any country has multiple records for the same year, which is incorrect.

Writing Code to Locate Duplicates
We used the ROW_NUMBER() function to assign a unique row number to each duplicate entry, making it easy to identify them.
SELECT *
FROM (
SELECT Row_ID, CONCAT(Country, Year),
ROW_NUMBER() OVER (PARTITION BY CONCAT(Country, Year) ORDER BY CONCAT(Country, Year)) AS Row_Num
FROM bakery.worldlifexpectancy
) AS row_table
WHERE Row_Num > 1;
This allows us to see duplicate records in the table before deleting them.

Step 2: Removing Duplicate Entries
Once we identified the duplicates, we deleted them using the following query:
DELETE FROM bakery.worldlifexpectancy
WHERE Row_ID IN (
SELECT Row_ID
FROM (
SELECT Row_ID, CONCAT(Country, Year) AS Country_Year,
ROW_NUMBER() OVER (PARTITION BY CONCAT(Country, Year) ORDER BY CONCAT(Country, Year)) AS Row_Num
FROM bakery.worldlifexpectancy
) AS row_table
WHERE Row_Num > 1
);This ensures each country has only one record per year, preventing errors in our analysis.
Step 3: Checking for Missing Data
After handling duplicates, we checked if any values were missing in important columns.
Checking for Blank Values in the "Status" Column
SELECT *
FROM bakery.worldlifexpectancy
WHERE Status = '';Missing values in the "Status" column (which indicates whether a country is "Developed" or
"Developing") can cause errors in our analysis.

Finding Existing Values in the "Status" Column
SELECT DISTINCT(Status)
FROM bakery.worldlifexpectancy
WHERE Status <> '';This helps us see the valid values already in the dataset before filling in missing data.

Step 4: Filling in Missing Data
Now that we know which values are missing, we use SQL joins to correctly fill in blank entries.
Filling in "Developing" Status
UPDATE bakery.worldlifexpectancy t1
JOIN bakery.worldlifexpectancy t2 ON t1.Country = t2.Country
SET t1.Status = 'Developing'
WHERE t1.Status = ''
AND t2.Status <> ''
AND t2.Status = 'Developing';This updates the status for countries that are already known to be "Developing."
Checking for Remaining Missing Status Values
SELECT *
FROM bakery.worldlifexpectancy
WHERE Status = '';We recheck for any remaining blanks before filling in "Developed" statuses.
Filling in "Developed" Status
UPDATE bakery.worldlifexpectancy t1
JOIN bakery.worldlifexpectancy t2 ON t1.Country = t2.Country
SET t1.Status = 'Developed'
WHERE t1.Status = ''
AND t2.Status <> ''
AND t2.Status = 'Developed';This ensures that all missing values for "Developed" countries are filled correctly.
Step 5: Final Check for Missing Data
SELECT *
FROM bakery.worldlifexpectancy
WHERE Status = '';
SELECT *
FROM bakery.worldlifexpectancy
WHERE Status IS NULL;If no results appear, it means there are no more blank or missing values.

Step 6: Viewing the Cleaned Data
Finally, we check the cleaned dataset:
SELECT * FROM bakery.worldlifexpectancy;Now, our data is ready for analysis without duplicates or missing values!

Final Thoughts
In this project, we successfully cleaned the World Life Expectancy dataset, ensuring accuracy by:
Removing duplicate records
Filling in missing data
Ensuring consistency in country status
Now, our dataset is clean and ready for further analysis!
Link To World Life Expectancy Data Cleaning Code: https://github.com/mkitchens5/Merrill-Portfolio/blob/main/World%20Life%20Exp..%20Data%20Cleaning%20Script.sql
World Life Expectancy Analysis in SQL
In this part of the project, we explore how life expectancy is related to GDP, how it changes over time, and how it differs between developed and developing countries. I'll walk you through each analysis step-by-step so it's easy to follow, even for young viewers.
Step 1: Visualizing Life Expectancy Change Over 15 Years
Life Expectancy Change by Country (Highest Increase):
First, we wanted to see which countries had the biggest increase in life expectancy over 15 years. To do this, we:
Looked at the minimum and maximum life expectancy for each country over 15 years.
Calculated the life expectancy increase (difference between max and min life expectancy) and sorted by the highest increase.
SELECT Country, MIN(`Lifeexpectancy`), MAX(`Lifeexpectancy`),
ROUND(MAX(`Lifeexpectancy`) - MIN(`Lifeexpectancy`),1) AS Life_Increase_over_15_years
FROM bakery.worldlifexpectancy
GROUP BY Country
HAVING MIN(`Lifeexpectancy`) <> 0
AND MAX(`Lifeexpectancy`) <> 0
ORDER BY Life_Increase_over_15_years DESC;This helps us see which countries have made the most progress in improving life expectancy over the years.

Life Expectancy Change by Country (Smallest Increase:
We did the same thing as above, but this time we looked at countries with the smallest increase in life expectancy.
SELECT Country, MIN(`Lifeexpectancy`), MAX(`Lifeexpectancy`),
ROUND(MAX(`Lifeexpectancy`) - MIN(`Lifeexpectancy`),1) AS Life_Increase_over_15_years
FROM bakery.worldlifexpectancy
GROUP BY Country
HAVING MIN(`Lifeexpectancy`) <> 0
AND MAX(`Lifeexpectancy`) <> 0
ORDER BY Life_Increase_over_15_years ASC;We also wanted to identify countries that haven't seen much improvement in life expectancy.

Step 2: Visualizing Average Life Expectancy by Year
Next, we wanted to see the average life expectancy over the years, so we:
Grouped the data by year and calculated the average life expectancy for each year.
SELECT Year, ROUND(AVG(`Lifeexpectancy`),2)
FROM bakery.worldlifexpectancy
WHERE `Lifeexpectancy` <> 0
AND `Lifeexpectancy` <> 0
GROUP BY Year
ORDER BY Year;This helps us understand how life expectancy has changed year by year on average.

Step 3: Visualizing Average Life Expectancy and GDP by Country
We wanted to see if there's a connection between life expectancy and GDP (Gross Domestic Product), so we calculated:
Average life expectancy and average GDP for each country.
SELECT Country, ROUND(AVG(`Lifeexpectancy`),1) AS Avg_LifeExpectancy, ROUND(AVG(`GDP`),1) AS Avg_GDP
FROM bakery.worldlifexpectancy
GROUP BY Country
HAVING Avg_GDP <> 0
AND Avg_LifeExpectancy <> 0
ORDER BY Avg_GDP ASC;This shows us if countries with lower GDPs tend to have lower life expectancy.

Higher GDP and Life Expectancy Correlation To confirm the trend, we also ran a query to find countries with higher GDPs and how their life expectancy compares.
SELECT Country, ROUND(AVG(`Lifeexpectancy`),1) AS Avg_LifeExpectancy, ROUND(AVG(GDP),1) AS Avg_GDP
FROM bakery.worldlifexpectancy
GROUP BY Country
HAVING Avg_GDP <> 0
AND Avg_LifeExpectancy <> 0
ORDER BY Avg_GDP DESC;This confirms that higher GDP countries often have higher life expectancy.

Step 4: Comparing Countries with High vs. Low GDPs
Average Life Expectancy for High GDP Countries We used the average GDP to divide countries into two groups:
High GDP: GDP above the average.
Low GDP: GDP below the average.
SELECT ROUND(AVG(GDP),1) AS Avg_GDP
FROM bakery.worldlifexpectancy; -- Average GDP is 6342.1This gives us the average GDP across all countries (6342.1). We then separated countries into high and low GDP groups.

Life Expectancy for High vs. Low GDP Countries
SELECT
SUM(CASE WHEN GDP >= 6342.1 THEN 1 ELSE 0 END) AS High_GDP_Count,
ROUND(AVG(CASE WHEN GDP >= 6342.1 THEN `Lifeexpectancy` ELSE NULL END),1) AS High_GDP_Life_Expectancy,
SUM(CASE WHEN GDP <= 6342.1 THEN 1 ELSE 0 END) AS Low_GDP_Count,
ROUND(AVG(CASE WHEN GDP <= 6342.1 THEN `Lifeexpectancy` ELSE NULL END),1) AS Low_GDP_Life_Expectancy
FROM bakery.worldlifexpectancy;This helps us see how life expectancy differs for high vs. low GDP countries:
High GDP countries have an average life expectancy of 77 years.
Low GDP countries have an average life expectancy of 66.8 years.

Step 5: Comparing Developed vs. Developing Countries
We also wanted to compare life expectancy between developed and developing countries. So, we counted how many countries are in each group and their average life expectancy.
SELECT Status, COUNT(DISTINCT Country), ROUND(AVG(`Lifeexpectancy`),1) AS avg_life
FROM bakery.worldlifexpectancy
GROUP BY Status;This helps us understand the difference in life expectancy between developed and developing countries:
Developed countries have an average life expectancy of 79.2 years.
Developing countries have an average life expectancy of 66.8 years.

Step 6: How Many People Died on Average per Year in Each Country
Lastly, we wanted to calculate how many people died each year in each country. We did this by summing up the Adult Mortality values for each country, which tells us the number of adult deaths.
SELECT Country, Year, `Lifeexpectancy`, `AdultMortality`,
SUM(`AdultMortality`) OVER(PARTITION BY Country ORDER BY Year) AS Rolling_Total
FROM bakery.worldlifexpectancy;This gives us a running total of adult mortality by year, helping us understand how death rates evolve over time for each country.

Summary
In this analysis, we:
Explored how life expectancy changed over the years and which countries saw the largest improvements.
Compared life expectancy across countries with different GDPs.
Analyzed the difference in life expectancy between developed and developing countries.
Examined how adult mortality changes over time for each country.
By breaking down these steps in a simple way, we were able to uncover interesting patterns between life expectancy and economic factors like GDP.
This project successfully cleaned and analyzed world life expectancy data to provide accurate insights for stakeholders. We followed a clear step-by-step process to address data issues, such as duplicates and missing values, and then performed in-depth analysis to uncover key trends and relationships. Thank you for joining me on this journey to explore data-driven insights in a simple and accessible way!
Link To World Life Expectancy Exploratory Code: https://github.com/mkitchens5/Merrill-Portfolio/blob/main/World%20Life%20Expectancy%20Project%20(Exploratory%20Data%20Analysis).sql
Author: Merrill Kitchens




Impressive! Keep up the good work.