QAAnywhere

Remote QA & testing jobs, worldwide.

Guides

SQL for testers: the queries that find real defects

By Denis Bojkovski, Test Automation Architect · Published November 25, 2026

How much SQL is enough

SQL appeared in 22 of the 86 full descriptions on QAAnywhere on 25 November 2026, about a quarter. Testers are rarely asked to design schemas. They are asked to check that what the screen says is what was stored. Joins, grouping, null handling and subqueries cover nearly all of it.

The examples use two tables: orders(id, customer_id, status, total, created_at) and order_lines(order_id, sku, quantity, unit_price).

Does the total match its parts?

SELECT o.id, o.total, COALESCE(SUM(l.quantity * l.unit_price), 0) AS computed
FROM orders o
LEFT JOIN order_lines l ON l.order_id = o.id
GROUP BY o.id, o.total
HAVING o.total <> COALESCE(SUM(l.quantity * l.unit_price), 0);

The left join keeps orders that have no lines. An inner join would hide exactly the broken ones. If this returns rows, check whether discounts or tax explain them before filing a defect.

Are there orphans?

SELECT l.order_id, COUNT(*) AS orphan_lines
FROM order_lines l
WHERE NOT EXISTS (SELECT 1 FROM orders o WHERE o.id = l.order_id)
GROUP BY l.order_id;

NOT EXISTS is safer than NOT IN. If the inner list contains a single null, NOT IN returns nothing at all, and a check that can never fail looks like a pass.

Are there duplicates?

SELECT LOWER(TRIM(email)) AS email, COUNT(*) AS copies
FROM customers
GROUP BY LOWER(TRIM(email))
HAVING COUNT(*) > 1;

Normalising first finds the duplicates a unique constraint missed: different case, trailing spaces.

What happens with null?

Null means unknown, and comparing with it gives neither true nor false. WHERE status <> 'cancelled' leaves out rows whose status is null. When a report's count disagrees with the table, look here first:

SELECT status, COUNT(*) FROM orders GROUP BY status;

Did the migration lose anything?

Counting rows is not enough. Compare counts, sums and the extremes, then the difference in both directions:

SELECT id, total FROM orders_old
EXCEPT
SELECT id, total FROM orders_new;

Run it the other way round too. EXCEPT removes duplicates, so check those separately. Oracle traditionally calls the operator MINUS.

The latest row per group

A common interview question, and a real need: each customer's most recent order.

SELECT id, customer_id, created_at
FROM (
  SELECT id, customer_id, created_at,
         ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY created_at DESC, id DESC) AS position
  FROM orders
) ranked
WHERE position = 1;

The id in the ordering breaks ties, so the answer is the same every time.

Safe habits

  • Use a read-only account. Ask for one; it protects you as well.
  • Write SELECT before any UPDATE, with the same condition, and check the count.
  • Limit results on large tables, and avoid heavy queries at busy times.
  • Never paste personal data into a bug report. Give identifiers, and describe the pattern.
  • Save useful queries beside the tests, with a sentence on what each proves.

Practise

Install SQLite, create the two tables, insert ten orders and break three of them: a wrong total, an orphan line, a duplicate. Find each with one query. Then write the same check as an automated test, which is what many roles mean by "database testing". See API tests in C# for testing the layer above.

Open remote QA roles

  1. QA Engineer (Manual Testing)Velixo · Poland
  2. Quality Analyst (healthcare programs)General Dynamics Information Technology · United States
  3. Marathi Audio Transcription QAWelocalize · India
  4. Bengali Audio Transcription QAWelocalize · India
  5. Quality Analyst (healthcare programs)Gdit · Any Location / Remote

More guides