Understand how to filter rows based on conditions.
Learn how to use the WHERE function.
Practice using the WHERE function and conditions.
What is filtering?
Filtering is the process of selecting rows based on conditions. For example, you might want to select all rows where the value in a column is greater than 10.
The WHERE function is used to filter rows based on conditions. The WHERE function takes a condition as an argument and returns only the rows that satisfy the condition.
Syntax
The condition is a logical expression that evaluates to true or false. If the condition evaluates to true, the row is included in the result set; if the condition evaluates to false, the row is excluded from the result set.
Filtering numeric values
We’ll start by filtering rows based on numeric values. We’ll use the following operators to filter rows based on conditions:
Operator
Description
=
Equal to
!=
Not equal to
>
Greater than
>=
Greater than or equal to
<
Less than
<=
Less than or equal to
Greater than
We use the > operator to filter rows where the value in a column is greater than a specified value.
Syntax
Example
We have a table called students with the following data:
id
name
age
major
1
John
21
Mathematics
2
Lily
22
Biology
3
Anil
21
Mathematics
4
Ira
20
Computer Science
This query will return all students who are older than 21.
Output
id
name
age
major
2
Lily
22
Biology
Exercise
4.1 Run the following query to select all students who older than 21
id
name
age
major
1
John
21
Mathematics
2
Lily
22
Biology
3
Anil
21
Mathematics
4
Ira
20
Computer Science
Interactive
Greater than
4.2 Write a query to select students who are older than 20
id
name
age
major
1
John
21
Mathematics
2
Lily
22
Biology
3
Anil
21
Mathematics
4
Ira
20
Computer Science
Interactive
Greater than
Greater than or equal to
We use the >= operator to filter rows where the value in a column is greater than or equal to a specified value.
Syntax
Example
This query will return all students who are 21 or older.
Output
id
name
age
major
1
John
21
Mathematics
2
Lily
22
Biology
3
Anil
21
Mathematics
Exercise
4.3 Run the following query to select all students who are 21 or older
id
name
age
major
1
John
21
Mathematics
2
Lily
22
Biology
3
Anil
21
Mathematics
4
Ira
20
Computer Science
Interactive
Greater than or equal to
4.4 Write a query to select students who are 20 or older
id
name
age
major
1
John
21
Mathematics
2
Lily
22
Biology
3
Anil
21
Mathematics
4
Ira
20
Computer Science
Interactive
Greater than or equal to
Less than
Syntax
Example
This query will return all students who are younger than 21.
Output
id
name
age
major
4
Ira
20
Computer Science
Exercise
4.5 Run the following query to select all students who are younger than 21
id
name
age
major
1
John
21
Mathematics
2
Lily
22
Biology
3
Anil
21
Mathematics
4
Ira
20
Computer Science
Interactive
Less than
4.6 Write a query to select students who are younger than 22
id
name
age
major
1
John
21
Mathematics
2
Lily
22
Biology
3
Anil
21
Mathematics
4
Ira
20
Computer Science
Interactive
Less than
Less than or equal to
Syntax
Example
This query will return all students who are 21 or younger.
Output
id
name
age
major
1
John
21
Mathematics
3
Anil
21
Mathematics
4
Ira
20
Computer Science
Exercise
4.7 Run the following query to select all students who are 21 or younger
id
name
age
major
1
John
21
Mathematics
2
Lily
22
Biology
3
Anil
21
Mathematics
4
Ira
20
Computer Science
Interactive
Less than or equal to
4.8 Write a query to select students who are 22 or younger
id
name
age
major
1
John
21
Mathematics
2
Lily
22
Biology
3
Anil
21
Mathematics
4
Ira
20
Computer Science
Interactive
Less than or equal to
Equal to
Syntax
Example
This query will return all students who are 21 years old.
Output
id
name
age
major
1
John
21
Mathematics
3
Anil
21
Mathematics
Exercise
4.9 Run the following query to select all students who are 21 years old
id
name
age
major
1
John
21
Mathematics
2
Lily
22
Biology
3
Anil
21
Mathematics
4
Ira
20
Computer Science
Interactive
Equal to
4.10 Write a query to select students who are 22 years old
id
name
age
major
1
John
21
Mathematics
2
Lily
22
Biology
3
Anil
21
Mathematics
4
Ira
20
Computer Science
Interactive
Equal to
Filtering string values
We can filter rows based on string values. We’ll use the following operators to filter rows based on conditions:
Operator
Description
=
Equal to
!=
Not equal to
LIKE
Matches a pattern
IN
Matches any value in a list
NOT IN
Does not match any value in a list
Equal to
Syntax
Example
This query will return all students who are majoring in Mathematics.
Output
id
name
age
major
1
John
21
Mathematics
3
Anil
21
Mathematics
Exercise
4.11 Run the following query to select all students who are majoring in Mathematics
id
name
age
major
1
John
21
Mathematics
2
Lily
22
Biology
3
Anil
21
Mathematics
4
Ira
20
Computer Science
Interactive
Equal to
4.12 Write a query to select students who are majoring in Biology
id
name
age
major
1
John
21
Mathematics
2
Lily
22
Biology
3
Anil
21
Mathematics
4
Ira
20
Computer Science
Interactive
Equal to
Not equal to
Syntax
Example
This query will return all students who are not majoring in Mathematics.
Output
id
name
age
major
2
Lily
22
Biology
4
Ira
20
Computer Science
Exercise
4.13 Run the following query to select all students who are NOT majoring in Mathematics
id
name
age
major
1
John
21
Mathematics
2
Lily
22
Biology
3
Anil
21
Mathematics
4
Ira
20
Computer Science
Interactive
Not equal to
4.14 Write a query to select students who are NOT majoring in Biology
id
name
age
major
1
John
21
Mathematics
2
Lily
22
Biology
3
Anil
21
Mathematics
4
Ira
20
Computer Science
Interactive
Not equal to
LIKE
The LIKE operator is used to match a pattern. The % symbol is used as a wildcard character to match any sequence of characters.
Syntax
Example
This query will return all students who are majoring in a field that starts with “Math”.
Output
id
name
age
major
1
John
21
Mathematics
3
Anil
21
Mathematics
Exercise
4.15 Run the following query to select all students who are majoring in a field that starts with “Math”
id
name
age
major
1
John
21
Mathematics
2
Lily
22
Biology
3
Anil
21
Mathematics
4
Ira
20
Computer Science
Interactive
LIKE
4.16 Write a query to select students who are majoring in a field that starts with “Bio”
id
name
age
major
1
John
21
Mathematics
2
Lily
22
Biology
3
Anil
21
Mathematics
4
Ira
20
Computer Science
Interactive
LIKE
IN and NOT IN
The IN and NOT IN operators are used to match any value in a list of values.
Syntax
Example
IN
This query will return all students who are majoring in Mathematics or Biology.
Output
id
name
age
major
1
John
21
Mathematics
2
Lily
22
Biology
3
Anil
21
Mathematics
NOT IN
This query will return all students who are not majoring in Mathematics or Biology.
Output
id
name
age
major
4
Ira
20
Computer Science
Exercise
4.17 Run the following query to select all students who are majoring in Mathematics or Biology
id
name
age
major
1
John
21
Mathematics
2
Lily
22
Biology
3
Anil
21
Mathematics
4
Ira
20
Computer Science
Interactive
IN
4.18 Write a query to select students who are NOT majoring in Computer Science or Mathematics
id
name
age
major
1
John
21
Mathematics
2
Lily
22
Biology
3
Anil
21
Mathematics
4
Ira
20
Computer Science
Interactive
NOT IN
Summary
Filtering is the process of selecting rows based on conditions.
The WHERE function is used to filter rows based on conditions.
We can filter rows based on numeric values using the operators =, !=, >, >=, <, and <= .
We can filter rows based on string values using the operators =, !=, LIKE, IN, and NOT IN.