Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

SQL WHERE Clause

SQL WHERE Clause


The WHERE clause is used to filter records.


SQL WHERE Clause

The WHERE clause is used to extract records that satisfy a specified condition.

SQL WHERE Syntax

SELECT column1, column2, ...
FROM table_name
WHERE condition;

Parameter Description:


Demonstration Database

In this tutorial, we will use the RUNOOB sample database.

Below is the data selected from the "Websites" table:


+----+--------------+---------------------------+-------+---------+
| id | name         | url                       | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 1  | Google       | https://www.google.cm/    | 1     | USA     |
| 2  | 淘宝          | https://www.taobao.com/   | 13    | CN      |
| 3  | Runoob Tutorial      | http://www.runoob.com/    | 4689  | CN      |
| 4  | 微博          | http://weibo.com/         | 20    | CN      |
| 5  | Facebook     | https://www.facebook.com/ | 3     | USA     |
+----+--------------+---------------------------+-------+---------+

WHERE Clause Examples

The following SQL statement selects all websites from the Websites table whose country is "CN":

Example

SELECT

FROM

Websites

WHERE

country

=

'

CN

'

;

Execution output:


Text Fields vs. Numeric Fields

SQL uses single quotes around text values (most database systems also accept double quotes).

In the previous example, the text value 'CN' was enclosed in 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 to. Note: in some versions of SQL, this operator can also 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