How do you find anomalies and data integrity violations?
A rule "this must not happen" is written as a query returning exactly the violations: an empty result means normal, and the rows go into the bug report. This finds orphans, duplicates, impossible values and stuck states. A result is a suspicion, not a verdict, so it is checked against the requirement. If the violations could be prohibited by FOREIGN KEY, CHECK or UNIQUE, the missing constraint is a bug too.
SQL · QA practice
The UI looks fine, yet the database has a transaction on a blocked account. How to hunt for findings like this systematically?
Tests check the scenarios a user goes through. But some defects live in data nobody opens: a transaction on an account blocked a week ago, a payment with no account, a balance below zero. They cannot be found by clicking the UI, but a few queries find them. This lesson is about turning domain rules into check queries and what they do not prove.
How it works and why
Each rule of the form "this must not happen" is written as a query that returns exactly the violations. An empty result means normal, and a non-empty one is already a ready description of a defect: the row identifiers go into the bug report. That is why such a query is worth saving and running again after migrations, releases and logic changes. The shape of the query depends on the class of violation.
| Class | Query shape | Example |
|---|---|---|
| Orphans | NOT EXISTS | a transaction with no account |
| Duplicates | GROUP BY … HAVING COUNT(*) > 1 | two users with one email |
| Impossible values | WHERE with a check for IS NULL | amount ≤ 0, an unknown status |
| Impossible combinations | JOIN and comparing columns of two tables | an operation after blocking |
| Stuck states | status and record age | pending for over an hour |
Example: five rows and three findings
A small data set with violations hidden in it:
Data in which violations must be found
| id | status | blocked_at |
|---|---|---|
| 1 | active | NULL |
| 2 | blocked | 2026-11-10 |
| id | account_id | amount | created |
|---|---|---|---|
| t1 | 1 | 100.00 | 11-02 |
| t2 | 2 | 50.00 | 11-09 |
| t3 | 2 | 70.00 | 11-12 |
| t4 | 9 | 30.00 | 11-05 |
| t5 | 1 | -20.00 | 11-06 |
-- orphans: a transaction with no account
SELECT t.id, t.account_id
FROM transactions t
WHERE NOT EXISTS (SELECT 1 FROM accounts a WHERE a.id = t.account_id);
-- t4
-- impossible combination: an operation after blocking
SELECT t.id, a.id AS account_id, t.created_at, a.blocked_at
FROM transactions t
JOIN accounts a ON a.id = t.account_id
WHERE a.status = 'blocked' AND t.created_at > a.blocked_at;
-- t3
-- impossible values (NULL is a violation too)
SELECT id, amount, status
FROM transactions
WHERE amount IS NULL OR amount <= 0
OR status IS NULL
OR status NOT IN ('pending', 'completed', 'failed');
-- t5 (amount -20.00)
Three queries found three different rows: t4, t3 and t5. Note the third: status NOT IN (…) for a row with status = NULL gives UNKNOWN, so without a separate status IS NULL condition such a row would silently drop out of the result. For orphans NOT EXISTS is used instead of NOT IN (SELECT …): one NULL in the subquery zeroes the whole result. The other classes are written the same way.
-- duplicates: email ignoring case
SELECT lower(email) AS email, COUNT(*) AS n
FROM users
GROUP BY lower(email)
HAVING COUNT(*) > 1;
-- stuck in pending for over an hour
SELECT id, account_id, created_at
FROM transactions
WHERE status = 'pending' AND created_at < NOW() - INTERVAL '1 hour';
Limits and alternatives
A query result is a suspicion, not a verdict. A transaction after blocking may be a legitimate refund, and pending for over an hour may be normal for a bank transfer. So first find the rule in the requirements or from an analyst and only then declare a bug. Many classes are closed by database constraints altogether: FOREIGN KEY will not allow an orphan, CHECK (amount > 0) rejects a negative amount, UNIQUE will not let a duplicate through. So quite often the right bug reads: "the constraint is missing, and the data already has violations". On large tables an anti-join without an index on account_id is slow, so on a shared environment the query is limited by date or run on a replica.
Common mistakes
NOT INfor the list of allowed statuses withoutIS NULL: rows with no status never reach the result.- Duplicates by
emailwithoutlower():Anna@x.comandanna@x.comstay unnoticed. - Treating a non-empty result as a bug without checking the rule: some findings are legitimate exceptions.
- Running such queries with no
LIMITand date on a shared database: a slow query disturbs others. - A one-off check that is not saved: the next release breaks the same thing again and nobody notices.
Check yourself
The query WHERE status NOT IN ('pending','completed','failed') returns 0 rows although the table has rows with no status. Why, and how to fix it?
Answer
For status = NULL the NOT IN condition gives UNKNOWN, and WHERE keeps only true conditions. Add OR status IS NULL as a separate branch.
GROUP BY email HAVING COUNT(*) > 1 found nothing, but the database has Anna@x.com and anna@x.com. What to change?
Answer
Grouping sees two different strings because the comparison is case-sensitive. Group by lower(email). Separately ask whether there is a unique index on lower(email): if not, that is a bug of its own.