sqlcmd.net validated sql reference
beginner data-quality MySQL MariaDB SQL Server PostgreSQL SQLite

Find Rows That Violate A Data Rule

Use a `WHERE` predicate to list rows that already break a business or constraint rule.

Docker-validated Not currently validation-green

Find inventory rows with invalid negative values

The intended rule is that quantity and price cannot be negative. Row 2 violates the quantity rule, and row 3 violates the price rule. Rows 1 and 4 already satisfy both checks, so they are not returned.

Rows loaded before the example query runs.
Setup
CREATE TABLE inventory (id INT, item VARCHAR(50), quantity INT, price INT);

INSERT INTO
  inventory
VALUES
  (1, 'Widget', 12, 25),
  (2, 'Gadget', -3, 10),
  (3, 'Cable', 5, -1),
  (4, 'Bracket', 0, 8);
Shared across supported engines.
SQL
SELECT
  id,
  item,
  quantity,
  price
FROM
  inventory
WHERE
  quantity < 0
  OR price < 0
ORDER BY
  id;
Returned rows for the shared example.
iditemquantityprice
2Gadget-310
3Cable5-1

The rule is a simple predicate, so the same audit SQL and result work across supported engines.

Where this command helps.

  • finding negative quantities or prices before adding check constraints
  • validating staged data before loading it into production tables

What the command is doing.

Before adding a CHECK constraint or promoting imported data, audit the existing rows with the same rule expressed as a WHERE filter. The query should return only the rows that need investigation or cleanup. This is different from creating the constraint: it is a read-only preflight check that tells you whether existing data is clean enough for the rule to be enforced.