BBampaa

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

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)

Result:

order_idvalidation_flag
O2002Ship date before order date
O2003Revenue mismatch
O2005Ship date before order date

Step by step

  1. 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);
  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);
  3. 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

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

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_id
2. 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_id
3. Using products, write a query to find rows where brand is BEFORE product_id (a logical inconsistency).
SELECT * FROM products WHERE brand < product_id
4. 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_id
5. 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_id

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