Hi Guys,
Today we share our idea related to AND and OR operator with example.
The AND and OR operators are the conditional operator in SQL. It is used to filter the data or records based on conditions. The main difference between AND and OR operators are: the AND operator displays a record if both the first condition AND the second condition are true and the OR operator displays a record if either the first condition OR the second condition is true.
The AND Operator
The AND operator use in SQL statement’s WHERE clause in SELECT query. It allows the existence of multiple conditions in an SQL statement’s WHERE clause.
Syntax:
The basic syntax of AND operator with WHERE clause is as follows:
SELECT column1, column2, columnN
FROM table_name
WHERE [condition1] AND [condition2]…AND [conditionN];
We can combine N number of conditions using AND operator.The AND operator displays a record if both the first condition AND the second condition are true
Example using AND operator. Consider following Employee table
Employee_id | Employee_name | salary |
---|---|---|
1 | Appu | 9000 |
2 | Ammu | 8000 |
3 | Achu | 6000 |
4 | Arathy | 10000 |
5 | Ashwathi | 8000 |
Employee_id | Employee_name | salary |
---|---|---|
5 | Ashwathi | 8000 |
The OR Operator
The OR operator use in SQL statement’s WHERE clause in SELECT query. It allows the existence of multiple conditions in an SQL statement’s WHERE clause.
Syntax
The basic syntax of OR operator with WHERE clause is as follows:
SELECT column1, column2, columnN
FROM table_name
WHERE [condition1] OR [condition2]…OR [conditionN]
We can combine N number of conditions using OR operator.The OR operatordisplays a record if either the first condition OR the second condition is true.
Example using OR operator. Consider following Employee table
Employee_id | Employee_name | salary |
---|---|---|
1 | Appu | 9000 |
2 | Ammu | 8000 |
3 | Achu | 6000 |
4 | Arathy | 10000 |
5 | Ashwathi | 8000 |
Employee_id | Employee_name | salary |
---|---|---|
3 | Achu | 6000 |
5 | Ashwathi | 8000 |
Combining AND & OR
You can also combine AND and OR operator.
Example using Combining AND and OR operator. Consider following Employee table
Employee_id | Employee_name | salary |
---|---|---|
1 | Appu | 9000 |
2 | Ammu | 8000 |
3 | Achu | 6000 |
4 | Arathy | 10000 |
5 | Ashwathi | 8000 |
Employee_id | Employee_name | salary |
---|---|---|
5 | Ashwathi | 8000 |
Example2 using Combining AND and OR operator. Consider following Employee table
Employee_id | Employee_name | salary |
---|---|---|
1 | Appu | 9000 |
2 | Ammu | 8000 |
3 | Achu | 6000 |
4 | Ashwathi | 10000 |
5 | Ashwathi | 8000 |
Employee_id | Employee_name | salary |
---|---|---|
4 | Ashwathi | 10000 |
5 | Ashwathi | 8000 |
If anyone has doubts on this topic then please do let me know by leaving comments or send me an email.