top of page
Search

Automated Data Cleaning Project (SQL)

Feb 19, 2025
4 min read

Updated: Mar 5, 2025

In this project, I built an Automated Data Cleaner in SQL to streamline the process of cleaning and updating the dataset. Here's how I went about it:





Automated Data Cleaning Project (SQL)

Step 1: Create the us_household_income_Cleaned table

First, we created a new table us_household_income_Cleaned to store the cleaned data. This table mirrors the original us_householdincome table but allows us to work with a fresh, cleaned version of the data.

SELECT *
FROM bakery.ushouseholdincome;

SELECT *
FROM bakery.us_household_income_cleaned;

CREATE TABLE IF NOT EXISTS `us_household_income_Cleaned` (
  `row_id` int DEFAULT NULL,
  `id` int DEFAULT NULL,
  `State_Code` int DEFAULT NULL,
  `State_Name` text,
  `State_ab` text,
  `County` text,
  `City` text,
  `Place` text,
  `Type` text,
  `Primary` text,
  `Zip_Code` bigint DEFAULT NULL,
  `Area_Code` int DEFAULT NULL,
  `ALand` bigint DEFAULT NULL,
  `AWater` int DEFAULT NULL,
  `Lat` double DEFAULT NULL,
  `Lon` double DEFAULT NULL,
  `TimeStamp` TIMESTAMP DEFAULT NULL
);

Explanation of Code:

  • This step ensures that a new table, us_household_income_Cleaned, is created in the database with the same structure as the original ushouseholdincome table. The reason for creating this new table is to avoid modifying the original data directly, thus preserving it as a backup.

  • The table has a TimeStamp column to track when the data is copied into this cleaned version, which is useful for monitoring updates in the future.




Step 2: Copy data from the original table

We copied the entire dataset from the us_householdincome table into the new us_household_income_Cleaned table, adding a timestamp to track when the data was copied.

INSERT INTO us_household_income_Cleaned
SELECT *, CURRENT_TIMESTAMP
FROM bakery.ushouseholdincome;

Explanation of Code:

  • This SQL query copies all the data from the ushouseholdincome table into the new us_household_income_Cleaned table.

  • We also add a CURRENT_TIMESTAMP to each row to record when the data was copied, which helps track changes or updates over time.


Step 3: Remove Duplicates

Next, we identified and removed duplicates based on the id and TimeStamp. This ensures that each record is unique in the cleaned dataset.

DELETE FROM us_household_income_Cleaned 
WHERE 
    row_id IN (
    SELECT row_id
    FROM (
        SELECT row_id, id,
            ROW_NUMBER() OVER (
                PARTITION BY id,`TimeStamp`
                ORDER BY id,`TimeStamp`) AS row_num
        FROM us_household_income_Cleaned
    ) duplicates
    WHERE row_num > 1
);

Explanation of Code:

  • In this step, we use ROW_NUMBER() to identify duplicate rows. The PARTITION BY clause groups the rows by id and TimeStamp, and ROW_NUMBER() assigns a sequential number to each row in the group.

  • We then delete any row where the row_num is greater than 1, meaning the row is a duplicate of another one.

  • By removing duplicates, we ensure that the dataset contains only unique records.



Step 4: Fixing Data Quality Issues

We standardized and fixed any typos or inconsistent formatting in the data, such as ensuring correct state names and standardizing text to uppercase.

UPDATE us_household_income_Cleaned
SET State_Name = 'Georgia'
WHERE State_Name = 'georia';

UPDATE us_household_income_Cleaned
SET County = UPPER(County);

UPDATE us_household_income_Cleaned
SET City = UPPER(City);

UPDATE us_household_income_Cleaned
SET Place = UPPER(Place);

UPDATE us_household_income_Cleaned
SET State_Name = UPPER(State_Name);

UPDATE us_household_income_Cleaned
SET `Type` = 'CDP'
WHERE `Type` = 'CPD';

UPDATE us_household_income_Cleaned
SET `Type` = 'Borough'
WHERE `Type` = 'Boroughs';

Explanation of Code:

  • In this section, we addressed data quality issues that arise from inconsistent or incorrect data entries.

    • For example, we corrected a typo in the State_Name column from 'georia' to 'Georgia'.

    • We standardized text in columns like County, City, and Place to uppercase to maintain uniformity and avoid discrepancies when querying the data.

    • We also corrected values in the Type column (e.g., changing 'CPD' to 'CDP' and 'Boroughs' to 'Borough') to ensure consistency.




Step 5: Automate the Process

We then created an event to automate the cleaning process every 30 days, ensuring that the data remains fresh and up-to-date.

CREATE EVENT run_data_cleaning
       ON SCHEDULE EVERY 30 DAY
       DO CALL Copy_and_Clean_Data();

Explanation of Code:

  • This step sets up an automated event that runs every 30 days.

  • The event calls the stored procedure Copy_and_Clean_Data(), which runs the cleaning process automatically at regular intervals.

  • This ensures that the data in the us_household_income_Cleaned table stays updated, and any new data from the original ushouseholdincome table is cleaned without manual intervention.



Step 6: Verify the Data

Finally, we verified that the data was cleaned successfully by checking for any remaining duplicates and ensuring the dataset was complete.

SELECT row_id, id, row_num
FROM (
    SELECT row_id, id,
        ROW_NUMBER() OVER (PARTITION BY id ORDER BY id) AS row_num
    FROM us_household_income_Cleaned
) duplicates
WHERE row_num > 1;

SELECT COUNT(row_id)
FROM us_household_income_Cleaned;

SELECT State_Name, COUNT(State_Name)
FROM us_household_income_Cleaned
GROUP BY State_Name;

Explanation of Code:

  • We used this step to check for any remaining duplicates and ensure that the dataset was properly cleaned.

  • The first query looks for any rows that still have duplicates by showing the row_id, id, and row_num for each duplicated row.

  • The second query counts how many rows are in the us_household_income_Cleaned table, which helps us ensure the data cleaning process didn't inadvertently remove or add rows.

  • The third query groups the cleaned data by State_Name and counts how many rows exist for each state, helping us check for consistency and completeness in the data.






















Step 7: Review Cleaned Data

Once the data was clean, we reviewed the results and confirmed that everything was properly updated.

SELECT *
FROM us_household_income_cleaned;

SELECT DISTINCT TimeStamp 
FROM bakery.us_household_income_cleaned;

Explanation of Code:

  • Finally, we review the cleaned data by selecting all records from the us_household_income_cleaned table. This allows us to see if the data looks accurate and consistent after the cleaning process.

  • We also check the TimeStamp to confirm that the automated data cleaning process is running as expected and that the most recent data is being updated.


Conclusion:

Through these steps, we have successfully automated the data cleaning process, ensuring that the us_household_income_cleaned table remains accurate, reliable, and free of errors. By using a combination of SQL queries, stored procedures, and automated events, we've created a robust and efficient system to maintain clean data. This process not only ensures accuracy but also provides an efficient way to handle data for analysis.



Author: Merrill Kitchens

 
 
 

1 Comment


dharber583
Jan 20

Good work merrill!!

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