WHERE Clause
WHERE Clause
Section titled βWHERE ClauseβWhat it means
Section titled βWhat it meansβWHERE filters which rows a query returns (or which rows an UPDATE/DELETE affects), based on a boolean condition evaluated per row. It supports comparison operators (=, <>, <, >=), logical combinators (AND, OR, NOT), pattern matching (LIKE), range checks (BETWEEN), and list membership (IN).
Examples
Section titled βExamplesβSELECT * FROM Products WHERE Price > 50;
SELECT * FROM Products WHERE Price BETWEEN 50 AND 100; -- inclusive on both ends
SELECT * FROM Customers WHERE Country = 'USA' AND IsActive = 1;
SELECT * FROM Customers WHERE Country = 'USA' OR Country = 'Canada';SELECT * FROM Customers WHERE Country IN ('USA', 'Canada', 'Mexico'); -- cleaner equivalent
SELECT * FROM Customers WHERE Name LIKE 'A%'; -- starts with 'A'SELECT * FROM Customers WHERE Email LIKE '%@gmail.com'; -- ends with '@gmail.com'
SELECT * FROM Orders WHERE NOT (Status = 'Cancelled');Common mistake
Section titled βCommon mistakeβMixing AND and OR without parentheses to group them clearly β SQL evaluates AND before OR by default (like standard operator precedence), which can silently produce a different condition than what was intended.
-- Intended: "USA customers who are active, OR any Canada customer"-- Actual (due to AND binding tighter than OR): "USA AND active" OR "just Canada" (any status!)SELECT * FROM CustomersWHERE Country = 'USA' AND IsActive = 1 OR Country = 'Canada';
-- Fix: use parentheses to make the intended grouping explicitSELECT * FROM CustomersWHERE (Country = 'USA' AND IsActive = 1) OR Country = 'Canada';Quick practice
Section titled βQuick practiceβ-
What does
WHERE Price BETWEEN 50 AND 100include β is 100 itself matched?Answer
Yes βBETWEENis inclusive on both ends, so rows withPriceexactly 50 or exactly 100 are included. -
Why should you use parentheses when mixing
ANDandORin the sameWHEREclause?Answer
Without them, SQL applies standard operator precedence (ANDbinds tighter thanOR), which can group conditions differently than you intended β parentheses make the grouping explicit and unambiguous. -
What does
WHERE Name LIKE 'A%'match?Answer
Any value whereNamestarts with the letter "A" β%is a wildcard matching any sequence of characters (including none).