SQL WHEREClauses


The WHERE clause is used to filter records.


SQL WHERE Clause

The WHERE clause is used to extract records that meet specified conditions.

SQL WHERE Syntax

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

Parameter Description:

  • column1, column2, ...: The field name(s) to select; multiple fields are allowed. If no field names are specified, all fields are selected.
  • table_name: The name of the table to query.


Demo Database

In this tutorial, we will use the EXAMPLE 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  | Example      | http://www.example.com/    | 4689  | CN      |
| 4  | 微博          | 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 whose country is "CN":

Example

SELECT * FROM Websites WHERE country='CN';

Execution output result:



Text Fields vs. Numeric Fields

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

In the previous example, single quotes were used for the 'CN' text field.

If it is a numeric field, do not use quotes.

Example

SELECT * FROM Websites WHERE id=1;

Execution output result:



Operators in the WHERE Clause

The following operators can be used in the WHERE clause:

Operator Description
= Equal to
<> Not equal to.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 certain pattern
IN Specify multiple possible values for a column
Other extensions