SQL WHERE
Clause
The WHERE clause is used to filter records.
SQL WHERE Clause
The WHERE clause is used to extract only those records that fulfill a specified condition.
SQL WHERE Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Parameter Explanation:
column1, column2, ...: The names of the fields to select. Multiple fields can be specified. If no field names are specified, all fields will be selected.
table_name: The name of the table to query.
Demonstration Database
In this tutorial, we will use the tutorialpro sample database.
Below is the data from the "Websites" table:
+----+--------------+---------------------------+-------+---------+
| id | name | url | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 1 | Google | https://www.google.cm/ | 1 | USA |
| 2 | Taobao | https://www.taobao.com/ | 13 | CN |
| 3 | tutorialpro.org | http://www.tutorialpro.org/ | 4689 | CN |
| 4 | Weibo | http://weibo.com/ | 20 | CN |
| 5 | Facebook | https://www.facebook.com/ | 3 | USA |
+----+--------------+---------------------------+-------+---------+
WHERE Clause Example
The following SQL statement selects all websites from the "Websites" table where the country is "CN":
Example
SELECT * FROM Websites WHERE country='CN';
Execution Output:
Text Fields vs. Numeric Fields
SQL uses single quotes to enclose text values (most database systems also accept double quotes).
In the previous example, the text field 'CN' used single quotes.
For numeric fields, do not use quotes.
Example
SELECT * FROM Websites WHERE id=1;
Execution Output:
Operators in the WHERE Clause
The following operators can be used in the WHERE clause:
Operator | Description |
---|---|
= | Equal |
<> | Not equal. Note: In some versions of SQL, this operator can be written as != |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
BETWEEN | Within a certain range |
LIKE | Search for a pattern |
IN | Specify multiple possible values for a column |