Data Analyst Program

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:

StepProblem foundAction takenRows affected
11,204 duplicate ride IDsRemoved duplicates1,204
2Rides with negative lengthRemoved (data error)87
3Station names with trailing spacesTrimmed9,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:

  1. Write the question as a comment at the top of the query.
  2. Start simple (one table, a COUNT) and check the result makes sense.
  3. Add filters, grouping and joins step by step.
  4. Save each query as its own file in the sql folder, numbered in order (01_rides_by_type.sql).
  5. 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

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.