Lessons › SQL › Deep Dive: Data Validation
SQL lesson
Cross-field validation
Cross-field validation checks whether values in different columns actually agree with each other, not just whether each column looks clean on its own.
Try this lesson free →Example
Checking Retail Fields Against Each Other
SELECT order_id, order_date, ship_date, quantity, price, revenue,
CASE
WHEN ship_date < order_date THEN 'Ship date before order date'
WHEN ROUND(revenue, 2) <> ROUND(quantity * price, 2) THEN 'Revenue mismatch'
ELSE 'OK'
END AS validation_flag
FROM orders
WHERE ship_date < order_date
OR ROUND(revenue, 2) <> ROUND(quantity * price, 2)| order_id | validation_flag |
|---|---|
| O2002 | Ship date before order date |
| O2003 | Revenue mismatch |
| O2005 | Ship date before order date |
Step by step
- Run the query without ROUND so you can see what the unfiltered values look like.
SELECT order_id, order_date, ship_date, quantity, price, revenue, CASE WHEN ship_date < order_date THEN 'Ship date before order date' WHEN revenue <> ROUND(quantity * price, 2) THEN 'Revenue mismatch' ELSE 'OK' END AS validation_flag FROM orders WHERE ship_date < order_date OR ROUND(revenue, 2) <> ROUND(quantity * price, 2); - ROUND rounds the value to the nearest integer (or to N decimal places with a second argument).
SELECT order_id, order_date, ship_date, quantity, price, revenue, CASE WHEN ship_date < order_date THEN 'Ship date before order date' WHEN ROUND(revenue, 2) <> ROUND(quantity * price, 2) THEN 'Revenue mismatch' ELSE 'OK' END AS validation_flag FROM orders WHERE ship_date < order_date OR ROUND(revenue, 2) <> ROUND(quantity * price, 2); - AS gives the ROUND result a friendly column name so the output reads cleanly.
SELECT order_id, order_date, ship_date, quantity, price, revenue, CASE WHEN ship_date < order_date THEN 'Ship date before order date' WHEN ROUND(revenue, 2) AS round_result <> ROUND(quantity * price, 2) THEN 'Revenue mismatch' ELSE 'OK' END AS validation_flag FROM orders WHERE ship_date < order_date OR ROUND(revenue, 2) <> ROUND(quantity * price, 2);
Practice
1. Using orders, write a query to find rows where customer_id is BEFORE order_id (a logical inconsistency).
SELECT * FROM orders WHERE customer_id < order_id2. Using store_sales, write a query to find rows where store_id is BEFORE sale_id (a logical inconsistency).
SELECT * FROM store_sales WHERE store_id < sale_id3. Using products, write a query to find rows where brand is BEFORE product_id (a logical inconsistency).
SELECT * FROM products WHERE brand < product_id4. Using customers, write a query to find rows where full_name is BEFORE customer_id (a logical inconsistency).
SELECT * FROM customers WHERE full_name < customer_id5. Using returns, write a query to find rows where order_id is BEFORE return_id (a logical inconsistency).
SELECT * FROM returns WHERE order_id < return_idLearn 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 →