The SQL AND and OR operators are used to combine multiple conditions to narrow data in an SQL statement. These two operators are called conjunctive operators. These operators provide a means to make multiple comparisons with different operators in the same SQL statement.
The basic syntax for the WHERE clause when used in a SELECT statement is as follows.:
WHERE condition1 AND condition2...OR condition_n; |
Employee |
Emp_id | Name | Surname | Salary | Dept_id |
1 | Rajesh | Khanna | 45000 | 2 |
2 | Sneha | Gupta | 55000 | 4 |
3 | Sandeep | Nehte | 95000 | 1 |
4 | Kirti | Patil | 25000 | 3 |
5 | Nilesh | Jadhav | 45000 | 3 |
6 | Dipesh | Das | 35000 | 5 |
7 | Kailash | Rane | 62000 | 1 |
8 | Kavita | Deshpande | 48000 | 3 |
9 | Sanjay | Datt | 50000 | 2 |
10 | Shruti | Hasan | 78000 | 1 |
11 | Sandeep | Deshmukh | 52000 | 3 |
USING AND CONDITION:
USING AND CONDITION: SQL AND clause are used when you want to specify more than one condition in your SQL WHERE clause, and at the same time, you want all conditions to be true.
SELECT * FROM Employee WHERE Dept_id = '1' AND Salary > 50000; |
After Execution:
Emp_id | Name | Surname | Salary | Dept_id |
3 | Sandeep | Nehte | 95000 | 1 |
7 | Kailash | Rane | 62000 | 1 |
10 | Shruti | Hasan | 78000 | 1 |
USING OR CONDITION:
You can combine N number of conditions using OR operator.
Basic Syntex:
SELECT column1, column2, columnN FROM table_name WHERE [condition1] OR[condition2]...OR [conditionN] |
SQL Syntex:
SELECT * FROM Employee WHERE Name = 'Sandeep' OR Name = 'Kailash' ; |
After Execution:
Emp_id | Name | Surname | Salary | Dept_id |
3 | Sandeep | Nehte | 95000 | 1 |
7 | Kailash | Rane | 62000 | 1 |
11 | Sandeep | Deshmukh | 52000 | 3 |
We can also use more then one OR Clause in SQL Query:
SELECT * FROM Employee WHERE Name = 'Sandeep' OR Name = 'Kailash' OR Dept_id='2'; |
After Execution:
Emp_id | Name | Surname | Salary | Dept_id |
3 | Sandeep | Nehte | 95000 | 1 |
7 | Kailash | Rane | 62000 | 1 |
9 | Sanjay | Datt | 48000 | 2 |
11 | Sandeep | Deshmukh | 52000 | 3 |
COMBINING AND & OR CONDITIONS
SELECT * FROM Employee WHERE (Dept_id = '3' AND Surname = 'Gupta') OR (Salary>50000); |
After Execution:
Emp_id | Name | Surname | Salary | Dept_id |
2 | Sneha | Gupta | 55000 | 4 |
3 | Sandeep | Nehte | 95000 | 1 |
7 | Kailash | Rane | 62000 | 1 |
10 | Shruti | Hasan | 78000 | 1 |
11 | Sandeep | Deshmukh | 52000 | 3 |
No comments:
Post a Comment