Skip to main content
Module 1 · Lesson 3 of 43/57 in the course~14 min
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 / ConstructMeaningCritical note in SQL
=, <> (or !=)Equality / diversityStrings always in single quotes 'Milano'
<, <=, >, >=Numerical, date or lexicographic comparisonDates in ISO 'YYYY-MM-DD' format
AND, OR, NOTLogical connectorsAND binds tighter than OR: use brackets ()!
IS NULL / IS NOT NULLExplicit test for missing valuescol = NULL returns UNKNOWN, never TRUE

2. Progressive & Annotated Examples

SQL
-- 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;
SQL
-- Esempio 2: confronto su date ISO e testo tra apici singoli
SELECT title, year, genre
FROM books
WHERE genre = 'Romanzo' AND year >= 1980;
SQL
-- 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;
SQL
-- 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)

SQL Quiz#sql.m1.l3.e5
Ready

How many rows does this query return knowing that Andrea Costa has department_id NULL?

SQL
SELECT first_name FROM employees WHERE department_id = NULL;
Answer options
SQL Quiz#sql.m1.l3.e6
Ready

Anna Bianchi is in department 1 with salary 42000. Is it returned by this query?

SQL
SELECT first_name FROM employees
WHERE department_id = 1 OR department_id = 2 AND salary >= 50000;
Answer options

5. Progressive Practical Laboratory

Warm-Up Stage

Warm-Up: Simple filter on key

Extract only members of a specific department using WHERE and equality.

Exercise#sql.m1.l3.e1
Attempts: 0Loading…

Shows first_name, last_name, and salary of employees in department 1 (department_id = 1).

Loading editor…
Show hint

Add WHERE department_id = 1 after FROM employees.

Solution available after 3 attempts

Practice Stage

Practice: Combine department and salary threshold

Join a foreign key condition and a numeric comparison with AND.

Exercise#sql.m1.l3.e2
Attempts: 0Loading…

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.

Loading editor…
Show hint

Use salary >= 45000 AND department_id = 1 in the WHERE clause.

Solution available after 3 attempts

Practice Stage

Practice: Filter on strings and dates in the Library

Combines a text filter between single quotes and a time threshold on the year.

Exercise#sql.m1.l3.e3
Attempts: 0Loading…

From the books table it shows the title and year of books with the 'Novel' genre published from 1980 onwards (year >= 1980).

Loading editor…
Show hint

Add AND year >= 1980 to the genre filter.

Solution available after 3 attempts

Debug Stage

Debug: Fix precedence between OR and AND

Use parentheses to prevent AND from only capturing the second branch of the OR.

Exercise#sql.m1.l3.e4
Attempts: 0Loading…

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.

Loading editor…
Show hint

Enclose department_id = 1 OR department_id = 4 in parentheses.

Solution available after 3 attempts