(SQL) WHERE: Specifying Conditions
The WHERE clause is used to filter records. It is used to extract only those records that fulfill a specified condition and usually follows a SELECT
expression.
Example
Select all customers from Mexico:
SELECT * FROM Customers WHERE Country='Mexico';
Text Fields vs. Numeric Fields
SQL requires single quotes around text values (most database systems will also allow double quotes).
However, numeric fields should not be enclosed in quotes:
Example
SELECT * FROM Customers WHERE CustomerID=1;
Operators in The WHERE Clause
You can use other operators than the = operator to filter the search.
Example
Select all customers with a CustomerID greater than 80:
SELECT * FROM Customers WHERE CustomerID > 80;
The following operators can be used in the WHERE clause:
Operator | Description |
= | Equal |
> | Greater than |
< | Less than |
>= | Greater than or equal |
<= | Less than or equal |
<> | Not equal. Note: In some versions of SQL this operator may be written as != |
BETWEEN | Between a certain range, as in WHERE population BETWEEN 500000 AND 5000000 |
LIKE | Search for a pattern, as in WHERE name LIKE '_ublin' |
IN | To specify multiple possible values for a column, as in WHERE country_id IN (1, 4, 7) |