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

SQL › SELECT and filtering

Which operators are used in WHERE?

WHERE uses comparisons, AND, OR, NOT, IN, BETWEEN, LIKE and IS NULL, and only rows where the condition is true reach the result. Precedence is NOT first, then AND, then OR, so mixed AND and OR are put in parentheses. A precedence mistake raises no error; it shows only on data where every branch of the condition has its own row.

SQL · SELECT and WHERE

A query for failed deposits and withdrawals also returned a successful deposit. It looks like a database bug, but it is not.

A report must show failed transactions of two types: deposit and withdrawal. The query is written the way it sounds in words and it returns rows with status completed. The database is not wrong: it did exactly what was written. This lesson is about the WHERE operators and one precedence rule that separates "what I meant" from "what I wrote".

How it works and why. WHERE is a predicate checked for every row. A row reaches the result only if the predicate is true. False and unknown (when a comparison involves NULL) rows are dropped the same way. A predicate is built from these groups of operators:

GroupOperatorsNuance
Comparison= <> != > < >= <=<> is standard, != is supported by the main databases. With NULL they give unknown, not true or false.
LogicalNOT AND ORIn priority order: NOT, then AND, then OR.
List and rangeIN BETWEENIN replaces a chain of OR, BETWEEN includes both bounds. A separate lesson.
PatternLIKE ILIKEILIKE is not standard, it is a PostgreSQL extension. A separate lesson.
NULLIS NULL IS NOT NULL IS DISTINCT FROMThe only correct way to test for an empty value.

Complex conditions are assembled from AND, OR and NOT, and the language has precedence: first NOT, then AND, then OR. It is the same logic as in arithmetic: multiplication before addition. In the phrase "deposit or withdrawal, and failed" the words do not show what "failed" refers to. SQL parses without hints: AND binds tighter, so status = 'failed' attaches only to withdrawal.

Example: an extra row in the report. The transactions table has five rows: failed and successful deposits and withdrawals plus one failed transfer. The goal is to get rows 1 and 3.

-- intended: failed deposit or withdrawal
SELECT id, type, status
FROM transactions
WHERE type = 'deposit' OR type = 'withdrawal' AND status = 'failed';
-- is read as: deposit OR (withdrawal AND failed)

The same query without and with parentheses

without parentheses
idtypestatus
1depositfailed
2depositcompleted extra
3withdrawalfailed
with parentheses
idtypestatus
1depositfailed
3withdrawalfailed
Figure 1. Without parentheses every deposit reaches the result regardless of status, so row 2 is extra.

The query produced no error and returned "plausible" data, and the problem can be found only by comparing with the expected result. The fix is parentheses or IN, which removes the ambiguity itself:

SELECT id, type, status
FROM transactions
WHERE (type = 'deposit' OR type = 'withdrawal') AND status = 'failed';

-- the same, shorter, with no parentheses needed
SELECT id, type, status
FROM transactions
WHERE type IN ('deposit', 'withdrawal') AND status = 'failed';

How to check that a condition does what was intended. Build a minimal data set with one row for each combination: a type from the condition and one outside it, a status from the condition and one outside it. Here that is 2 × 2 plus a foreign type. A query with the right condition returns exactly rows 1 and 3, and an error in the logic gives a different set. Compare ids, not only the count: a condition with status <> 'failed' also returns 2 rows. You do not need a big database: you need one where each branch of the condition has "its own" row.

Limits and alternatives. Parentheses cost nothing, so in mixed AND and OR you always add them, even when the precedence matches the intent: the next reader will not have to guess. NOT also binds only to the nearest predicate: NOT a = 1 AND b = 2 means (NOT a = 1) AND b = 2. For several values of one column IN is shorter and safer than a chain of OR. A separate trap is string comparison: in PostgreSQL 'Failed' = 'failed' is false, while in MySQL with a typical collation (for example utf8mb4_0900_ai_ci in MySQL 8) such a comparison is case-insensitive, so the same test can behave differently across databases. Comparing a number with a string also depends on the database: PostgreSQL casts the literal '5' to a number, but comparing a text column with a number gives a type error. Conditions with NULL are covered separately in the IS NULL lesson.

Common mistakes.

  • OR together with AND without parentheses: the condition reads differently from how it sounds.
  • A condition on a date or string checked only on "good" values: the branches of the condition did not get their own rows.
  • A chain of OR on one column instead of IN.
  • Comparing with NULL through = or <>.
  • Moving a query between databases without checking case and types in comparisons.

Check yourself

How will SQL read WHERE a = 1 OR b = 2 AND c = 3?

Answer

As a = 1 OR (b = 2 AND c = 3): AND has higher precedence than OR. If you meant (a = 1 OR b = 2) AND c = 3, you need parentheses.

What minimal test data is needed to find an error in the condition (type IN (...)) AND status = 'failed'?

Answer

A row for each combination: a type from the condition and one outside it, status failed and not failed. Then the right condition returns exactly the intended rows, and a missing parenthesis or AND gives an extra or a missing row, visible in the set of ids.

Why does a query with a precedence mistake not fail with an error?

Answer

It is syntactically valid and has clear semantics, just not the ones the author meant. The database does not know the intent, so the error shows only by comparing the result with the expectation.

In an interview. Junior: In WHERE I use comparisons, the logical AND, OR, NOT, and also IN, BETWEEN, LIKE and IS NULL. The main trap is precedence: AND runs before OR, so when I combine them I add parentheses. Middle/Senior: WHERE keeps rows where the predicate is true, and false and unknown ones are dropped. A query with a precedence mistake is valid and plausible, so it is invisible without a check on data where each branch of the condition has its own row. For several values I choose IN, I always add parentheses and I remember the differences in case and types between databases.

More articles in this chapter