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
- Sometimes user requires more than one condition for filtering the data, this purpose is served by AND / OR clauses
- The
AND
andOR
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 |
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 |
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 |
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
Login/Signup to comment