TOP

SQL WHERE Clause

SQL WHERE Description

The WHERE clause is used to filter records.

It is used to extract only those records that meet a certain condition.


WHERE Syntax

SELECT column1, column2, ...
FROM table_name
WHERE condition
The WHERE clause is not only used in SELECT statements, it is also used in UPDATE, DELETE, and more!

Demonstration database

The following is a sample from the "Customers" table of the "Northwind" database:

CustomerIDCustomerNameContactNameAddressCityPostalCodeCountry
1Alfreds FutterkisteMaria AndersObere Str. 57Berlin12209Germany
2Ana Trujillo Emparedados y heladosAna TrujilloAvda. de la Constitución 2222México D.F.5021Mexico
3Antonio Moreno TaqueríaAntonio MorenoMataderos 2312México D.F.5023Mexico
4Around the HornThomas Hardy120 Hanover Sq.LondonWA1 1DPUK
5Berglunds snabbköpChristina BerglundBerguvsvägen 8LuleåS-958 22Sweden

SQL WHERE Example

The following SQL statement selects all customers from the country "Mexico" in the table "Customers":

Run SQLSELECT * FROM Customers 
WHERE Country = 'Mexico'

Text fields vs Numeric fields

SQL requires single quotes around text values (most database systems also allow double quotes).

However, numeric fields should not be enclosed in quotes:

Run SQLSELECT * FROM Customers 
WHERE CustomerID = 1

Operators with a WHERE clause

The following statements can be used with a WHERE clause:

Operator Description
= Equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
<> Not equal to Note: In some versions of SQL, this operator may be written as !=
BETWEEN Between a certain range (inclusively)
LIKE Search for a pattern
IN To specify multiple possible values for a column