The SQL WHERE Clause
What is the WHERE Clause?
The WHERE clause is used to filter records in SQL queries. It is used to extract only those records that satisfy a specific condition.
Example
Select all customers from Mexico:
SELECT * FROM Customers
WHERE Country = 'Mexico';
Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Note: The WHERE clause can be used not only in SELECT statements but also in UPDATE, DELETE, and other SQL statements.
Demo Database
Below is a selection from the “Customers” table used in the examples:
| CustomerID | CustomerName | ContactName | Country |
|---|---|---|---|
| 1 | Alfreds Futterkiste | Maria Anders | Germany |
| 2 | Ana Trujillo | Ana Trujillo | Mexico |
Text Fields vs. Numeric Fields
SQL requires single quotes around text values (e.g., ‘Mexico’), but numeric fields should not be enclosed in quotes.
SELECT * FROM Customers
WHERE CustomerID = 1;
Operators in the WHERE Clause
You can use various operators in the WHERE clause to filter records:
| Operator | Description |
|---|---|
| = | Equal |
| > | Greater than |
| >= | Greater than or equal |
| LIKE | Search for a pattern |
