SQL WHERE Clause
SQL > SQL Commands >
The WHERE clause is used to filter the result set based on the condition specified following the word WHERE.
The WHERE clause can be used with the following types of SQL statements:
The syntax for using WHERE in the SELECT statement is as follows:
The syntax for using WHERE in the UPDATE statement is as follows:
SET "column_1" = [new value]
The syntax for using WHERE in the DELETE statement is as follows:
DELETE FROM "table_name"
"Condition" can include a single comparison clause (called simple condition) or multiple comparison clauses combined together using AND or OR operators (compound condition).
We provide examples here to show how to use WHERE in the SELECT statement.
Example 1: WHERE Clause With Simple Condition
To select all stores with sales above $1,000 in Table Store_Information,
we key in,
WHERE Sales > 1000;
Example 2: WHERE Clause With OR Operator
To view all data with sales greater than $1,000 or with transaction date of 'Jan-08-1999', we use the following SQL,
WHERE Sales > 1000 OR Txn_Date = 'Jan-08-1999';
Using WHERE With UPDATE and DELETE
As mentioned above, the WHERE clause can be used with UPDATE and DELETE statements in addition to the SELECT statement. Examples of how to use the WHERE clause with these two commands can be seen in the UPDATE and DELETE sections.
For these exercises, assume we have a table called Users with the following data:
1. Which of the following SQL statement is valid? (There can be more than one answer)
2. What's the result of the following query?
3. (True or False) The condition used in the WHERE clause must include a column that is part of the SELECT clause.