SQL WHERE Clause
Where Clause :
In SQL where clause is used for filtering the records.
If condition mentioned in the where clause is satisfied by the record then and then that record will be selected.
SQL WHERE Syntax
SELECT column_name,column_name FROM table_name WHERE column_name operator value;
Demo Example :
In this example we are using our customer database. Consider following “CUSTOMER” table -
ID | NAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
1 | Raj | 20 | Pune | 1000.00 |
2 | Saurabh | 20 | Pune | 6000.00 |
3 | Omkar | 24 | Mumbai | 4000.00 |
4 | Anand | 23 | Pune | 3000.00 |
5 | Anmol | 29 | Goa | 1000.00 |
6 | Poonam | 25 | Delhi | 9000.00 |
Where Clause #1 : Select Customer living in Pune
Suppose we need to select all the customers that are living in Pune only then we can use below query -
SQL> Select * from CUSTOMER where city = 'Pune'
Above query will provide you following records
ID | NAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
1 | Raj | 20 | Pune | 1000.00 |
2 | Saurabh | 20 | Pune | 6000.00 |
4 | Anand | 23 | Pune | 3000.00 |
Where Clause #2 : Numeric Value
In the above example we have used single quote for specifying the value of the Column. In this example we will be using the numeric value as parameter in where clause.
Consider the below query -
SQL> Select * from CUSTOMER where ID = 3
Query will provide you following result -
ID | NAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
3 | Omkar | 24 | Mumbai | 4000.00 |
Where Clause #3 : Operators and Conditions
The following operators can be used in the WHERE clause:
Operator | Description | |||
---|---|---|---|---|
= | Equal | |||
<> | Not equal. Note: In some versions of SQL this operator may be written as != | |||
> | Greater than | |||
< | Less than | |||
>= | Greater than or equal | |||
<= | Less than or equal | |||
BETWEEN | Between an range provided | |||
LIKE | Searching for particular pattern | |||
IN | Specify multiple possible values for a column | |||
#91;/table#93; | ||||
Consider the following query - | ||||
SQL> Select * from CUSTOMER where ID > 3 AND CITY = 'PUNE' | ||||
Output of the query will be - | ||||
ID | NAME | AGE | ADDRESS | SALARY |
4 | Anand | 23 | Pune | 3000.00 |