SQL WHERE Clause
The SQL WHERE clause is used to restrict the number of rows affected by a SELECT, UPDATE or DELETE query.
- WHERE clause is used to limit the number of rows.
- WHERE clause filters for rows that meet certain criteria.
- WHERE clause is followed by a condition that returns either true or false.
SQL WHERE syntax
The WHERE clause is not only used in SELECT statement, it is also used in UPDATE, DELETE etc.
Where clause with a SELECT
SELECT column_names
FROM table_name
WHERE [condition];
Where clause with a UPDATE
UPDATE table_name
SET column_name = value
WHERE [condition];
Where clause with a DELETE
DELETE table_name
WHERE [condition];
Employee Table
A sample of EMPLOYEE records table & the name of table Employee. The column(s) of the Employee table [ID, EMPLOYEE_NAME, EMPLOYEE_EMAIL, ADDRESS, CITY, and COUNTRY]:
ID |
EMPLOYEE_NAME |
EMPLOYEE_EMAIL |
ADDRESS |
CITY |
COUNTRY |
1 |
Mark J. |
mark@gmail.com |
House 53, Sq. |
London |
U.K. |
2 |
Lack Josh |
lackjosh@gmail.com |
2745 road mark |
Delhi |
India |
3 |
Kim Juhu |
kimjuhu@gmail.com |
Street 587327, 34 House |
Seoul |
South Korea |
4 |
Jack Jenifer |
jenifer@gmail.com |
464673 Mataderos |
Mexico D.F. |
Mexico |
5 |
McKinley |
mckinley@gmail.com |
54, Road Washington |
Washington |
U.S. |
6 |
Alena |
aln@gmail.com |
House No. 6563 |
Tokyo Japan |
Japan |
7 |
Mack Marina |
mmk@gmail.com |
43 Road, NH |
San Francisco |
U.S. |
8 |
Dipak Jha |
jha@gmail.com |
Mahanager, 26 Road |
Lucknow |
India |
SELECT Query, WHERE Clause Example
Note: For char value should be enclosed in quotes
SELECT * FROM Employee
WHERE Country = 'India';
Result
ID |
EmployeeName |
EmployeeEmail |
Address |
City |
Country |
2 |
Lack Josh |
lackjosh@gmail.com |
2745 road mark |
Delhi |
India |
8 |
Dipak Jha |
jha@gmail.com |
Mahanager, 26 Road |
Lucknow |
India |
WHERE Clause Operators
Operators |
Description |
=, != |
Equal, Not Equal |
>, < |
Greater than, Less than |
>=, <= |
Greater than or Equal, Less than or Equal |
BETWEEN |
A Certain Range |
LIKE |
Search for a pattern |
IN |
Specify multiple possible values for a column |