Lessons › SQL › Deep Dive: Data Cleaning
SQL lesson
Before-and-after validation comparing row counts and sums after cleaning
Before-and-after validation means capturing row counts and totals before a cleanup, then running the same checks afterward to confirm only the intended data changed.
Try this lesson free →Example
Validating Retail Cleaning Results Before and After
WITH cleaned AS (SELECT DISTINCT order_id, customer_id, revenue, status, order_date FROM orders WHERE revenue IS NOT NULL) SELECT (SELECT COUNT(*) FROM orders) AS before_rows, (SELECT SUM(revenue) FROM orders) AS before_revenue_sum, (SELECT COUNT(*) FROM cleaned) AS after_rows, (SELECT SUM(revenue) FROM cleaned) AS after_revenue_sum| before_rows | before_revenue_sum | after_rows | after_revenue_sum |
|---|---|---|---|
| 5 | 255.49 | 3 | 255.49 |
Step by step
- WITH lets you name a sub-query so the main query can reference it.
WITH cleaned AS (SELECT DISTINCT order_id, customer_id, revenue, status, order_date FROM orders WHERE revenue IS NOT NULL) - AS gives a column a friendlier name in the result.
WITH cleaned AS (SELECT DISTINCT order_id, customer_id, revenue, status, order_date FROM orders WHERE revenue IS NOT NULL) SELECT (SELECT COUNT(*) FROM orders) AS before_rows, (SELECT SUM(revenue) FROM orders) AS before_revenue_sum, (SELECT COUNT(*) FROM cleaned) AS after_rows, (SELECT SUM(revenue) FROM cleaned) AS after_revenue_sum;
Practice
1. Write a query that compares the row count of orders_raw to orders_clean to validate the cleaning step.
SELECT (SELECT COUNT(*) FROM orders_raw) AS raw_count, (SELECT COUNT(*) FROM orders_clean) AS clean_count2. Write a query that compares the row count of products_raw to products_clean to validate the cleaning step.
SELECT (SELECT COUNT(*) FROM products_raw) AS raw_count, (SELECT COUNT(*) FROM products_clean) AS clean_count3. Write a query that compares the row count of customers_raw to customers_clean to validate the cleaning step.
SELECT (SELECT COUNT(*) FROM customers_raw) AS raw_count, (SELECT COUNT(*) FROM customers_clean) AS clean_count4. Write a query that compares the row count of store_sales_raw to store_sales_clean to validate the cleaning step.
SELECT (SELECT COUNT(*) FROM store_sales_raw) AS raw_count, (SELECT COUNT(*) FROM store_sales_clean) AS clean_count5. Write a query that compares the row count of returns_raw to returns_clean to validate the cleaning step.
SELECT (SELECT COUNT(*) FROM returns_raw) AS raw_count, (SELECT COUNT(*) FROM returns_clean) AS clean_countLearn 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 →