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;
copied = true)" @mouseleave.debounce.1000ms="copied && (copied = false)" @keydown.enter.debounce.1000ms="copied && (copied = false)" @keydown.space.debounce.1000ms="copied && (copied = false)" @touchstart.debounce.1000ms="copied && (copied = false)" class="group flex select-none items-center justify-between gap-2 rounded p-2 hover:bg-3 hover:text-brand focus-visible:outline-none focus-visible:ring-1 motion-safe:transition-colors absolute right-0 top-0 text-2">
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
SELECT *
FROM table_name
WHERE column_name > value ;
copied = true)" @mouseleave.debounce.1000ms="copied && (copied = false)" @keydown.enter.debounce.1000ms="copied && (copied = false)" @keydown.space.debounce.1000ms="copied && (copied = false)" @touchstart.debounce.1000ms="copied && (copied = false)" class="group flex select-none items-center justify-between gap-2 rounded p-2 hover:bg-3 hover:text-brand focus-visible:outline-none focus-visible:ring-1 motion-safe:transition-colors absolute right-0 top-0 text-2">
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
SELECT *
FROM students
WHERE age > 21 ;
copied = true)" @mouseleave.debounce.1000ms="copied && (copied = false)" @keydown.enter.debounce.1000ms="copied && (copied = false)" @keydown.space.debounce.1000ms="copied && (copied = false)" @touchstart.debounce.1000ms="copied && (copied = false)" class="group flex select-none items-center justify-between gap-2 rounded p-2 hover:bg-3 hover:text-brand focus-visible:outline-none focus-visible:ring-1 motion-safe:transition-colors absolute right-0 top-0 text-2">
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
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
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 ;
copied = true)" @mouseleave.debounce.1000ms="copied && (copied = false)" @keydown.enter.debounce.1000ms="copied && (copied = false)" @keydown.space.debounce.1000ms="copied && (copied = false)" @touchstart.debounce.1000ms="copied && (copied = false)" class="group flex select-none items-center justify-between gap-2 rounded p-2 hover:bg-3 hover:text-brand focus-visible:outline-none focus-visible:ring-1 motion-safe:transition-colors absolute right-0 top-0 text-2">
Example
SELECT *
FROM students
WHERE age >= 21 ;
copied = true)" @mouseleave.debounce.1000ms="copied && (copied = false)" @keydown.enter.debounce.1000ms="copied && (copied = false)" @keydown.space.debounce.1000ms="copied && (copied = false)" @touchstart.debounce.1000ms="copied && (copied = false)" class="group flex select-none items-center justify-between gap-2 rounded p-2 hover:bg-3 hover:text-brand focus-visible:outline-none focus-visible:ring-1 motion-safe:transition-colors absolute right-0 top-0 text-2">
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
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
Less than
Syntax
SELECT *
FROM table_name
WHERE column_name < value ;
copied = true)" @mouseleave.debounce.1000ms="copied && (copied = false)" @keydown.enter.debounce.1000ms="copied && (copied = false)" @keydown.space.debounce.1000ms="copied && (copied = false)" @touchstart.debounce.1000ms="copied && (copied = false)" class="group flex select-none items-center justify-between gap-2 rounded p-2 hover:bg-3 hover:text-brand focus-visible:outline-none focus-visible:ring-1 motion-safe:transition-colors absolute right-0 top-0 text-2">
Example
SELECT *
FROM students
WHERE age < 21 ;
copied = true)" @mouseleave.debounce.1000ms="copied && (copied = false)" @keydown.enter.debounce.1000ms="copied && (copied = false)" @keydown.space.debounce.1000ms="copied && (copied = false)" @touchstart.debounce.1000ms="copied && (copied = false)" class="group flex select-none items-center justify-between gap-2 rounded p-2 hover:bg-3 hover:text-brand focus-visible:outline-none focus-visible:ring-1 motion-safe:transition-colors absolute right-0 top-0 text-2">
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
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
Less than or equal to
Syntax
SELECT *
FROM table_name
WHERE column_name <= value ;
copied = true)" @mouseleave.debounce.1000ms="copied && (copied = false)" @keydown.enter.debounce.1000ms="copied && (copied = false)" @keydown.space.debounce.1000ms="copied && (copied = false)" @touchstart.debounce.1000ms="copied && (copied = false)" class="group flex select-none items-center justify-between gap-2 rounded p-2 hover:bg-3 hover:text-brand focus-visible:outline-none focus-visible:ring-1 motion-safe:transition-colors absolute right-0 top-0 text-2">
Example
SELECT *
FROM students
WHERE age <= 21 ;
copied = true)" @mouseleave.debounce.1000ms="copied && (copied = false)" @keydown.enter.debounce.1000ms="copied && (copied = false)" @keydown.space.debounce.1000ms="copied && (copied = false)" @touchstart.debounce.1000ms="copied && (copied = false)" class="group flex select-none items-center justify-between gap-2 rounded p-2 hover:bg-3 hover:text-brand focus-visible:outline-none focus-visible:ring-1 motion-safe:transition-colors absolute right-0 top-0 text-2">
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
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
Equal to
Syntax
SELECT *
FROM table_name
WHERE column_name = value ;
copied = true)" @mouseleave.debounce.1000ms="copied && (copied = false)" @keydown.enter.debounce.1000ms="copied && (copied = false)" @keydown.space.debounce.1000ms="copied && (copied = false)" @touchstart.debounce.1000ms="copied && (copied = false)" class="group flex select-none items-center justify-between gap-2 rounded p-2 hover:bg-3 hover:text-brand focus-visible:outline-none focus-visible:ring-1 motion-safe:transition-colors absolute right-0 top-0 text-2">
Example
SELECT *
FROM students
WHERE age = 21 ;
copied = true)" @mouseleave.debounce.1000ms="copied && (copied = false)" @keydown.enter.debounce.1000ms="copied && (copied = false)" @keydown.space.debounce.1000ms="copied && (copied = false)" @touchstart.debounce.1000ms="copied && (copied = false)" class="group flex select-none items-center justify-between gap-2 rounded p-2 hover:bg-3 hover:text-brand focus-visible:outline-none focus-visible:ring-1 motion-safe:transition-colors absolute right-0 top-0 text-2">
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
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
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
SELECT *
FROM table_name
WHERE column_name = 'value' ;
copied = true)" @mouseleave.debounce.1000ms="copied && (copied = false)" @keydown.enter.debounce.1000ms="copied && (copied = false)" @keydown.space.debounce.1000ms="copied && (copied = false)" @touchstart.debounce.1000ms="copied && (copied = false)" class="group flex select-none items-center justify-between gap-2 rounded p-2 hover:bg-3 hover:text-brand focus-visible:outline-none focus-visible:ring-1 motion-safe:transition-colors absolute right-0 top-0 text-2">
Example
SELECT *
FROM students
WHERE major = 'Mathematics' ;
copied = true)" @mouseleave.debounce.1000ms="copied && (copied = false)" @keydown.enter.debounce.1000ms="copied && (copied = false)" @keydown.space.debounce.1000ms="copied && (copied = false)" @touchstart.debounce.1000ms="copied && (copied = false)" class="group flex select-none items-center justify-between gap-2 rounded p-2 hover:bg-3 hover:text-brand focus-visible:outline-none focus-visible:ring-1 motion-safe:transition-colors absolute right-0 top-0 text-2">
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
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
Not equal to
Syntax
SELECT *
FROM table_name
WHERE column_name != 'value' ;
copied = true)" @mouseleave.debounce.1000ms="copied && (copied = false)" @keydown.enter.debounce.1000ms="copied && (copied = false)" @keydown.space.debounce.1000ms="copied && (copied = false)" @touchstart.debounce.1000ms="copied && (copied = false)" class="group flex select-none items-center justify-between gap-2 rounded p-2 hover:bg-3 hover:text-brand focus-visible:outline-none focus-visible:ring-1 motion-safe:transition-colors absolute right-0 top-0 text-2">
Example
SELECT *
FROM students
WHERE major != 'Mathematics' ;
copied = true)" @mouseleave.debounce.1000ms="copied && (copied = false)" @keydown.enter.debounce.1000ms="copied && (copied = false)" @keydown.space.debounce.1000ms="copied && (copied = false)" @touchstart.debounce.1000ms="copied && (copied = false)" class="group flex select-none items-center justify-between gap-2 rounded p-2 hover:bg-3 hover:text-brand focus-visible:outline-none focus-visible:ring-1 motion-safe:transition-colors absolute right-0 top-0 text-2">
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
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
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' ;
copied = true)" @mouseleave.debounce.1000ms="copied && (copied = false)" @keydown.enter.debounce.1000ms="copied && (copied = false)" @keydown.space.debounce.1000ms="copied && (copied = false)" @touchstart.debounce.1000ms="copied && (copied = false)" class="group flex select-none items-center justify-between gap-2 rounded p-2 hover:bg-3 hover:text-brand focus-visible:outline-none focus-visible:ring-1 motion-safe:transition-colors absolute right-0 top-0 text-2">
Example
SELECT *
FROM students
WHERE major LIKE 'Math%' ;
copied = true)" @mouseleave.debounce.1000ms="copied && (copied = false)" @keydown.enter.debounce.1000ms="copied && (copied = false)" @keydown.space.debounce.1000ms="copied && (copied = false)" @touchstart.debounce.1000ms="copied && (copied = false)" class="group flex select-none items-center justify-between gap-2 rounded p-2 hover:bg-3 hover:text-brand focus-visible:outline-none focus-visible:ring-1 motion-safe:transition-colors absolute right-0 top-0 text-2">
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
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
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' , ...);
copied = true)" @mouseleave.debounce.1000ms="copied && (copied = false)" @keydown.enter.debounce.1000ms="copied && (copied = false)" @keydown.space.debounce.1000ms="copied && (copied = false)" @touchstart.debounce.1000ms="copied && (copied = false)" class="group flex select-none items-center justify-between gap-2 rounded p-2 hover:bg-3 hover:text-brand focus-visible:outline-none focus-visible:ring-1 motion-safe:transition-colors absolute right-0 top-0 text-2">
SELECT *
FROM table_name
WHERE column_name NOT IN ( 'value1' , 'value2' , ...);
copied = true)" @mouseleave.debounce.1000ms="copied && (copied = false)" @keydown.enter.debounce.1000ms="copied && (copied = false)" @keydown.space.debounce.1000ms="copied && (copied = false)" @touchstart.debounce.1000ms="copied && (copied = false)" class="group flex select-none items-center justify-between gap-2 rounded p-2 hover:bg-3 hover:text-brand focus-visible:outline-none focus-visible:ring-1 motion-safe:transition-colors absolute right-0 top-0 text-2">
Example
IN
SELECT *
FROM students
WHERE major IN ( 'Mathematics' , 'Biology' );
copied = true)" @mouseleave.debounce.1000ms="copied && (copied = false)" @keydown.enter.debounce.1000ms="copied && (copied = false)" @keydown.space.debounce.1000ms="copied && (copied = false)" @touchstart.debounce.1000ms="copied && (copied = false)" class="group flex select-none items-center justify-between gap-2 rounded p-2 hover:bg-3 hover:text-brand focus-visible:outline-none focus-visible:ring-1 motion-safe:transition-colors absolute right-0 top-0 text-2">
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
SELECT *
FROM students
WHERE major NOT IN ( 'Mathematics' , 'Biology' );
copied = true)" @mouseleave.debounce.1000ms="copied && (copied = false)" @keydown.enter.debounce.1000ms="copied && (copied = false)" @keydown.space.debounce.1000ms="copied && (copied = false)" @touchstart.debounce.1000ms="copied && (copied = false)" class="group flex select-none items-center justify-between gap-2 rounded p-2 hover:bg-3 hover:text-brand focus-visible:outline-none focus-visible:ring-1 motion-safe:transition-colors absolute right-0 top-0 text-2">
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
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
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.