A WHERE
clause can be added to statements to filter the outcome of the statement based on a condition.
In the case of a SELECT
, this means the result set is filtered to only include results that match the condition. WHERE
can also be applied to UPDATE
and DELETE
statements to limit the rows they affect.
Note that a column does not need to be included as a SELECT
ed column to be used in a WHERE
clause.
SELECT name_first, name_last
FROM users
WHERE email_address = 'marcus@mcrawford.ca';
WHERE
clauses can use operators besides =
:
>
Greater than.
<
Less than.
>=
Greater than or equal to.
<=
Less than or equal to.
<>
or !=
Not Equal.
WHERE age BETWEEN 30 AND 40;
Match when between two values.
WHERE email_address LIKE '%mail.com';
Match based on a pattern. %
is typically the wildcard character used to match 0 or more of any character.
WHERE id IN (1,2,3);
Match based on any value in a list of values or a subquery.
WHERE age IS NULL;
or IS NOT NULL;
Check for NULL.
See Operators for more examples.
SELECT name_first, age
FROM users
WHERE age < 30;
WHERE
clauses can have multiple predicates separated by logical operators such as AND
and OR
.
The NOT
operator can be applied before any of the above operators to negate them.
See Operators for more examples.
SELECT name_first, age, email_address
FROM users
WHERE email_address LIKE '%.com'
AND age NOT IN (40, 31);