The WHERE clause is used to filter records.
SQL WHERE clause
The 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;
FROM table_name
WHERE column_name operator value;
Demo database
In this tutorial, we will use the sample database.
The following is the data selected 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 | Rookie Tutorial | http : //www.runoob.COM / | 4689 | CN | | 4 | Twitter | HTTP : //weibo.com/ | 20 | CN | | 5 | Facebook | HTTPS : //www.facebook.com/ | 3 | USA | + --- - + -------------- + --------------------------- + ----- - + --------- +
WHERE clause instance
The following SQL statement selects all sites from the "Websites" table for the "CN":
Examples
SELECT * FROM Websites WHERE country = ' CN ' ;
Execute the output:
Text field vs. numeric field
SQL uses single quotation marks to wrap text values (most database systems also accept double quotes).
In the previous example, the 'CN' text field uses single quotation marks.
If it is a numeric field, do not use quotation marks.
Examples
SELECT * FROM Websites WHERE id = 1 ;
Execute the output:
The 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, the operator can be written as! = |
> | more than the |
< | Less than |
> = | greater or equal to |
<= | Less than or equal to |
BETWEEN | In a certain range |
LIKE | Search for a pattern |
IN | Specifies multiple possible values for a column |