Professional Documents
Culture Documents
(PCCCS402P)
Practical No. 5
Aim: Design & Develop a SQL query for the given problems by making
use of aggregate functions, Group by & Having clause.
Semester/Year: 4th/2nd
Date of Performance:14/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.
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)
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.
34666.6666666666666666666666666666666667
O/P:
COUNT(EID)
O/P:
ENAME
Achal
O/P:
SUM(SAL)
104000
6
Database Management System Lab
(PCCCS402P)
O/P:
EID
107
O/P:
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
O/P:
COUNT(EID)
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
O/P:
CNAME COUNT(EID)
Persistence 2
Nestle 1
Nippon 1
Infosys 3
Syntel 3
8
Database Management System Lab
(PCCCS402P)
O/P:
CNAME MAX(SAL)
Persistence 21000
Nestle 32000
Nippon 52000
Infosys 55000
Syntel 50000
O/P:
CNAME SUM(SAL)
Persistence 22500
Nestle 32000
Nippon 52000
Infosys 104000
Syntel 80000
O/P:
9
Database Management System Lab
(PCCCS402P)
CNAME AVG(SAL)
Persistence 11250
Nestle 32000
Nippon 52000
Infosys 34666.6666666666666666666666666666666667
Syntel 26666.6666666666666666666666666666666667
O/P:
AVG(SAL) CNAME
32000 Nestle
52000 Nippon
34666.6666666666666666666666666666666667 Infosys
26666.6666666666666666666666666666666667 Syntel
O/P:
SUM(SAL) CNAME
32000 Nestle
52000 Nippon
104000 Infosys
80000 Syntel
10
Database Management System Lab
(PCCCS402P)
O/P:
MAX(SAL) CNAME
50000 Syntel
19) select sal from emp where sal>=all(select sal from emp)
O/P:
SAL
55000
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