Introduction to SQL

Filtering Rows

SQL is a language that is used to query and manage data.

Course Sections

What is filtering?

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

 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

To create a condition, we need to use comparison operators. Here are some common comparison operators for numeric values:

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:

students
idnameagemajor
1John21Mathematics
2Lily22Biology
3Anil21Mathematics
4Ira20Computer Science

To select all students who are older than 21, we can use the following SQL query:

 SELECT *
FROM students
WHERE age > 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

We can use the following SQL query to select all students who are 21 or older:

 SELECT *
FROM students
WHERE age >= 21; 

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

To select all students who are younger than 21, we can use the following SQL query:

 SELECT *
FROM students
WHERE age < 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; 

We can use the following SQL query to select all students who are 21 or younger:

Example

 SELECT *
FROM students
WHERE age <= 21; 

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; 

We can use the following SQL query to select all students who are 21 years old:

Example

 SELECT *
FROM students
WHERE age = 21; 

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

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.

 SELECT *
FROM students
WHERE major = '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

We can use the LIKE operator to select all students who are majoring in a field that starts with “Math”.

 SELECT *
FROM students
WHERE major LIKE '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

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.

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

Output

idnameagemajor
1John21Mathematics
2Lily22Biology
3Anil21Mathematics

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.

 SELECT *
FROM students
WHERE major NOT IN ('Mathematics', '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.

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

 SELECT *
FROM table_name
WHERE condition1 AND condition2; 

Example

We can use the AND operator to select all students who are majoring in Mathematics and are older than 20.

 SELECT *
FROM students
WHERE major = 'Mathematics' AND age > 20; 

Output

idnameagemajor
1John21Mathematics

Exercise

4.19 Run the following query to select all students who are majoring in Mathematics and are older than 20.
idnameagemajor
1John21Mathematics
2Lily22Biology
3Anil21Mathematics
4Ira20Computer Science
Interactive
AND
4.20 Write a query to select students who are majoring in Biology and their age is equal to 22.
idnameagemajor
1John21Mathematics
2Lily22Biology
3Anil21Mathematics
4Ira20Computer 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

 SELECT *
FROM table_name
WHERE condition1 OR condition2; 

Example

We can use the OR operator to select all students who are majoring in Mathematics or are older than 20.

 SELECT *
FROM students
WHERE major = 'Mathematics' OR age > 20; 

Output

idnameagemajor
1John21Mathematics
2Lily22Biology
3Anil21Mathematics

Exercise

4.21 Run the following query to select all students who are majoring in Mathematics or are older than 20
idnameagemajor
1John21Mathematics
2Lily22Biology
3Anil21Mathematics
4Ira20Computer Science
Interactive
OR
4.22 Write a query to select students who are majoring in Biology or are younger than 22
idnameagemajor
1John21Mathematics
2Lily22Biology
3Anil21Mathematics
4Ira20Computer Science
Interactive
OR

NOT

The NOT operator is used to negate a condition. It returns rows that do not satisfy the condition.

Syntax

 SELECT *
FROM table_name
WHERE NOT condition; 

Example

We can use the NOT operator to select all students who are NOT majoring in Mathematics.

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

Output

idnameagemajor
2Lily22Biology
4Ira20Computer Science

Exercise

4.23 Run the following query to select all students who are NOT majoring in Mathematics
idnameagemajor
1John21Mathematics
2Lily22Biology
3Anil21Mathematics
4Ira20Computer Science
Interactive
NOT
4.24 Write a query to select students who are NOT majoring in Biology
idnameagemajor
1John21Mathematics
2Lily22Biology
3Anil21Mathematics
4Ira20Computer 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.
  • The NOT operator is used to negate a condition.