AND & OR in DBMS

AND & OR

On this article, we will learn about AND & OR  in DBMS. Some times user requires more than one condition for filtering the data, this purpose is served by AND / OR clauses

AND OR in DBMS

AND & OR in DBMS

  • Sometimes user requires more than one condition for filtering the data, this purpose is served by AND / OR clauses
  • The AND  and  OR operators are used with the where clause for precise filtration of data from the database tables by combining more than one condition along with select, update and delete queries

Syntax:

SELECT Column1, Column2....
FROM table_name
WHERE
condition1 AND/OR condition2,
AND/OR condition4......;

AND clause

Definition: The AND results true only when all the conjunction of conditions specified after the where clause are satisfied

Syntax:

SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;
Consider a sample table emp
EMPNO ENAME JOB MGR HIREDATE SAL DEPTNO
7839 KING PRESIDENT 17-NOV-81 5000 10
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7566 JONES MANAGER 7839 02-APR-81 2975 20
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7369 SMITH CLERK 7902 17-DEC-80 800 20

Display the records of those employees who are working as a manager and getting salary greater than 2500 /-

select * from emp
where job='MANAGER' AND sal>2500;
O/P 2 rows selected.
EMPNO ENAME JOB MGR HIREDATE SAL DEPTNO
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7566 JONES MANAGER 7839 02-APR-81 2975 20
Here two conditions first employee must be a manager at the same time he must be getting salary greater than 2500 then only condition becomes true and the record gets displayed.

OR clause

Definition: Among multiple conditions specified in the where clause  the transaction is performed if any of the condition becomes true

Syntax

SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...

Display all employees records who are working as analyst and managers

select * from emp
where JOB='ANALYST' OR JOB='MANAGER';
O/P 5 rows selected 
EMPNO ENAME JOB MGR HIREDATE SAL DEPTNO
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7566 JONES MANAGER 7839 02-APR-81 2975 20
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7902 FORD ANALYST 7566 03-DEC-81 3000 20
In the example above, the data is fetched if  the employee is an analyst or a manager i.e data is fetched even if any one condition is satisfied.

Combining AND and OR

In the where clause it is possible that a condition is specified as a conjunction of both AND  & OR 

Syntax:

SELECT * FROM table_name 
WHERE condition1 AND|OR (condition2 AND|OR condition3....);

Example

select * from emp
where (sal>1500 OR job='MANAGER') AND (deptno=10 OR deptno=30);
O/P 3 rows selected.
EMPNO ENAME JOB MGR HIREDATE SAL DEPTNO
7839 KING PRESIDENT 17-NOV-81 5000 10
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
The above query displays the details of employees  having a salary greater than 1500/- or if he is a manager which is exclusively from 10th dept and 30th departments.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription