Guided Project · Week 2 of 4 · 6 hours
Data Cleaning and SQL Analysis
Make the data trustworthy, then answer your first questions with SQL.
Learning objectives
By the end of this week you will be able to:
- Profile the data and list its quality problems.
- Clean the data and keep a cleaning log.
- Write SQL queries that answer your SMART questions.
- Save queries and results in an organized way.
Lessons
Data Cleaning
Start with a data profile: for each column, record its type, number of missing values, number of unique values, and the minimum and maximum. Then look for the usual problems: duplicates, missing values, inconsistent text, wrong data types, impossible values (negative quantities, end time before start time) and outliers.
Clean in whichever tool suits the data: Power Query or Excel for small files, SQL for data already in a database, pandas for large or repeated work. Whatever the tool, keep a cleaning log:
| Step | Problem found | Action taken | Rows affected |
|---|---|---|---|
| 1 | 1,204 duplicate ride IDs | Removed duplicates | 1,204 |
| 2 | Rides with negative length | Removed (data error) | 87 |
| 3 | Station names with trailing spaces | Trimmed | 9,512 |
Save the cleaned data in data/clean. Your final row count, and what was removed, goes into your documentation.
SQL Analysis
Load the clean data into your database and answer your SMART questions with SQL. Work question by question:
- Write the question as a comment at the top of the query.
- Start simple (one table, a COUNT) and check the result makes sense.
- Add filters, grouping and joins step by step.
- Save each query as its own file in the
sqlfolder, numbered in order (01_rides_by_type.sql). - Export the result to CSV or copy it into a results table.
Typical project queries: totals and averages by group, trends by month, top and bottom 10 lists, comparisons between segments, and percentages of the total. A useful pattern for percentages:
SELECT rider_type,
COUNT(*) AS rides,
ROUND(100.0 * COUNT(*) / (SELECT COUNT(*) FROM rides), 1) AS pct_of_rides
FROM rides
GROUP BY rider_type;Watch
- VideoData Analyst Portfolio Project: Data Cleaning in SQL (Alex The Analyst)
A full cleaning project to follow along with.
- VideoClean Messy Data in Python (Step-by-Step for Beginners)
The same ideas in pandas, if you prefer Python.
Practice
Profile your dataset, clean it and complete your cleaning log. Then write at least five SQL queries that answer or partly answer your SMART questions, saving each one as a numbered file.
Check your understanding
Try each question first, then open it to see the answer.
What four columns should a cleaning log have?
Step, problem found, action taken, rows affected.
Why start a query with a simple COUNT?
To check the data and joins are behaving as expected before adding complexity.
Why save each query as a separate numbered file?
So the analysis is organized, easy to rerun and easy for others to follow.
Assignment
Checkpoint 2: submit your data profile, cleaning log, cleaned data file and at least five SQL queries with their results.