More SQL Please: WHERE

pop out tables

users

| id | name_first | name_last | age | email_address | |----|------------|-----------|-----|---------------------------| | 1 | Marcus | Crawford | 25 | marcus@mcrawford.ca | | 2 | Elena | Moreno | 40 | elena@hellocorp.com | | 3 | Simon | Chen | 22 | simon@sqlanalytics.io | | 4 | Priya | Amini | 31 | priya@aminiconsulting.com | | 5 | Zara | Amini | 30 | zara@aminiconsulting.com |

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 SELECTed 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);
previous: SELECT home next: ORDER BY