0% found this document useful (0 votes)
41 views15 pages

B4 - 72 DBMS P3

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 15

Prac cal 3

Name: Rugved Umesh Mhatre


Name:Vivek
Batch: B4 Mehar
Batch:B4
Roll
RollNo: 60
No:72

Aim: Implementa on of queries using Aggregate Func on and demonstrate the use of
Group By clause on underlying tables in the database.

Q. Create table using DDL Script

Products (product_id Integer, product_type_id Integer, Name Varchar2(30), Price Integer)

Note: product_id is Primary Key

Insert following 09 tupules using DML scripts

SQL> create table products(

2 product_id integer,

3 product_type_id integer,

4 name varchar2(30),

5 price integer,

6 constraint products_pk_product_id primary key(product_id)

7 );

Table created.

SQL> insert into products values (1,1,'Simple TV',1000.00);

1 row created.

SQL> insert into products values (2,1,'LED TV',1500.00);

1 row created.

SQL> insert into products values (3,1,'LCD TV',2000.00);

1 row created.

SQL> insert into products values (4,2,'Mobile Phone',1000.00);

1 row created.
SQL> insert into products values (5,2,'Smart Phone',2000.00);

1 row created.

SQL> insert into products values (6,2,'Jio Phone',3000.00);

1 row created.

SQL> insert into products values (7,3,'Simple WM',1500.00);

1 row created.

SQL> insert into products values (8,3,'Automated WM',2000.00);

1 row created.

SQL> insert into products values (9,3,'Semi WM',2500.00);

1 row created.

TASK 1:
1. Count the number of products
SQL> select count(*) from products;
COUNT(*)
----------
9

2. Count the number of products and sum of price of products


SQL> select count(*),sum(price) as totalprice from products;

COUNT(*) TOTALPRICE
---------- ----------
9 16500

3. Count the number of products_type_id


SQL> select count(product_type_id) from products;

COUNT(PRODUCT_TYPE_ID)
----------------------
9

4. Count the number of dis nct products_type_id


SQL> select count(dis nct product_type_id) from products;

COUNT(DISTINCTPRODUCT_TYPE_ID)
------------------------------
3

5. Calculate the average price of the product


SQL> select avg(price) as averageprice from products;

AVERAGEPRICE
------------
1833.33333

6. Calculate the average price of the dis nct product


SQL> select avg(dis nct price) as averageprice from products;

AVERAGEPRICE
------------
2000

7. Calculate maximum and minimum price of the product


SQL> select max(price) from products;

MAX(PRICE)
----------
3000
SQL> select min(price) from products;

MIN(PRICE)
----------
1000

8. Find the count of number of ROWID


SQL> select count(rowid) from products;
COUNT(ROWID)
------------
9

9. Find maximum and minimum product name


SQL> select max(name) from products;

MAX(NAME)
------------------------------
Smart Phone

SQL> select min(name) from products;

MIN(NAME)
------------------------------
Automated WM

10. Calculate standard devia on of price


SQL> select stddev(price) from products;
STDDEV(PRICE)
-------------
661.437828

11. Calculate variance of price


SQL> select variance(price) from products;

VARIANCE(PRICE)
---------------
437500

12. Calculate average price group by product_type_id


SQL> select product_type_id, avg(price) as average_price from products group by
product_type_id;

PRODUCT_TYPE_ID AVERAGE_PRICE
--------------- -------------
1 1500
2 2000
3 2000

13. Calculate Variance on price group by product_type_id


SQL> select product_type_id, variance(price) as pricevariance from products group by
product_type_id;

PRODUCT_TYPE_ID PRICEVARIANCE
--------------- -------------
1 250000
2 1000000
3 250000

14. Calculate Variance on price group by product_type_id order by Variance


SQL> select product_type_id,variance(price) as PriceVariance from products group by
product_type_id order by PriceVariance;

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

2. Find details of highest paid employee.


SQL> select * from emp where sal=(select max(sal) from emp);
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7839 KING PRESIDENT 17-NOV-81 5000 10

3. Find the highest paid employee of sales department


SQL> select * from emp where job='SALESMAN' and sal=(select max(sal) from emp where
job='SALESMAN');
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30

4. Find the total sal given to the MGR


SQL> select sum(sal) as total_sal from emp where mgr is not null;
TOTAL_SAL
----------
24025

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

6. Display the average salaries of all the clerks.


SQL> select avg(sal) from emp where job='CLERK';

AVG(SAL)
----------
1037.5

7. List the employeein dept 20 whose sal is &gt;the average sal 0f dept 10 emps
SQL> select * from emp where deptno=20 and sal>(select avg(sal) from emp where
deptno=10);

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO


---------- ---------- --------- ---------- --------- ---------- ---------- ----------
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

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;

DEPTNO JOB NUM_EMP


---------- --------- ----------
10 CLERK 1
10 MANAGER 1
10 PRESIDENT 1
20 ANALYST 2
20 CLERK 2
20 MANAGER 1
30 CLERK 1
30 MANAGER 1
30 SALESMAN 4

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.

10. List departmentwise employee count


SQL> select deptno,count(*) from emp group by deptno;
DEPTNO COUNT(*)
---------- ----------
30 6
20 5
10 3

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
);

ENAME DEPTNO SAL


---------- ---------- ----------
BLAKE 30 2850
SCOTT 20 3000
KING 10 5000
FORD 20 3000
13. List the emps whose sal is greater than or equal to the average of max and minimum
SQL> select * from emp where sal>=(select (max(sal)+min(sal))/2 from emp);

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO


---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7566 JONES MANAGER 7839 02-APR-81 2975 20
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7839 KING PRESIDENT 17-NOV-81 5000 10
7902 FORD ANALYST 7566 03-DEC-81 3000 20

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;

JOB SAL ENAME


--------- ---------- ----------
ANALYST 3000 SCOTT
ANALYST 3000 FORD
CLERK 800 SMITH
MANAGER 2450 CLARK
PRESIDENT 5000 KING
SALESMAN 1250 WARD
SALESMAN 1250 MARTIN
7 rows selected.

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;

JOB SAL ENAME


--------- ---------- ----------
PRESIDENT 5000 KING
ANALYST 3000 SCOTT
ANALYST 3000 FORD
MANAGER 2975 JONES
SALESMAN 1600 ALLEN
CLERK 1300 MILLER
6 rows selected.

19. List the Deptno where there are no emps.


SQL> select d.deptno from dept d le join emp e on d.deptno=e.deptno where e.deptno is
null;

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;

DEPTNO JOB EMP_COUNT AVG_SAL


---------- --------- ---------- ----------
20 CLERK 2 950
30 SALESMAN 4 1400
20 MANAGER 1 2975
30 CLERK 1 950
10 PRESIDENT 1 5000
30 MANAGER 1 2850
10 CLERK 1 1300
10 MANAGER 1 2450
20 ANALYST 2 3000

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

22. List the highest paid emp.


SQL> select * from emp where sal=(select max(sal) from emp);

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO


---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7839 KING PRESIDENT 17-NOV-81 5000 10

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 ;

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO


---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30

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;

EMP_COUNT AVG_SAL TOTAL_SAL


---------- ---------- ----------
14 2073.21429 29025
25. Find the count of employee, average salary and sum of the salary and group by
department number wise in the ascending order
SQL> select deptno,count(*) as emp_count,avg(sal) as avg_sal, sum(sal) as total_sal from
emp group by deptno order by deptno asc;

DEPTNO EMP_COUNT AVG_SAL TOTAL_SAL


---------- ---------- ---------- ----------
10 3 2916.66667 8750
20 5 2175 10875
30 6 1566.66667 9400

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;

DEPTNO JOB EMP_COUNT AVG_SAL TOTAL_SAL


---------- --------- ---------- ---------- ----------
10 CLERK 1 1300 1300
10 MANAGER 1 2450 2450
10 PRESIDENT 1 5000 5000
20 ANALYST 2 3000 6000
20 CLERK 2 950 1900
20 MANAGER 1 2975 2975
30 CLERK 1 950 950
30 MANAGER 1 2850 2850
30 SALESMAN 4 1400 5600
9 rows selected.

You might also like