Filtering is the process of selecting rows based on conditions. For example, you may want to select all rows where the value in a column is greater than 10.
The WHERE function is used to do this. It 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
To create a condition, we need to use comparison operators. Here are some common comparison operators for numeric values:
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:
students
id
name
age
major
1
John
21
Mathematics
2
Lily
22
Biology
3
Anil
21
Mathematics
4
Ira
20
Computer Science
To select all students who are older than 21, we can use the following SQL query:
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
We can use the following SQL query to select 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
To select all students who are younger than 21, we can use the following SQL query:
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
We can use the following SQL query to select all students who are 21 or younger:
Example
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
We can use the following SQL query to select all students who are 21 years old:
Example
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
We can use the following SQL query to select all students who are majoring in Mathematics. We need to use quotes around the string value.
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
We can use the LIKE operator to select 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
We can use the IN operator to select all students who are majoring in Mathematics or Biology. We need to enclose the values in parentheses, separated by commas.
Output
id
name
age
major
1
John
21
Mathematics
2
Lily
22
Biology
3
Anil
21
Mathematics
NOT IN
We can use the NOT IN operator to select all students who are NOT majoring in Mathematics or Biology. We need to enclose the values in parentheses, separated by commas.
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.
Filtering Rows with Multiple Conditions
We can filter rows based on multiple conditions using logical operators such as AND , OR , and NOT .
AND
The AND operator is used to combine two or more conditions. It returns rows that satisfy all the conditions.
Syntax
Example
We can use the AND operator to select all students who are majoring in Mathematics and are older than 20.
Output
id
name
age
major
1
John
21
Mathematics
Exercise
4.19 Run the following query to select all students who are majoring in Mathematics and 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
AND
4.20 Write a query to select students who are majoring in Biology and their age is equal to 22.
id
name
age
major
1
John
21
Mathematics
2
Lily
22
Biology
3
Anil
21
Mathematics
4
Ira
20
Computer Science
Interactive
AND
OR
The OR operator is used to combine two or more conditions. It returns rows that satisfy at least one of the conditions.
Syntax
Example
We can use the OR operator to select all students who are majoring in Mathematics or are older than 20.
Output
id
name
age
major
1
John
21
Mathematics
2
Lily
22
Biology
3
Anil
21
Mathematics
Exercise
4.21 Run the following query to select all students who are majoring in Mathematics or 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
OR
4.22 Write a query to select students who are majoring in Biology or 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
OR
NOT
The NOT operator is used to negate a condition. It returns rows that do not satisfy the condition.
Syntax
Example
We can use the NOT operator to select all students who are NOT majoring in Mathematics.
Output
id
name
age
major
2
Lily
22
Biology
4
Ira
20
Computer Science
Exercise
4.23 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
4.24 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
Summary
We can filter rows based on multiple conditions using logical operators such as AND , OR , and NOT .
The AND operator returns rows that satisfy all the conditions.
The OR operator returns rows that satisfy at least one of the conditions.