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 →Example
Detecting Impossible Negative Retail Values
SELECT *
FROM inventory
WHERE stock_count < 0| product_id | brand | category | stock_count | price |
|---|---|---|---|---|
| P2002 | UrbanWear | Clothing | -5 | 49.95 |
| P2004 | FreshMart | Grocery | -12 | 3.49 |
Step by step
- The asterisk symbol means all columns.
SELECT * - FROM tells SQL which table to pull the data from.
SELECT * FROM inventory - WHERE keeps only the rows that match your condition.
SELECT * FROM inventory WHERE stock_count < 0;
Practice
1. Using orders, write a query to find rows where revenue is negative (a likely data error).
SELECT * FROM orders WHERE revenue < 02. Using customers, write a query to find rows where customer_id is negative (a likely data error).
SELECT * FROM customers WHERE customer_id < 03. Using products, write a query to find rows where price is negative (a likely data error).
SELECT * FROM products WHERE price < 04. Using store_sales, write a query to find rows where revenue is negative (a likely data error).
SELECT * FROM store_sales WHERE revenue < 05. Using employees, write a query to find rows where salary is negative (a likely data error).
SELECT * FROM employees WHERE salary < 0Learn 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 →