WHERE clause is used to specify/apply any condition while retrieving, updating or deleting data from a table. This clause is used mostly with SELECT, UPDATE and DELETE query.
However, when you are specifying a condition where clause, the query executes only for those records for which the condition specified by the where clause is true..
You will learn how to use the WHERE clause with a DELETE statement, or any other statement in this section,
DELETE FROM table_name WHERE [condition];
Also, the WHERE clause is commonly used at the end of any SQL query, to specify a condition for execution.
Example:
You can take a look at this table, student;
s_id | name | age |
---|---|---|
11 | Alexa | 21 |
12 | Nick | |
13 | Ada Chan |
At this stage, we can use the SELECT statement to display data of the table, based on a condition, which we will add to our SELECT query using the WHERE clause.
Now, let’s write a simple SQL query that will display the record for a student with s_id as 11.
SELECT s_id,
name,
age,
address
FROM student WHERE s_id = 11;
The table below shows the result of the above query.
s_id | name | age |
---|---|---|
11 | Alexa | 21 |
From the example above, we applied a condition to an integer value field, but suppose we want to apply the condition to the name field. Therefore, we must enclose the value in a single quote ' '. However, some databases even accept double quotes, but single quotes are accepted by all.
SELECT s_id,
name,
age,
address
FROM student WHERE name = 'Nick';
The table below shows the result of the above query.
s_id | name | age |
---|---|---|
12 | Nick |
The table below contains a list of operators that can be used while specifying the WHERE clause condition.
Operator | Description |
---|---|
= | Equal to |
!= | Not Equal to |
< | Less than |
> | Greater than |
<= | Less than or Equal to |
>= | Greate than or Equal to |
BETWEEN | Between a specified range of values |
LIKE | This is used to search for a pattern in value. |
IN | In a given set of values |