top of page
Search

World Life Expectancy Exploratory (SQL)

Feb 19, 2025
6 min read

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.




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!





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.1

This 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:

  1. Explored how life expectancy changed over the years and which countries saw the largest improvements.

  2. Compared life expectancy across countries with different GDPs.

  3. Analyzed the difference in life expectancy between developed and developing countries.

  4. 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!



Author: Merrill Kitchens


 
 
 

1 Comment


dharber583
Jan 20

Impressive! Keep up the good work.

Like
vvffg_5_edited_edited.png

Data analytics professional with a recent BBA in Business Economics. Proficient in data analysis platforms including MySQL, Google Spreadsheets, Excel, Tableau, Python, R, Pandas, Azure, AWS, Big Query, Econometrics, and data visualization. Adept at extracting actionable insights and presenting data-driven recommendations to stakeholders. Eager to leverage technical skills and academic knowledge to drive impactful business decisions.

•    SQL (MySQL, PostgreSQL, Big Query)   
•    Tableau (Advanced Data Preparation, Creating Dashboards, Data Cleaning)
•    Python (Pandas, NumPy, Matplotlib, Seaborn)    
•    Excel (VLOOKUP, Pivot Tables, Conditional Formatting, Automation)    
•    Microsoft Power BI      
•    Google Spreadsheets
•    Aws
•    Azure           
•    Econometrics
•    Microsoft Office: Proficient in Outlook, Word, Excel, PowerPoint Microsoft Navision
•    Knowledge: Educated in Economic Concepts, Financial Analytical Skills

 

Education:

Bachelor of Business Administration, Business Economics at Georgia State University      

- (3.5 GPA): Data Collection & Analysis, Data Structures & Algorithms, Economic Research

 

Certifications:
•    Analyst Builder for Data Analyst Certification: (Advanced MySQL for Data Analysis, Pandas, Python, Tableau)
•    Google Data Analytics Certification: (Advanced Excel for Automation, R, Big Query) 
•    Market Research Foundations (Market Research Program)

  • GitHub
  • LinkedIn
bottom of page