SQL WHERE clause

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..

 

Syntax for WHERE clause

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_idnameage
11Alexa21
12Nick 
13Ada 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_idnameage
11Alexa21

 

Applying condition on Text Fields

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_idnameage
12Nick 

 

Operators for WHERE clause condition

The table below contains a list of operators that can be used while specifying the WHERE clause condition.

OperatorDescription
=Equal to
!=Not Equal to
<Less than
>Greater than
<=Less than or Equal to
>=Greate than or Equal to
BETWEENBetween a specified range of values
LIKEThis is used to search for a pattern in value.
INIn a given set of values