SelectFromData
AnalyticsLesson 3 of 9 · 25 min

Filtering Deeper: AND, OR, IN, BETWEEN, LIKE, NULL

Real questions rarely have a single condition. "Engineers in Antwerp earning over €60k" is three conditions at once. This lesson is the toolkit for combining them, plus the one value that breaks every beginner's filter: NULL.

In one line

Combine conditions with AND / OR, match a list with IN, a range with BETWEEN, a text pattern with LIKE, and always test missing values with IS NULL, never = NULL.

AND and OR

AND requires every condition to be true; OR requires at least one. Run it: engineers earning over €60k.

Two conditions joined with AND
Editable · runs in your browser

Watch out

When you mix AND and OR, use parentheses. WHERE a = 1 AND b = 2 OR c = 3 does not mean what it looks like. AND binds tighter than OR, so it reads as (a=1 AND b=2) OR c=3. Write the parentheses you mean.

IN — match any value in a list

IN is a clean shorthand for several ORs. WHERE city IN ('Antwerp', 'Ghent') beats WHERE city = 'Antwerp' OR city = 'Ghent'. Run it:

IN matches any value in the list
Editable · runs in your browser

BETWEEN — match a range

BETWEEN is inclusive on both ends: salary BETWEEN 48000 AND 62000 includes both 48000 and 62000. Rows below 48000 or above 62000 drop out; the endpoints themselves stay:

namesalary
Ada62000
Bo48000
Cy71000
Di52000
Eli45000
WHERE salary BETWEEN 48000 AND 62000
namesalary
Ada62000
Bo48000
Di52000
Inclusive range: 48000 and 62000 pass; Cy (71000) and Eli (45000) fall outside and are dropped.

Try it now: write the range filter the visual showed, name and salary of employees earning between 48000 and 62000 (inclusive).

Your turn · Exercise

Return the name and salary of employees whose salary is between 48000 and 62000 (inclusive).

The data

employees

5 rows returned

namesalary
Ada62,000
Bo48,000
Cy71,000
Di52,000
Eli45,000
Runs in your browser

Run it:

BETWEEN includes both endpoints
Editable · runs in your browser

LIKE — match a text pattern

LIKE matches patterns with two wildcards: % (any number of characters) and _ (exactly one). WHERE name LIKE 'A%' finds names starting with A. Run it, then try '%a%' for names containing a lowercase a:

LIKE with % wildcard — names starting with A
Editable · runs in your browser

NULL — the missing value

NULL means "unknown / no value." It is not zero and not an empty string. The trap: WHERE bonus = NULL returns nothing, ever, because nothing "equals" an unknown. You must use IS NULL / IS NOT NULL. Run it to find employees with no recorded bonus:

IS NULL finds missing values; = NULL would return nothing
Editable · runs in your browser

Watch out

= NULL and <> NULL always return zero rows, silently. This is the #1 source of "my filter returns nothing and I can't see why." Missing values are tested with IS NULL and IS NOT NULL, never =.

Your turn

Two conditions at once. Return the name and salary of engineers earning more than 60000.

Your turn · Exercise

Return the name and salary of engineers (department Engineering) who earn more than 60000.

The data

employees

5 rows returned

namedepartmentsalarycity
AdaEngineering62,000Antwerp
BoSales48,000Ghent
CyEngineering71,000Antwerp
DiSales52,000Bruges
EliEngineering45,000Ghent
Runs in your browser

Next: instead of listing rows, you'll start summarising them. Counting, summing, and averaging with GROUP BY.