SQL Where Clause
SQL “where” clause is used to retrieve a subset of table's data based on the filter condition specified. SQL “Where” clause is specified after table name. For example, To retrieve a row from the table specified below, following query is used.
Table Name: Employee
| Employee_ID | Employee_Name | Age |
|---|---|---|
| 1 | John | 28 |
| 2 | Alex | 24 |
| 3 | James | 35 |
| 4 | Roy | 22 |
| 5 | Kay | 44 |
Select * from Employee where employee_id = 2
| Employee_ID | Employee_Name | Age |
|---|---|---|
| 2 | Alex | 24 |
If we want to retrieve records which is greater than a specified value, greater than operator is used.
Select * from Employee where employee_id > 2
| Employee_ID | Employee_Name | Age |
|---|---|---|
| 3 | James | 35 |
| 4 | Roy | 22 |
| 5 | Kay | 44 |
To retrieve records which is greater than or equal to specified value, “>=” operator is used.
Select * from Employee where employee_id >= 2
| Employee_ID | Employee_Name | Age |
|---|---|---|
| 2 | Alex | 24 |
| 3 | James | 35 |
| 4 | Roy | 22 |
| 5 | Kay | 44 |
To retrieve records which is less than a specified value, "<" operator is used.
Select * from Employee where employee_id < 2
| Employee_ID | Employee_Name | Age |
|---|---|---|
| 1 | John | 28 |
To retrieve records which less than or equal to a specified value, operator used is "<=".
Select * from Employee where employee_id <= 2
| Employee_ID | Employee_Name | Age |
|---|---|---|
| 1 | John | 28 |
| 2 | Alex | 24 |
To retrieve records from a table which is not matching the specified filter condition, “< > or !=” not equal operator is used.
Select * from Employee where employee_id < > 2 or Select * from Employee where employee_id != 2
| Employee_ID | Employee_Name | Age |
|---|---|---|
| 1 | John | 28 |
| 3 | James | 35 |
| 4 | Roy | 22 |
| 5 | Kay | 44 |
SQL Between
“Between” operator is used to find matching records between two values.
Select * from Employee where employee_id between 2 and 4
| Employee_ID | Employee_Name | Age |
|---|---|---|
| 2 | Alex | 24 |
| 3 | James | 35 |
| 4 | Roy | 22 |
SQL Null
“Null” keyword is used to fetch data from a table where a columns value in NULL.
select * from employee where age is null