B4 - 72 DBMS P3
B4 - 72 DBMS P3
B4 - 72 DBMS P3
Aim: Implementa on of queries using Aggregate Func on and demonstrate the use of
Group By clause on underlying tables in the database.
2 product_id integer,
3 product_type_id integer,
4 name varchar2(30),
5 price integer,
7 );
Table created.
1 row created.
1 row created.
1 row created.
1 row created.
SQL> insert into products values (5,2,'Smart Phone',2000.00);
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
TASK 1:
1. Count the number of products
SQL> select count(*) from products;
COUNT(*)
----------
9
COUNT(*) TOTALPRICE
---------- ----------
9 16500
COUNT(PRODUCT_TYPE_ID)
----------------------
9
COUNT(DISTINCTPRODUCT_TYPE_ID)
------------------------------
3
AVERAGEPRICE
------------
1833.33333
AVERAGEPRICE
------------
2000
MAX(PRICE)
----------
3000
SQL> select min(price) from products;
MIN(PRICE)
----------
1000
MAX(NAME)
------------------------------
Smart Phone
MIN(NAME)
------------------------------
Automated WM
VARIANCE(PRICE)
---------------
437500
PRODUCT_TYPE_ID AVERAGE_PRICE
--------------- -------------
1 1500
2 2000
3 2000
PRODUCT_TYPE_ID PRICEVARIANCE
--------------- -------------
1 250000
2 1000000
3 250000
PRODUCT_TYPE_ID PRICEVARIANCE
--------------- -------------
1 250000
3 250000
2 1000000
15. Calculate average price group by product_type_id and having average price greater
than 1500.
SQL> select product_type_id,avg(price) as AveragePrice from products group by
product_type_id having avg(price)>1500;
PRODUCT_TYPE_ID AVERAGEPRICE
--------------- ------------
2 2000
3 2000
16. Calculate average price of the products whose price is less that Rs.2000 and group
by product_type_id and having average price greater than 1500.
SQL> select product_type_id,avg(price) as AveragePrice from products where price<2000
group by product_type_id having avg(price)>1500;
no rows selected
TASK 2:
1. Find the highest sal of EMP table
SQL> select max(sal) as highest_sal from emp;
HIGHEST_SAL
-----------
5000
5. Find the total annual sal to distribute job wise in the year 81.
SQL> select job,sum(sal*12) as annual_sal from emp where hiredate between '1-jan-1981'
and '31-dec-1981' group by job;
JOB ANNUAL_SAL
--------- ----------
SALESMAN 67200
CLERK 11400
PRESIDENT 60000
MANAGER 99300
ANALYST 36000
AVG(SAL)
----------
1037.5
7. List the employeein dept 20 whose sal is >the average sal 0f dept 10 emps
SQL> select * from emp where deptno=20 and sal>(select avg(sal) from emp where
deptno=10);
8. Display the number of employee for each job group deptno wise
SQL> select deptno,job,count(*) as num_emp from emp group by deptno,job order by
deptno,job;
9 rows selected.
9. List the manage rno and the number of employees working for those mgrs in the
ascending Mgrno.
SQL> select mgr,count(*) as num_emp from emp group by mgr order by mgr asc;
MGR NUM_EMP
---------- ----------
7566 2
7698 5
7782 1
7788 1
7839 3
7902 1
1
7 rows selected.
11. List the department, details where at least two emps are working
SQL> select * from (select deptno,count(*) as num_emp from emp group by deptno) where
num_emp>=2;
DEPTNO NUM_EMP
---------- ----------
30 6
20 5
10 3
12. List the names of the emps who are ge ng the highest sal dept wise.
SQL> select e1.ename,e1.deptno,e1.sal from emp e1 where e1.sal=(select max(e2.sal) from
emp e2 where e2.deptno=e1.deptno
);
14. List the no. of emps in each department where the no. is more than 3.
SQL> select deptno,count(*) from emp group by deptno having count(*)>3;
DEPTNO COUNT(*)
---------- ----------
30 6
20 5
15. Find out how may Managers are their in the company.
SQL> select count(job) from emp where job='MANAGER';
COUNT(JOB)
----------
3
16. Check whether all the emps numbers are indeed unique
SQL> select empno from emp group by empno having count(empno)>1;
no rows selected
17. Find all the emps who earn the minimum Salary for each job wise in ascending
order.
SQL> select job,sal,ename from emp where (job,sal) in (select job,min(sal) as min_sal from
emp group by job) order by job asc;
18. Find out all the emps who earn highest salary in each job type. Sort in descending
salary order.
SQL> select job,sal,ename from emp where (job,sal) in (select job,max(sal) as max_sal from
emp group by job) order by sal desc;
DEPTNO
----------
40
20. List the No.of emp’s and Avg salary within each department for each job.
SQL> select d.deptno,e.job,count(*) as emp_count,avg(e.sal) as avg_sal from emp e inner
join dept d on e.deptno=d.deptno group by d.deptno, e.job;
9 rows selected.
21. Find the maximum average salary drawn for each job except for ‘President’.
SQL> select job,max(avg_sal) as max_avg_sal from (select job,avg(sal) as avg_sal from emp
where job <> 'PRESIDENT' group by job) group by job;
JOB MAX_AVG_SAL
--------- -----------
CLERK 1037.5
SALESMAN 1400
MANAGER 2758.33333
ANALYST 3000
23. List the details of most recently hired emp of dept 30.
SQL> select * from emp where deptno=30 and rownum=1 order by hiredate desc ;
24. Find the count of employee, average salary and sum of the salary
SQL> select count(*) as emp_count,avg(sal) as avg_sal, sum(sal) as total_sal from emp;
26. Find the count of employee, average salary and sum of the salary and group by
department number wise and job wise and in the ascending order of dept number
and Job.
SQL> select deptno,job,count(*) as emp_count,avg(sal) as avg_sal, sum(sal) as total_sal from
emp group by deptno,job order by deptno asc,job asc;