What basic SQL queries should QA know?
QA needs basic SQL: SELECT with WHERE, ORDER BY, LIMIT, DISTINCT, and aggregates with GROUP BY and HAVING. A row condition goes in WHERE and an aggregate condition in HAVING. NULL means an unknown value: comparing with it yields UNKNOWN, so <> and NOT IN silently drop rows, and the check must use IS NULL.
QA Fundamentals · SQL for QA
The report shows 118 transactions and the table has 121. Where did three go?
A tester writes a query "all transactions except failed ones": WHERE status <> 'failed'. The table has 121 rows, two failed, so 119 are expected. The query returns 118. One row vanished with no error and no warning. Queries like this are reassuring, because they run successfully while the result is wrong. This lesson is about the basic SQL set QA needs, the order in which the database executes a query and NULL, the source of most silent mistakes.
How it works and why. SQL is declarative: you describe which result you want, not how to compute it. Still, inside, a query is logically executed in a fixed order, and knowing that order explains almost every "why does this not work". First FROM defines the table, then WHERE filters individual rows, GROUP BY collects them into groups, HAVING filters groups, SELECT computes columns and aliases, ORDER BY sorts, LIMIT cuts. (Physically the optimiser may act differently, but the result must match this order.)
The logical order of executing a query
Simple rules follow. A condition on a row lives in WHERE, a condition on an aggregate (SUM(amount) > 10000) in HAVING. An alias created in SELECT does not exist in WHERE yet. Without ORDER BY the row order is not guaranteed, so "the first" or "the last" row and LIMIT without sorting give an unpredictable result. QA's basic set: SELECT with WHERE (comparison, IN, BETWEEN, LIKE, IS NULL), ORDER BY, LIMIT, DISTINCT, aggregates (COUNT, SUM, AVG, MIN, MAX) and GROUP BY with HAVING.
Why the row vanished: NULL. NULL means "the value is unknown", not "an empty string" or zero. A comparison with NULL gives not "false" but a third value, UNKNOWN, and WHERE keeps only rows where the condition is true. So status <> 'failed' is not true for a row with status = NULL and the row is dropped. In the same way status = NULL never fires: you must use IS NULL. A second consequence: COUNT(*) counts rows, while COUNT(status) counts only those where the value is not NULL.
Example: from a wrong number to a data defect. The initial query and its result:
SELECT COUNT(*)
FROM transactions
WHERE status <> 'failed';
-- 118
Instead of guessing the cause, look at what is in the table. A diagnostic query compares the counts:
SELECT COUNT(*) AS total,
COUNT(status) AS with_status,
SUM(CASE WHEN status = 'failed' THEN 1 ELSE 0 END) AS failed
FROM transactions;
-- illustration: total 121, with_status 120, failed 2
The gap between 121 and 120 says that one row has no status, and 118 is 121 minus 2 failed minus 1 row with NULL. The fixed query and a separate query for the defect itself:
-- everything except failed, including an empty status: 119
SELECT COUNT(*)
FROM transactions
WHERE status <> 'failed' OR status IS NULL;
-- the data defect itself: rows with no status
SELECT id, user_id, created_at
FROM transactions
WHERE status IS NULL;
Two conclusions here. First: the query is fixed and now counts what was intended. Second, and more important: a row with no status is itself a finding. If the status is mandatory by the requirement, there is a defect in the data (its cause is sought in the code that creates the record), and it must be written up as a bug. This is how SQL in the hands of QA works as a way to learn about the system, not only as a way to count.
Another sample of what QA writes most often: a monthly report with grouping and a filter on groups.
SELECT status, currency, COUNT(*) AS cnt, SUM(amount) AS total
FROM transactions
WHERE created_at >= '2026-10-01' AND created_at < '2026-11-01'
GROUP BY status, currency
HAVING SUM(amount) > 10000
ORDER BY total DESC
LIMIT 10;
The date range is written as "from the start of October inclusive to the start of November exclusive". The variant BETWEEN '2026-10-01' AND '2026-10-31' on a column with time drops everything that happened on 31 October after midnight, because the bound "2026-10-31" means 00:00. Money amounts must be stored in fixed-precision types (DECIMAL/NUMERIC), not FLOAT: otherwise SUM accumulates errors.
The same search for non-failed rows, written two ways
status <> 'failed'
Rows with NULL in the status silently vanish. The result 118 looks plausible and alarms nobody.
<> 'failed' OR IS NULL
The condition handles NULL explicitly. The result is 119, and the row with no status is found separately as a data defect.
Limits and alternatives. SQL differs between databases. Limiting the number of rows is LIMIT in PostgreSQL and MySQL, TOP in SQL Server, FETCH FIRST in the standard. ILIKE exists only in PostgreSQL. Case sensitivity in string comparison depends on the collation: in MySQL it is insensitive by default, in PostgreSQL sensitive. So first find out which database the project uses. On large tables SELECT * without WHERE can load a shared environment: add LIMIT and a filter, and for slow queries look at EXPLAIN. Some checks can be done without the database: through the API or an admin panel. The database is needed when you must see what the user is not shown: side effects, service states, the state after a failure.
Common mistakes.
= NULLor<> NULLinstead ofIS NULL/IS NOT NULL: the condition never becomes true.- A
<>orNOT INfilter on a column where NULL is possible: rows vanish without a warning. - An aggregate in
WHERE: SQL rejects the query. For a condition on a group there isHAVING. LIMITwithoutORDER BY: the set of "first" rows is different every time.BETWEENfor dates with time: the last day of the range is lost. A half-open interval is better.SELECT *without limits on a large table of a shared environment: slow and harmful for others.- Money in
FLOAT: differences of cents in sums and comparisons.
Check yourself
The orders table has 50 rows. SELECT COUNT(discount) FROM orders returns 32. What does it mean and how does it differ from COUNT(*)?
Answer
COUNT(discount) counts only rows where discount is not NULL, so 18 orders have no discount set (NULL). COUNT(*) would return 50, because it counts all rows. The difference is useful for checking: whether empty fields are expected or are a data defect.
Why does SELECT user_id, COUNT(*) FROM payments WHERE COUNT(*) > 1 GROUP BY user_id fail and how do you fix it?
Answer
WHERE runs before grouping, when there are no aggregates yet. A condition on an aggregate moves to HAVING: GROUP BY user_id HAVING COUNT(*) > 1. The general rule: a condition on a row goes in WHERE, a condition on a group in HAVING.
<> and NOT IN silently drop rows, and COUNT(*) and COUNT(column) give different numbers. I write date ranges half-open, check money in fixed-precision types and clarify the database dialect at the start.