Tutorial on SQL WHERE Clause
The WHERE clause is mainly used to filter the result set depending on certain conditions or situation.
The syntax for using WHERE in the SELECT statement is given below:
“Condition” might include a single comparison clause (known as simple condition) or multiple comparison clauses, when combined together using AND or OR operators (known as compound condition).
Example of the WHERE Clause with following given Condition
Select all country names, having sales above $1,000 in Table Country_Information
Enter the following query :
WHERE Sales > 1000;
Example of the WHERE Clause with OR Operator
To access all data having sales greater than $1,000 or having transaction date of ‘Jan-08-1999’, we are allowed to use the following SQL,
WHERE Sales > 1000 OR Txn_Date = ‘Jan-08-1999’;
By using WHERE With UPDATE and DELETE command
Further, in addition to the SELECT statement, the WHERE clause may also be used with UPDATE and DELETE commands.
For performing these exercises, assume a table known as Users having following data:
1. Find the valid SQL statement from the following? (There could be more than one answer)
a) SELECT * FROM Users WHERE Gender = ‘M’;
b) SELECT * WHERE Gender = ‘M’ FROM Users;
c) SELECT Gender= ‘M’ FROM Users;
d) SELECT Gender FROM Users WHERE Last_Name = ‘Wilkes’;
2. Give the result of the following given query?
SELECT * FROM Users WHERE Join_Date = ‘Apr-09-2015’;
3. The condition that is used in the WHERE clause should include a column that is part of the SELECT clause (True or False).