Overview
TheWHERE clause returns a specific value from a table or multiple tables based on specified conditions. It will filter out records you do not want to be included and only returns the exact result when the condition is fulfilled.
Syntax
The basic syntax of the WHERE clause is as follows −SELECT column1, column2, ...defines the columns where the records will be displayed.FROM table_namesets the table name where the records will be taken from.WHERE [condition]specifies the search condition using comparison or logical operators (e.g.,>,=,LIKE)
It starts with the
FROM clause -> then it executes the WHERE condition -> after that, it will SELECT the specified columns.Examples
Let’s assume that we have a table salary with records as follows:#Case 1: WHERE clause with = Operator
Here we will be using the “equal” operator to look up the employee who works in the Marketing department:
#Case 2: WHERE clause with != Operator
Here we will be using the “not equal” operator to look up the employee who doesn’t live in Texas:
We can use the
<> operator for another “not equal” operator.#Case 3: WHERE clause with > Operator
Here we will be using the “greater than” operator to figure out who has a salary above 20000:
We can use the
< operator for a “less than” condition.#Case 4: WHERE clause with <= Operator
Here we will be using the “less than or equal to” operator to see who has a salary less than or equal to 15000:
We can use the
>= operator for a “greater than or equal to” condition.#Case 5: WHERE clause with LIKE Operator
Here we will use the “like” operator to retrieve the employee whose first name starts with Br.
Do the reverse to get the result based on the last string,
%string.#Case 6: WHERE clause with IS NULL Operator
Here we will use the “is null” operator to search for the employee who doesn’t have a salary value. It will return true and display the result set if a value is NULL; otherwise, it will return false with no result set.