BBampaa

Lessons › SQL › Deep Dive: Data Validation

SQL lesson

Detecting negative values where only positives are expected

Filtering for values below zero in a column that should only hold positive numbers is a quick sanity check that surfaces corrupted or mistakenly entered data early.

Try this lesson free →
About 5–10 minutes · no sign-up needed

Example

Detecting Impossible Negative Retail Values

SELECT *
FROM inventory
WHERE stock_count < 0

Result:

product_idbrandcategorystock_countprice
P2002UrbanWearClothing-549.95
P2004FreshMartGrocery-123.49

Step by step

  1. The asterisk symbol means all columns.
    SELECT *
  2. FROM tells SQL which table to pull the data from.
    SELECT *
    FROM inventory
  3. WHERE keeps only the rows that match your condition.
    SELECT *
    FROM inventory
    WHERE stock_count < 0;

Practice

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

1. Using orders, write a query to find rows where revenue is negative (a likely data error).
SELECT * FROM orders WHERE revenue < 0
2. Using customers, write a query to find rows where customer_id is negative (a likely data error).
SELECT * FROM customers WHERE customer_id < 0
3. Using products, write a query to find rows where price is negative (a likely data error).
SELECT * FROM products WHERE price < 0
4. Using store_sales, write a query to find rows where revenue is negative (a likely data error).
SELECT * FROM store_sales WHERE revenue < 0
5. Using employees, write a query to find rows where salary is negative (a likely data error).
SELECT * FROM employees WHERE salary < 0

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