SQL Where Clause
Where clause/keyword is used to specify records on basis of some condition. This condition may be relational or logical expression, depending on requirements.
Where clause can be used with every DML commands such as INSERT, SELECT and DELETE. Where clause is commonly used with select statement. It is good practice to filter record every time using where clause.
We can use multiple conditions with single where clause. Each condition will be evaluated and results will be filter accumulatively.
WHERE Keyword Uses
Each table in database may contain a large amount of records, so that each time we do not require to retrieve all records using select statement. For this purpose SQL provide where keyword that can filter records from table basis on particular condition. A condition in SQL is relational or logical expression that is evacuated as TRUE or FALSE. Where clause can be used with any field of table to display selective information.
Syntax of where clause in SQL statement is
SELECT list_of_columns | * FROM table_name WHERE condition;
To write multiple SQL statements for execution we separate each statement using semi colon.
This example is using the same data table that is used in SELECT statement.
Let we have to select all employee whose blood group is O-. So we will write SQL statement as.
SELECT * FROM employee WHERE blood_group= "O-";
The result of above will also a table containing records of employees having O- blood group.
String Matching in WHERE Clause
SQL allows to match strings (collection of characters) in where clause. For this purpose single or double quotes are used around parameter. Parameter is the string that is required to be matched. In above example blood group is string parameter and is enclosed in double quotes. It is recommended to use single quotes, but both are supported.
SQL Numeric Parameter
While providing numeric/integers parameter with where clause, there is no need of encapsulate value within single or double quotes.
Where clause condition
Condition is vital part of where clause. It is composed of relational operators. These are commonly used mathematical operators. This condition contain two operands and one operator. The list of relational operators used with condition are as follows.
- = (equal to)
- <> (Not equal to)
- >= (greater than or equal to)
- <= (Less than or equal to)
- < (Less than)
- > (Greater than)
SQL Condition Keywords
In SQL some keywords are also used to match particular format. These keywords are used with where clause. Some of these are provided here.
|1||IN||This conditional keyword is used to match distinct values provided as parameter.|
|2||BETWEEN||Between keyword is used to match column value from the continuous data set or series.|
|3||LIKE||Like keyword in SQL statements is used for string pattern matching.|