AND and OR Operators – SQL

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

SQL query selects all Employee from the Employee_name “Ashwathi” AND the salary “8000”, in the “Employee” table:




SELECT * FROM Employee
WHERE Employee_name=’Ashwathi’
AND salary=8000;;

Result of the above query will be,

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

SQL query selects all Employee from the Employee_name “Ashwathi” OR the Employee_name “Achu”, in the “Employee” table:

SELECT * FROM Employee
WHERE Employee_name=’Ashwathi’
OR Employee_name=’Achu’;;

Result of the above query will be,

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

SQL query selects all Employee from the Employee_name “Ashwathi” AND the salary “8000” or salary ‘10000’ in the “Employee” table:




SELECT * FROM Employee
WHERE Employee_name=’Ashwathi’ AND(salary=8000 OR salary=10000);

Result of the above query will be,

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

SQL query selects all Employee from the Employee_name “Ashwathi” OR the Employee “Achu”, in the “Employee” table:

SELECT * FROM Employee
WHERE Employee_name=’Ashwathi’ AND(salary=8000 OR salary=10000);

Result of the above query will be,

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.

Leave a Reply

Your email address will not be published.