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:
 
SELECT “column_name”
FROM “table_name”
WHERE “condition”;
 
“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

Table Country_Information

Country _Name Sales Txn_Date
Los Angeles 1500 Jan-05-1999
San Diego 250 Jan-07-1999
Los Angeles 300 Jan-08-1999
Boston 700 Jan-08-1999

 
Enter the following query :
 
SELECT Country_Name
FROM Country_Information
WHERE Sales > 1000;

Output:

Country _Name
Los Angeles
 
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,
 
SELECT *
FROM Country_Information
WHERE Sales > 1000 OR Txn_Date = ‘Jan-08-1999’;

Result:

Country _Name Sales Txn_Date
Los Angeles 1500 Jan-05-1999
Los Angeles 300 Jan-08-1999
Boston 700 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.

Practice Exercises

For performing these exercises, assume a table known as Users having following data:
 

Table Users

First_Name Last_Name Birth_Date Gender Join_Date
Sophie Lee Jan-05-1960 F Apr-05-2015
Richard Brown Jan-07-1975 M Apr-05-2015
Jamal Santo Oct-08-1983 M Apr-09-2015
Casey Healy Sep-20-1969 M Apr-09-2015
Jill Wilkes Nov-20-1979 F Apr-15-2015

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

Kristin is a content strategist at Techarex Networks. Kristin follows the B2B technology space closely and loves to write on the latest changes in technology, futuretech and fixes for day to day how to issues. Besides writing Kristin also loves music, moves and skating.