Module lessons (3/4)
Filtering with WHERE
A real query rarely wants the entire table: WHERE applies a boolean predicate to every single row immediately after FROM and only lets through those for which the expression is TRUE.
1. Mental Model: Three-Value Logic and Precedence
| Operator / Construct | Meaning | Critical note in SQL |
|---|---|---|
=, <> (or !=) | Equality / diversity | Strings always in single quotes 'Milano' |
<, <=, >, >= | Numerical, date or lexicographic comparison | Dates in ISO 'YYYY-MM-DD' format |
AND, OR, NOT | Logical connectors | AND binds tighter than OR: use brackets ()! |
IS NULL / IS NOT NULL | Explicit test for missing values | col = NULL returns UNKNOWN, never TRUE |
2. Progressive & Annotated Examples
-- Esempio 1: confronto numerico ed esattamente uguale su chiave esterna
SELECT first_name, last_name, salary
FROM employees
WHERE department_id = 1 AND salary >= 40000;-- Esempio 2: confronto su date ISO e testo tra apici singoli
SELECT title, year, genre
FROM books
WHERE genre = 'Romanzo' AND year >= 1980;-- Esempio 3: righe con valore mancante (Andrea Costa non ha reparto)
SELECT first_name, last_name, department_id
FROM employees
WHERE department_id IS NULL;-- Esempio 4: parentesi essenziali per non falsare OR + AND
SELECT first_name, last_name, department_id, salary
FROM employees
WHERE (department_id = 1 OR department_id = 4)
AND salary >= 45000;3. Interactive Laboratory: When WHERE Comes into Play
Advance the pipeline step by step and see how WHERE discards individual rows already before grouping or sorting:
4. Diagnostic Verification (Quiz)
How many rows does this query return knowing that Andrea Costa has department_id NULL?
SELECT first_name FROM employees WHERE department_id = NULL;Anna Bianchi is in department 1 with salary 42000. Is it returned by this query?
SELECT first_name FROM employees
WHERE department_id = 1 OR department_id = 2 AND salary >= 50000;5. Progressive Practical Laboratory
Warm-Up: Simple filter on key
Extract only members of a specific department using WHERE and equality.
Shows first_name, last_name, and salary of employees in department 1 (department_id = 1).
Show hint
Add WHERE department_id = 1 after FROM employees.
Solution available after 3 attempts
Practice: Combine department and salary threshold
Join a foreign key condition and a numeric comparison with AND.
Extract the name (first_name), surname (last_name) and salary of the employees with a salary of at least 45,000 who work in department 1.
Show hint
Use salary >= 45000 AND department_id = 1 in the WHERE clause.
Solution available after 3 attempts
Practice: Filter on strings and dates in the Library
Combines a text filter between single quotes and a time threshold on the year.
From the books table it shows the title and year of books with the 'Novel' genre published from 1980 onwards (year >= 1980).
Show hint
Add AND year >= 1980 to the genre filter.
Solution available after 3 attempts
Debug: Fix precedence between OR and AND
Use parentheses to prevent AND from only capturing the second branch of the OR.
The starter query should find those who work in departments 1 or 4 AND earn at least 45,000, but also let through those who earn less in department 1! Add the correct brackets.
Show hint
Enclose department_id = 1 OR department_id = 4 in parentheses.
Solution available after 3 attempts