Latest web development tutorials

SQL WHERE clause

WHERE clause is used to filter the records.


SQL WHERE clause

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

SQL WHERE Syntax

SELECT column_name , column_name
FROM table_name
WHERE column_name operator value ;


The demo database

In this tutorial, we will use w3big sample database.

The following is a selected "Websites" table data:

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


WHERE clause examples

The following SQL statement Select a country as "CN" from all the sites "Websites" table:

Examples

SELECT * FROM Websites WHERE country = 'CN';

Execution output:



Text field vs. a numeric field

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

In the last instance 'CN' text field uses a single quote.

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

Examples

SELECT * FROM Websites WHERE id = 1;

Execution output:



WHERE clause operator

The following operators can use in the WHERE clause:

Operators description
= equal
<> not equal to.NOTE: In some versions of SQL, the operator can be written as =!
> more than the
< Less than
> = greater or equal to
<= Less than or equal
BETWEEN Within a certain range
LIKE Search for a pattern
IN Specifies the possible values ​​for a plurality of columns