SQL WHERE Clause
The SQL WHERE clause specifies a certain condition in the query and records are filtered from the database according to that condition.
Where clause is used with SELECT, UPDATE, DELETE statement.
WHERE Clause Syntax :
The syntax of the WHERE Clause is −
SELECT column1, column2, ... FROM table_name WHERE condition;
WHERE Clause Example :
Consider the Customer table with the following records -
CustomerID | CustomerName | Age | Address | CustomerSalary |
---|---|---|---|---|
1 | Aarav | 28 | Udaipur | 28000 |
2 | Vivaan | 25 | Mumbai | 30000 |
3 | Reyansh | 28 | Chennai | 35000 |
4 | Muhammad | 24 | Udaipur | 50000 |
5 | Sai | 30 | Mumbai | 27000 |
TEST CASE 1 :- The following code is an example, which would fetch the all fields from Customer table where Address is Udaipur.
SELECT * FROM Customer WHERE Address='Udaipur';
The result for the respective sql query is as follows −
CustomerID | CustomerName | Age | Address | CustomerSalary |
---|---|---|---|---|
1 | Aarav | 28 | Udaipur | 28000 |
4 | Muhammad | 24 | Udaipur | 50000 |
TEST CASE 2:- The following code is an example, which would fetch the all fields from Customer table where Age is 28.
SELECT * FROM Customer WHERE Age=28;
The result for the respective sql query is as follows −
CustomerID | CustomerName | Age | Address | CustomerSalary |
---|---|---|---|---|
1 | Aarav | 28 | Udaipur | 28000 |
3 | Reyansh | 28 | Chennai | 35000 |
Operators in The WHERE Clause:
The following operators can be used in the WHERE clause:
Operator | Description |
---|---|
= | Equal |
> | Greater than |
< | Less than |
>= | Greater than or Equal |
<= | Less than or Equal |
< > | Not equal |
BETWEEN | A range |
LIKE | Search for a pattern |
IN | specify multiple possible values |
Visit :
Discussion