The WHERE clause is used to filter records.


SQL WHERE clause

The WHERE clause is used to extract those records that meet the specified criteria.

SQL WHERE syntax

SELECT column_name,column_name
FROM table_name
WHERE column_name operator value;


Demo database

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

The following is the data selected from the "Websites" table:

+----+--------------+--- ------------------------+-------+---------+
| id | name | url                                                                    --------+-------+---------+
| 1 | Google | https://www.google.cm/ | 1 | USA |
| 2 | Taobao | https://www.taobao.com/ | 13 | CN |
| 3 | php Chinese website | //m.sbmmt.com/ | 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 the country "CN" from the "Websites" table "All websites:

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 quotation marks).

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

If it is a numeric field, please do not use quotation marks.

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:

OperatorDescription= is equal to <> is not equal to. > is greater than < is less than >= is greater than or equal to <=Less than or equal toBETWEENWithin a certain rangeLIKESearch for a patternINSpecify multiple possible values ​​for a column
Note: In some versions of SQL, this operator can be written as !=