WHERE
| Since: | SQL-92(1992) |
|---|
A clause used to filter rows by a condition. You can specify various conditions, including comparison operators and range checks.
Syntax
Retrieves rows that match the condition.
SELECT column FROM table WHERE condition;
Retrieves rows within a range using BETWEEN.
SELECT column FROM table WHERE column BETWEEN value1 AND value2;
Retrieves rows that match any value in a list.
SELECT column FROM table WHERE column IN (value1, value2, value3);
Operators
| Operator | Description |
|---|---|
| = | Retrieves rows where the value is equal. |
| <> or != | Retrieves rows where the value is not equal. |
| < | Retrieves rows where the value is less than the specified value. |
| > | Retrieves rows where the value is greater than the specified value. |
| <= | Retrieves rows where the value is less than or equal to the specified value. |
| >= | Retrieves rows where the value is greater than or equal to the specified value. |
| BETWEEN value1 AND value2 | Retrieves rows within the range from value1 to value2, inclusive. |
| IN (value1, value2, ...) | Retrieves rows that match any value in the list. |
| NOT IN (value1, value2, ...) | Retrieves rows that do not match any value in the list. |
Sample Code
The following 'employees' table is used in the examples below.
Retrieves employees in the 'Capsule Corp' department.
sample_where.sql
SELECT name, department FROM employees WHERE department = 'Capsule Corp';
+--------+--------------+ | name | department | +--------+--------------+ | Vegeta | Capsule Corp | | Bulma | Capsule Corp | +--------+--------------+ 2 rows in set
Retrieves employees with a salary of 300,000 or more.
sample_where.sql
SELECT name, salary FROM employees WHERE salary >= 300000;
+----------+--------+ | name | salary | +----------+--------+ | Son Goku | 300000 | | Bulma | 320000 | +----------+--------+ 2 rows in set
Retrieves employees with a salary between 250,000 and 350,000.
sample_where.sql
SELECT name, salary FROM employees WHERE salary BETWEEN 250000 AND 350000;
+----------+--------+ | name | salary | +----------+--------+ | Son Goku | 300000 | | Vegeta | 280000 | | Bulma | 320000 | +----------+--------+ 3 rows in set
Retrieves employees with a salary of 280,000 or 300,000.
sample_where.sql
SELECT name, salary FROM employees WHERE salary IN (280000, 300000);
+----------+--------+ | name | salary | +----------+--------+ | Son Goku | 300000 | | Vegeta | 280000 | +----------+--------+ 2 rows in set
Retrieves employees who are not in the 'NERV' department.
sample_where.sql
SELECT name, department FROM employees WHERE department <> 'NERV';
+----------+---------------+ | name | department | +----------+---------------+ | Son Goku | Turtle School | | Vegeta | Capsule Corp | | Bulma | Capsule Corp | +----------+---------------+ 3 rows in set
Database-Specific Notes
The basic syntax of WHERE — including comparison operators, BETWEEN, and IN — works consistently across all major databases.
SELECT name, salary FROM employees WHERE salary >= 300000; SELECT name, salary FROM employees WHERE salary BETWEEN 250000 AND 350000; SELECT name, salary FROM employees WHERE salary IN (280000, 300000);
For the not-equal operator, MySQL, PostgreSQL, and SQLite both support != and <>. For Oracle and SQL Server, the standard SQL <> is recommended.
Standard SQL (works in all databases)
SELECT name FROM employees WHERE department <> 'NERV';
Notes
The WHERE clause can be used with SELECT, UPDATE, DELETE, and many other SQL statements. You can combine multiple conditions using AND and OR. See AND / OR / NOT for details.
BETWEEN performs a range search that includes both endpoints. salary BETWEEN 250000 AND 350000 is equivalent to salary >= 250000 AND salary <= 350000.
You cannot use = to check for NULL values. Use IS NULL instead. See IS NULL / IS NOT NULL for details.
If you find any errors or copyright issues, please contact us.