WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

SQL WHERE Clause


The WHERE clause is used to filter records.


SQL WHERE clause

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

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 welookups sample database.

Here is the data from the "Websites" table:

+----+--------------+---------------------------+-------+---------+
| id | name         | url                       | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 1  | Google       | https://www.google.cm/| 1     | USA     |
| 2  | Indiatimes          | https://www.Indiatimes.com/| 13    | IN      |
| 3  | zomato      | http://www.zomato.com/| 4689  | 88      |
| 4  | indiamart          | http://indiamart.com/    | 20    | IN      |
| 5  | Facebook     | https://www.facebook.com/| 3     | USA     |
+----+--------------+---------------------------+-------+---------+


WHERE clause example

The following SQL statement selects all websites with the country "IN" from the "Websites" table:

Example

SELECT * FROM Websites WHERE country='CN';

Text field vs. Numeric field

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

In the previous example, the 'IN' text field used single quotes.

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

Example

SELECT * FROM Websites WHERE id=1;

Operators in WHERE clause

The following operators can be used in the WHERE clause:

operator Description
= equal
<> is not equal. Note: In some versions of SQL, this 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 Specify multiple possible values for a column