SQL helps testers verify stored data and prepare test conditions. The examples below use common syntax, but details vary by database.

SELECT and WHERE

SELECT id, email, status FROM users WHERE status = 'active' AND created_at >= '2026-08-01' ORDER BY created_at DESC LIMIT 20;
=, <>, >, <Compare values.
IN ('new', 'paid')Match one of several values.
BETWEEN 10 AND 20Inclusive range.
LIKE 'qa%'Text pattern using % and _.
IS NULLMissing database value.
AND, OR, NOTCombine conditions. Use parentheses.

JOIN

SELECT o.id, o.status, u.email FROM orders AS o JOIN users AS u ON u.id = o.user_id WHERE o.id = 123;
  • INNER JOIN: only matching rows.
  • LEFT JOIN: every left row, with NULL when no right row matches.

GROUP BY and totals

SELECT status, COUNT(*) AS total, SUM(amount) AS value FROM orders GROUP BY status HAVING COUNT(*) > 5 ORDER BY total DESC;

WHERE filters rows before grouping. HAVING filters the grouped result.

Data-verification queries

Find duplicates

SELECT email, COUNT(*) AS total FROM users GROUP BY email HAVING COUNT(*) > 1;

Find missing relationships

SELECT o.id FROM orders AS o LEFT JOIN users AS u ON u.id = o.user_id WHERE u.id IS NULL;

Check NULL and empty values

SELECT id, phone FROM users WHERE phone IS NULL OR TRIM(phone) = '';

Verify latest records

SELECT id, event_type, created_at FROM audit_events WHERE user_id = 42 ORDER BY created_at DESC LIMIT 10;

Safety

  • Use a read-only account for verification.
  • Start with SELECT and a narrow WHERE.
  • Never run UPDATE or DELETE without confirming the environment and affected rows.
  • Do not copy personal or production data into tickets.
  • Know the timezone, collation and database engine.
  • Use parameterised queries in application code.
NULL is not an empty string: use IS NULL, not = NULL.

Useful links