Lessons › SQL › Chapter 9: Data Cleaning
SQL lesson
Before-and-after validation comparing row counts and sums after cleaning
After cleaning, compare the table before and after: how many rows, and what the totals add up to. If rows went missing that shouldn't have, or totals changed, something went wrong.
Try this lesson free →Example
Check the cleaning worked
SELECT (SELECT COUNT(*) FROM orders_raw) AS raw_count,
(SELECT COUNT(*) FROM orders_clean) AS clean_count,
(SELECT SUM(revenue) FROM orders_raw) AS raw_revenue,
(SELECT SUM(revenue) FROM orders_clean) AS clean_revenue| raw_count | clean_count | raw_revenue | clean_revenue |
|---|---|---|---|
| 5 | 3 | 255.49 | 255.49 |
Step by step
- How many rows the raw and clean tables have.
SELECT (SELECT COUNT(*) FROM orders_raw) AS raw_count, (SELECT COUNT(*) FROM orders_clean) AS clean_count - Totals should match, unless you meant to remove something.
SELECT (SELECT COUNT(*) FROM orders_raw) AS raw_count, (SELECT COUNT(*) FROM orders_clean) AS clean_count, (SELECT SUM(revenue) FROM orders_raw) AS raw_revenue, (SELECT SUM(revenue) FROM orders_clean) AS clean_revenue - A query in brackets gives one number, so every check fits in one row.
SELECT (SELECT COUNT(*) FROM orders_raw) AS raw_count, (SELECT COUNT(*) FROM orders_clean) AS clean_count, (SELECT SUM(revenue) FROM orders_raw) AS raw_revenue, (SELECT SUM(revenue) FROM orders_clean) AS clean_revenue
Practice
1. Cleaning removed repeated orders. Compare the row counts of orders_raw and orders_clean. Name them raw_count and clean_count.
SELECT (SELECT COUNT(*) FROM orders_raw) AS raw_count, (SELECT COUNT(*) FROM orders_clean) AS clean_count2. Cleaning removed a repeated product. Compare the row counts of products_raw and products_clean. Name them raw_count and clean_count.
SELECT (SELECT COUNT(*) FROM products_raw) AS raw_count, (SELECT COUNT(*) FROM products_clean) AS clean_count3. Compare the total revenue of store_sales_raw and store_sales_clean. Name them raw_revenue and clean_revenue. Only the repeated sale should be gone.
SELECT (SELECT SUM(revenue) FROM store_sales_raw) AS raw_revenue, (SELECT SUM(revenue) FROM store_sales_clean) AS clean_revenue4. Count the customers with no email in customers_raw and in customers_clean. Name them raw_missing and clean_missing. Cleaning should have filled them.
SELECT (SELECT COUNT(*) FROM customers_raw WHERE email IS NULL) AS raw_missing, (SELECT COUNT(*) FROM customers_clean WHERE email IS NULL) AS clean_missing5. Compare returns_raw and returns_clean: the row counts and the refund totals, as four columns. Name them raw_count, clean_count, raw_refunds and clean_refunds.
SELECT (SELECT COUNT(*) FROM returns_raw) AS raw_count, (SELECT COUNT(*) FROM returns_clean) AS clean_count, (SELECT SUM(refund_amount) FROM returns_raw) AS raw_refunds, (SELECT SUM(refund_amount) FROM returns_clean) AS clean_refundsLearn it by doing. This lesson is a short hands-on level on Bampaa: see it work, then build and write it yourself, with instant feedback.
Try this lesson free →