QA Interview Hub · Knowledge base · Quizzes · Українська

SQL › SQL in QA work

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.

ClassQuery shapeExample
OrphansNOT EXISTSa transaction with no account
DuplicatesGROUP BY … HAVING COUNT(*) > 1two users with one email
Impossible valuesWHERE with a check for IS NULLamount ≤ 0, an unknown status
Impossible combinationsJOIN and comparing columns of two tablesan operation after blocking
Stuck statesstatus and record agepending 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

accounts
idstatusblocked_at
1activeNULL
2blocked2026-11-10
transactions
idaccount_idamountcreated
t11100.0011-02
t2250.0011-09
t3270.0011-12
t4930.0011-05
t51-20.0011-06
Figure 1. Account 2 was blocked on 10 November, and t3 was created on the 12th. Account 9 does not exist, the amount of t5 is negative.
-- 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 IN for the list of allowed statuses without IS NULL: rows with no status never reach the result.
  • Duplicates by email without lower(): Anna@x.com and anna@x.com stay unnoticed.
  • Treating a non-empty result as a bug without checking the rule: some findings are legitimate exceptions.
  • Running such queries with no LIMIT and 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.

In an interview. Junior: I keep a set of integrity queries: orphans through an anti-join, duplicates through GROUP BY with HAVING, impossible values such as a negative balance, and stuck statuses. They find bugs that cannot be seen from the UI. Middle/Senior: I write each rule so that the query returns the violations and an empty result means normal. I remember NULL in NOT IN, case in duplicates and that the result is only a suspicion that I check against the requirement. If a class of violations is closed by FOREIGN KEY, CHECK or UNIQUE, I file a bug about the missing constraint.

More articles in this chapter