0% found this document useful (0 votes)
27 views

DBMS LAB Exp-2

The document describes queries using various operators such as ANY, ALL, IN, EXISTS, NOT EXISTS, UNION, and INTERSECT on tables EMP and DEPT. It provides examples of queries using each operator to retrieve employee details that meet specified conditions. For ANY and ALL, examples filter employees by salary compared to a department. IN filters employees by job. UNION combines results from DEPT and DEPT_NEW tables, while INTERSECT finds matching rows. EXISTS and NOT EXISTS check for matching department numbers between EMP and DEPT tables.

Uploaded by

d38693402
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

DBMS LAB Exp-2

The document describes queries using various operators such as ANY, ALL, IN, EXISTS, NOT EXISTS, UNION, and INTERSECT on tables EMP and DEPT. It provides examples of queries using each operator to retrieve employee details that meet specified conditions. For ANY and ALL, examples filter employees by salary compared to a department. IN filters employees by job. UNION combines results from DEPT and DEPT_NEW tables, while INTERSECT finds matching rows. EXISTS and NOT EXISTS check for matching department numbers between EMP and DEPT tables.

Uploaded by

d38693402
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Experiment No: 2

AIM: Write the Queries (along with sub Queries) using ANY, ALL, IN, EXISTS,
NOTEXISTS, UNION, INTERSET.

Based upon the EMP table in Experiment no: 1

Queries (along with sub Queries):

SQL> select * from emp where sal > (select SAL from emp where ename ='SCOTT');

SQL> select SAL from emp where ename ='SCOTT';

SAL
----------
3000
SQL> select empno,sal,deptno from emp where sal > (select SAL from emp where
ename ='MILLER');

SQL> select SAL from emp where ename ='MILLER';

SAL
----------
1300

1. ANY:Queries (along with sub Queries)


● Retrieve the employee details whose salary is greater than the salary of any one
of the employees in department 10.
● Retrieve the employee details whose salary is less than the salary of any one of
the employees in department 10.

SQL> SELECT * FROM EMP WHERE SAL > ANY ( SELECT SAL FROM EMP
WHERE DEPTNO =10 );
SQL> SELECT * FROM EMP WHERE SAL < ANY ( SELECT SAL FROM EMP
WHERE DEPTNO =10 );

2. ALL: Queries (along with sub Queries)


● Retrieve the employees details whose salary is greater than the salary of all the
employees in department 10.
● Retrieve the employees details whose salary is less than the salary of all the
employees in department 10.

SQL> SELECT * FROM EMP WHERE SAL < ALL ( SELECT SAL FROM EMP WHERE
DEPTNO =10 );

SQL> SELECT * FROM EMP WHERE SAL > ALL ( SELECT SAL FROM EMP WHERE
DEPTNO =10 );
no rows selected
3. IN: Queries (along with sub Queries)

SQL> select * from emp where job in ('MANAGER','CLERK');

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO


---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-DEC-80 800 20
7566 JONES MANAGER 7839 02-APR-81 2975 20
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7876 ADAMS CLERK 7788 12-JAN-83 1100 20
7900 JAMES CLERK 7698 03-DEC-81 950 30
7934 MILLER CLERK 7782 23-JAN-82 1300 10

Create the tables based on the below table_name & column_names and insert the
values to their respective table.

SQL> SELECT * FROM DEPT; [Table - 1]

DEPTNO DNAME LOC


------------- ------------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON

SQL> SELECT * FROM DEPT_NEW; [Table - 2]

DNO DNAME LOC


---------- --------------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
3.UNION:

SQL> SELECT * FROM DEPT


2 UNION
3 SELECT * FROM DEPT_NEW;

DEPTNO DNAME LOC


------------ —------------------ -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON

4.UNION ALL:

SQL> SELECT * FROM DEPT


2 UNION ALL
3 SELECT* FROM DEPT_NEW;
DEPTNO DNAME LOC
------------ --------------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO

5. INTERSECT :
SQL> SELECT * FROM DEPT
2 INTERSECT
3 SELECT * FROM DEPT_NEW;

DEPTNO DNAME LOC


------------- --------------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
6.NOT EXISTS:

SQL> select * from dept where not exists ( select * from emp where emp.deptno
=dept.deptno);

DEPTNO DNAME LOCATION


---------- ------------------------------ ------------------------------
40 OPERATIONS BOSTON

7.EXISTS:
SQL> select * from dept where exists ( select * from emp where emp.deptno
=dept.deptno);

DEPTNO DNAME LOCATION


---------- ------------------------------ ------------------------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO

You might also like