Window Functions in SQL

Filtering

Filtering rows based on conditions

Course Sections
Objectives
    • 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

 SELECT *
FROM table_name
WHERE condition; 

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:

OperatorDescription
=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

 SELECT *
FROM table_name
WHERE column_name > value; 

Example

We have a table called students with the following data:

idnameagemajor
1John21Mathematics
2Lily22Biology
3Anil21Mathematics
4Ira20Computer Science
 SELECT *
FROM students
WHERE age > 21; 

This query will return all students who are older than 21.

Output

idnameagemajor
2Lily22Biology

Exercise

4.1 Run the following query to select all students who older than 21
idnameagemajor
1John21Mathematics
2Lily22Biology
3Anil21Mathematics
4Ira20Computer Science
Interactive
Greater than
4.2 Write a query to select students who are older than 20
idnameagemajor
1John21Mathematics
2Lily22Biology
3Anil21Mathematics
4Ira20Computer 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

 SELECT *
FROM table_name
WHERE column_name >= value; 

Example

 SELECT *
FROM students
WHERE age >= 21; 

This query will return all students who are 21 or older.

Output

idnameagemajor
1John21Mathematics
2Lily22Biology
3Anil21Mathematics

Exercise

4.3 Run the following query to select all students who are 21 or older
idnameagemajor
1John21Mathematics
2Lily22Biology
3Anil21Mathematics
4Ira20Computer Science
Interactive
Greater than or equal to
4.4 Write a query to select students who are 20 or older
idnameagemajor
1John21Mathematics
2Lily22Biology
3Anil21Mathematics
4Ira20Computer Science
Interactive
Greater than or equal to

Less than

Syntax

 SELECT *
FROM table_name
WHERE column_name < value; 

Example

 SELECT *
FROM students
WHERE age < 21; 

This query will return all students who are younger than 21.

Output

idnameagemajor
4Ira20Computer Science

Exercise

4.5 Run the following query to select all students who are younger than 21
idnameagemajor
1John21Mathematics
2Lily22Biology
3Anil21Mathematics
4Ira20Computer Science
Interactive
Less than
4.6 Write a query to select students who are younger than 22
idnameagemajor
1John21Mathematics
2Lily22Biology
3Anil21Mathematics
4Ira20Computer Science
Interactive
Less than

Less than or equal to

Syntax

 SELECT *
FROM table_name
WHERE column_name <= value; 

Example

 SELECT *
FROM students
WHERE age <= 21; 

This query will return all students who are 21 or younger.

Output

idnameagemajor
1John21Mathematics
3Anil21Mathematics
4Ira20Computer Science

Exercise

4.7 Run the following query to select all students who are 21 or younger
idnameagemajor
1John21Mathematics
2Lily22Biology
3Anil21Mathematics
4Ira20Computer Science
Interactive
Less than or equal to
4.8 Write a query to select students who are 22 or younger
idnameagemajor
1John21Mathematics
2Lily22Biology
3Anil21Mathematics
4Ira20Computer Science
Interactive
Less than or equal to

Equal to

Syntax

 SELECT *
FROM table_name
WHERE column_name = value; 

Example

 SELECT *
FROM students
WHERE age = 21; 

This query will return all students who are 21 years old.

Output

idnameagemajor
1John21Mathematics
3Anil21Mathematics

Exercise

4.9 Run the following query to select all students who are 21 years old
idnameagemajor
1John21Mathematics
2Lily22Biology
3Anil21Mathematics
4Ira20Computer Science
Interactive
Equal to
4.10 Write a query to select students who are 22 years old
idnameagemajor
1John21Mathematics
2Lily22Biology
3Anil21Mathematics
4Ira20Computer 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:

OperatorDescription
=Equal to
!=Not equal to
LIKEMatches a pattern
INMatches any value in a list
NOT INDoes not match any value in a list

Equal to

Syntax

 SELECT *
FROM table_name
WHERE column_name = 'value'; 

Example

 SELECT *
FROM students
WHERE major = 'Mathematics'; 

This query will return all students who are majoring in Mathematics.

Output

idnameagemajor
1John21Mathematics
3Anil21Mathematics

Exercise

4.11 Run the following query to select all students who are majoring in Mathematics
idnameagemajor
1John21Mathematics
2Lily22Biology
3Anil21Mathematics
4Ira20Computer Science
Interactive
Equal to
4.12 Write a query to select students who are majoring in Biology
idnameagemajor
1John21Mathematics
2Lily22Biology
3Anil21Mathematics
4Ira20Computer Science
Interactive
Equal to

Not equal to

Syntax

 SELECT *
FROM table_name
WHERE column_name != 'value'; 

Example

 SELECT *
FROM students
WHERE major != 'Mathematics'; 

This query will return all students who are not majoring in Mathematics.

Output

idnameagemajor
2Lily22Biology
4Ira20Computer Science

Exercise

4.13 Run the following query to select all students who are NOT majoring in Mathematics
idnameagemajor
1John21Mathematics
2Lily22Biology
3Anil21Mathematics
4Ira20Computer Science
Interactive
Not equal to
4.14 Write a query to select students who are NOT majoring in Biology
idnameagemajor
1John21Mathematics
2Lily22Biology
3Anil21Mathematics
4Ira20Computer 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

 SELECT *
FROM table_name
WHERE column_name LIKE 'pattern'; 

Example

 SELECT *
FROM students
WHERE major LIKE 'Math%'; 

This query will return all students who are majoring in a field that starts with “Math”.

Output

idnameagemajor
1John21Mathematics
3Anil21Mathematics

Exercise

4.15 Run the following query to select all students who are majoring in a field that starts with “Math”
idnameagemajor
1John21Mathematics
2Lily22Biology
3Anil21Mathematics
4Ira20Computer Science
Interactive
LIKE
4.16 Write a query to select students who are majoring in a field that starts with “Bio”
idnameagemajor
1John21Mathematics
2Lily22Biology
3Anil21Mathematics
4Ira20Computer 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

 SELECT *
FROM table_name
WHERE column_name IN ('value1', 'value2', ...); 
 SELECT *
FROM table_name
WHERE column_name NOT IN ('value1', 'value2', ...); 

Example

IN

 SELECT *
FROM students
WHERE major IN ('Mathematics', 'Biology'); 

This query will return all students who are majoring in Mathematics or Biology.

Output

idnameagemajor
1John21Mathematics
2Lily22Biology
3Anil21Mathematics

NOT IN

 SELECT *
FROM students
WHERE major NOT IN ('Mathematics', 'Biology'); 

This query will return all students who are not majoring in Mathematics or Biology.

Output

idnameagemajor
4Ira20Computer Science

Exercise

4.17 Run the following query to select all students who are majoring in Mathematics or Biology
idnameagemajor
1John21Mathematics
2Lily22Biology
3Anil21Mathematics
4Ira20Computer Science
Interactive
IN
4.18 Write a query to select students who are NOT majoring in Computer Science or Mathematics
idnameagemajor
1John21Mathematics
2Lily22Biology
3Anil21Mathematics
4Ira20Computer 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.