The WHERE clause filters records based on specified conditions, extracting only those that meet the criteria.
SELECT column1, column2, … FROM table_name WHERE condition; |
Note: The WHERE clause is not exclusive to SELECT statements; it is also utilized in UPDATE, DELETE, and other SQL operations. |
Here is an excerpt from the “Customers” table in the Northwind sample database:
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 |
The SQL statement below retrieves all customers located in “Mexico”:
SELECT * FROM Customers WHERE Country = ‘Mexico’; |
SQL necessitates single quotes for text values (with some database systems also accepting double quotes), while numeric fields should not be enclosed in quotes.
SELECT * FROM Customers WHERE CustomerID = 1; |
The following operators are applicable within the WHERE clause:
Operator |
Description |
= |
Equal |
> |
Greater than |
< |
Less than |
>= |
Greater than or equal |
<= |
Less than or equal |
<> |
Not equal. Note: In some SQL versions, this operator can also be represented as != |
BETWEEN |
Within a specified range |
LIKE |
To search for a specific pattern |
IN |
To specify multiple potential values for a column |