c2cedge
SQL & Databases · A — SQL Foundations

Filtering Rows with WHERE

WHERE narrows millions of rows down to the ones you care about. Operators, ranges, lists, patterns and NULLs all live here.

Test weight: Very highSkill: Precise filteringDifficulty: Easy–Medium

The WHERE clause keeps only the rows that satisfy a condition. Mastering its operators — comparisons, logical combinations, ranges, lists, patterns and the special handling of NULL — covers the majority of everyday SQL.

Comparison & logical operators

Compare with = <> < > <= >= and combine conditions with AND, OR and NOT. Use parentheses to make precedence explicit.

Filtering with conditions
SELECT name, salary FROM employees
WHERE  dept_id = 10 AND salary >= 60000;

SELECT name FROM employees
WHERE  dept_id = 10 OR dept_id = 20;

Ranges, lists and patterns

BETWEEN, IN and LIKE
WHERE salary BETWEEN 50000 AND 70000      -- inclusive range
WHERE dept_id IN (10, 30)                  -- membership list
WHERE name LIKE 'A%'                       -- starts with A
WHERE name LIKE '_a%'                       -- 2nd letter is a
Handling NULL
WHERE manager_id IS NULL          -- correct
WHERE manager_id = NULL           -- WRONG: never true
⚡ The edge
  • NULL means 'unknown', so it never equals anything — use IS NULL / IS NOT NULL, never = NULL.
  • LIKE patterns use % (any number of characters) and _ (exactly one). IN (...) is a clean shorthand for many OR conditions.
Worked example
Find employees in department 10 or 20 who earn more than 55000.
  1. Two department options: dept_id IN (10, 20).
  2. Plus a salary condition: salary > 55000.
  3. Combine with AND, parenthesising the IN for clarity.
Worked example
List employees whose name starts with the letter A.
  1. A 'starts with' pattern uses LIKE with a trailing %.
  2. The pattern 'A%' matches any name beginning with A.
  3. Note LIKE may be case-sensitive depending on the database/collation.
⚠ Watch out
  • = NULL is always unknown (never true) — use IS NULL.
  • Mixing AND/OR without parentheses can change the meaning — AND binds tighter than OR.
  • BETWEEN is inclusive of both endpoints.
Practice this — take a timed mock →
1,300+ questions, scored, with a weak-area report.
Know who's ready. Not who finished.
HomeLibraryPrivacyTerms