You are on page 1of 13

Database Management System Lab

(PCCCS402P)

S. B. JAIN INSTITUTE OF TECHNOLOGY,


MANAGEMENT & RESEARCH, NAGPUR.

Practical No. 5

Aim: Design & Develop a SQL query for the given problems by making
use of aggregate functions, Group by & Having clause.

Name of Student: Achal Jichkar

Roll No.: 105

Semester/Year: 4th/2nd

Academic Session: 2021-2022

Date of Performance:14/05/22

Date of Submission: 21/05/22

1
Database Management System Lab
(PCCCS402P)

2
Database Management System Lab
(PCCCS402P)
AIM: Design & Develop a SQL query for the given problems by making use of aggregate
functions, Group by & Having clause.

Task:
1. Create table emp with Eid, Ename, Cname, City, and Sal.
2. Determine avg salary paid to emp by Infosys.
3. Find total number of employees in Syntel.
4. Display the name of the employee who got the highest salary in the Company.
5. Find total salary paid to emp by Infosys.
6. Display id of emp having lowest amount of salary.
7. Display highest,lowest,avg & total salary paid by company.
8. Find total number of emp living in city pune.
9. Find a total number of emp whose salary is greater than 50000 and the city is pune.
10. Find the second largest salary paid to an employee.
11. Find the fifth lowest salary paid to employees.
12. Find the total number of emp in each company.
13. Find the maximum amount of salary paid by each company.
14. Find the total amount of salary paid by each company.
15. Find avg salary paid by each company.
16. Find company name having avg sal of emp greater than 20000.
17. Find the company name having a total salary paid to each emp is greater than 25000.
18. Display company name whose emp max salary greater than 50000.
19. Find the highest salary paid to an employee without using aggregate function max.

OBJECTIVE/EXPECTED LEARNING OUTCOME:


The objectives and expected learning outcome of this practical are:
• To realize how to retrieve single value output when number of values given as input.
• To know how to find out total number of records, average, max and min value by using
aggregate functions.
• To know how to arrange identical data into groups.
• To interpret the use of having clause.

THEORY:

• SQL has many built-in functions for performing calculations on data out of these one
type is SQL Aggregate Functions.
• SQL aggregate functions return a single value, calculated from values in a column.
• Useful aggregate functions:
· AVG() - Returns the average value
3
Database Management System Lab
(PCCCS402P)
· COUNT() - Returns the number of rows
· MAX() - Returns the largest value
· MIN() - Returns the smallest value
· SUM() - Returns the sum

AVG()
The AVG() function returns the average value of a numeric column.
Syntax
SELECT AVG(column_name) FROM table_name

COUNT ()
Returns the number of rows that matches a specified criteria.
Synatx:
SELECT COUNT(column_name) FROM table_name

Max()
The MAX() function returns the largest value of the selected column.
Syntax
SELECT MAX(column_name) FROM table_name

MIN ()
The MIN() function returns the smallest value of the selected column.
Syntax
SELECT MAX(column_name) FROM table_name

SUM ()
It returns the total sum of a numeric column.
Syntax
SELECT Sum(column_name) FROM table_name

GROUP BY Statement

The GROUP BY statement is used in conjunction with the aggregate functions to group the
result-set by one or more columns.
Syntax
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name

Example:
SELECT Customer,SUM(OrderPrice) FROM Orders
GROUP BY Customer

4
Database Management System Lab
(PCCCS402P)
HAVING Clause

The HAVING clause was added to SQL because the WHERE keyword could not be used with
aggregate functions.
Syntax
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
HAVING aggregate_function(column_name) operator value

CODE:
1)create table emp (Eid int, Ename varchar2(20), Cname varchar2(20), city
varchar2(20), sal number)

O/P: Table created

insert into emp values(101,'Achal','Infosys','Nagpur',55000)


insert into emp values(102,'Sakshi','Syntel','Agra',50000)
insert into emp values(103,'Gaurav','Infosys','Mumbai',30000)
insert into emp values(104,'Yash','Persistence','Mumbai',21000)
insert into emp values(105,'komal','Nestle','Nurpur',32000)
insert into emp values(106,'Aditya','Nippon','Pune',52000)
insert into emp values(107,'Preet','Persistence','Agra',1500)
insert into emp values(108,'Ram','Infosys','Agra',19000)
insert into emp values(109,'Rohit','Syntel','Pune',10000)
insert into emp values(110,'Pagote','Syntel','Mumbai',20000)

O/P: 1 row(s) inserted.

1 row(s) inserted.
1 row(s) inserted.
1 row(s) inserted.
1 row(s) inserted.
5
Database Management System Lab
(PCCCS402P)

1 row(s) inserted.
1 row(s) inserted.
1 row(s) inserted.
1 row(s) inserted.
1 row(s) inserted.

2) select AVG(sal) from emp where Cname='Infosys'


O/P:
AVG(SAL)

34666.6666666666666666666666666666666667

3) select count(Eid) from emp where Cname='Syntel'

O/P:
COUNT(EID)

4) select Ename from emp where sal=(select max(sal) from emp)

O/P:
ENAME

Achal

5) select sum(sal) from emp where Cname='Infosys'

O/P:

SUM(SAL)

104000
6
Database Management System Lab
(PCCCS402P)

6) select Eid from emp where sal=(select min(sal) from emp)

O/P:

EID

107

7) select Cname,max(sal),min(sal),avg(sal),sum(sal) from emp group by


Cname;

O/P:

CNAME MAX(SAL) MIN(SAL) AVG(SAL) SUM(SAL)

Persistenc
21000 1500 11250 22500
e
Nestle 32000 32000 32000 32000
Nippon 52000 52000 52000 52000
34666.666666666666666666666666666
Infosys 55000 19000 104000
6666667
26666.666666666666666666666666666
Syntel 50000 10000 80000
6666667

8) select count(Eid) from emp where city='Pune'

O/P:
COUNT(EID)

9) select count(Eid) from emp where sal>50000 and city='Pune'


7
Database Management System Lab
(PCCCS402P)

O/P:
COUNT(EID)

10) select max(sal) from emp where sal<(select max(sal) from emp)

O/P: MAX(SAL)

52000

11) select distinct sal from emp e1 where(select count(distinct sal) from emp
e2 where e1.sal>=e2.sal)=5

O/P:

SAL

21000

12) select Cname,count(Eid) from emp group by Cname

O/P:

CNAME COUNT(EID)

Persistence 2
Nestle 1
Nippon 1
Infosys 3
Syntel 3

8
Database Management System Lab
(PCCCS402P)

13) select Cname,max(sal) from emp group by Cname

O/P:

CNAME MAX(SAL)

Persistence 21000
Nestle 32000
Nippon 52000
Infosys 55000
Syntel 50000

14) select Cname,sum(sal) from emp group by Cname

O/P:

CNAME SUM(SAL)

Persistence 22500
Nestle 32000
Nippon 52000
Infosys 104000
Syntel 80000

15) select Cname,avg(sal) from emp group by Cname

O/P:
9
Database Management System Lab
(PCCCS402P)

CNAME AVG(SAL)

Persistence 11250
Nestle 32000
Nippon 52000
Infosys 34666.6666666666666666666666666666666667
Syntel 26666.6666666666666666666666666666666667

16) select avg(sal),Cname from emp group by Cname having avg(sal)>20000

O/P:

AVG(SAL) CNAME

32000 Nestle
52000 Nippon
34666.6666666666666666666666666666666667 Infosys
26666.6666666666666666666666666666666667 Syntel

17) select sum(sal),Cname from emp group by Cname having sum(sal)>25000

O/P:
SUM(SAL) CNAME

32000 Nestle
52000 Nippon
104000 Infosys
80000 Syntel

10
Database Management System Lab
(PCCCS402P)

18) select max(sal),Cname from emp group by Cname having max(sal)=50000

O/P:

MAX(SAL) CNAME

50000 Syntel

19) select sal from emp where sal>=all(select sal from emp)

O/P:

SAL

55000

CONCLUSION: Hence we successfully understood how to use the aggregate function


and group by clause,having clause.

DISCUSSION QUESTIONS:
• What is aggregate function?
Ans: In database management an aggregate function is a function where the values of multiple
rows are grouped together as input on certain criteria to form a single value of more significant
meaning.

11
Database Management System Lab
(PCCCS402P)
• How to retrieve total number of records in table?
Ans: The SQL COUNT() function returns the number of rows in a table satisfying the criteria
specified in the WHERE clause. It sets the number of rows or non NULL column values.
COUNT() returns 0 if there were no matching rows.
• What is use of group by clause?
Ans: The GROUP BY Clause is utilized in SQL with the SELECT statement to organize
similar data into groups. It combines the multiple records in single or more columns using some
functions.
• When to use having clause?
Ans: The HAVING clause places the condition in the groups defined by the GROUP BY
clause in the SELECT statement. This SQL clause is implemented after the 'GROUP BY' clause
in the 'SELECT' statement. This clause is used in SQL because we cannot use the WHERE
clause with the SQL aggregate functions.
• Can we use where clause to specify condition with group by clause?
Ans: To specify a WHERE condition in an aggregate query Clear the Output column unless
the data column is part of the GROUP BY clause or included in an aggregate function. In the
Filter column, specify the WHERE condition.

REFERENCE:

• http://sage.virtual-labs.ac.in/home/pub/22
• Database System Concepts by AviSilberschatz , Henry F. Korth , S. Sudarshan, Tata
McGraw Hill, Fifth Edition
• www.w3schools.com
• http://www.nptelvideos.in/2012/11/database-management-system.html

• https://docs.oracle.com/cd/B19188_01/doc/B15917/toc.html.

12
Database Management System Lab
(PCCCS402P)

13

You might also like