BBampaa

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 →
About 5–10 minutes · no sign-up needed

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

Result:

raw_countclean_countraw_revenueclean_revenue
53255.49255.49

Step by step

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

Try each one in your head first, then open the answer.

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_count
2. 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_count
3. 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_revenue
4. 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_missing
5. 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_refunds

Learn 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 →
About 5–10 minutes · no sign-up needed