The WHERE clause serves to extract records that meet specific conditions, filtering out those that do not satisfy the specified criteria.
Retrieve all customers who are located in Mexico.
| SELECT * FROM Customers WHERE Country=‘Mexico’; |
SELECT column1, column2, ... |
| Note: The WHERE clause is employed not only in SELECT statements but also in UPDATE, DELETE, and other SQL operations. |
Here is a segment from the Customers table utilized in the examples:
|
CustomerID |
CustomerName |
ContactName |
Address |
City |
PostalCode |
Country |
|
1 |
Alfreds Futterkiste |
Maria Anders |
Obere Str. 57 |
Berlin |
12209 |
Germany |
|
2 |
Ana Trujillo Emparedados y helados |
Ana Trujillo |
Avda. de la Constitución 2222 |
México D.F. |
05021 |
Mexico |
|
3 |
Antonio Moreno Taquería |
Antonio Moreno |
Mataderos 2312 |
México D.F. |
05023 |
Mexico |
|
4 |
Around the Horn |
Thomas Hardy |
120 Hanover Sq. |
London |
WA1 1DP |
UK |
|
5 |
Berglunds snabbköp |
Christina Berglund |
Berguvsvägen 8 |
Luleå |
S-958 22 |
Sweden |
In SQL, text values must be enclosed in single quotes (double quotes may also be accepted by most database systems), while numeric fields should not be enclosed in quotes.
| SELECT * FROM Customers WHERE CustomerID=1; |
Alternative operators beyond “=” can be utilized to refine search filters.
Retrieve all customers whose CustomerID exceeds 80.
| SELECT * FROM Customers WHERE CustomerID > 80; |
Various operators are available for use within the WHERE clause:
|
Operator |
Description |
|
= |
Equal |
|
> |
Greater than |
|
< |
Less than |
|
>= |
Greater than or equal |
|
<= |
Less than or equal |
|
<> |
Not equivalent. Note: In certain SQL versions, this operator might be expressed as != |
|
BETWEEN |
Within a specific range. |
|
LIKE |
Please find a pattern. |
|
IN |
To delineate multiple potential values for a column. |