Database Management Concepts Lab Course Code: CSE 704: Group Functions
Database Management Concepts Lab Course Code: CSE 704: Group Functions
Lecture 6
Topic
Group Functions
GROUP Functions
• In SQL, the following group functions can operate on a
whole table or on a specific grouping of rows. Each
function returns one result.
• Group Functions:
– AVG
– COUNT
– MIN
– MAX
– SUM
– VARIANCE
– STDDEV
GROUP Functions List
• MIN: Used with columns that store any DEPT_ID SALARY SELECT MAX(salary)
FROM employees;
90 24000
data type to return the minimum value. 90 17000
90 17000
• MAX: Used with columns that store any 60 9000
data type to return the maximum value. 60 6000
60 4200
• SUM: Used with columns that store 50 5800
MAX (SALARY)
50 3500
numeric data to find the total or sum of 50 3100 24000
values. 50 2600
50 2500
• AVG: Used with columns that store … …
numeric data to compute the average. 60 11000
60 8600
7000
SQL> SELECT dept, AVG(NVL(sal,0))
10 4400
FROM employee;
GROUP Functions List
• COUNT: Returns the number of rows.
• VARIANCE: Used with columns that store numeric data to
calculate the spread of data around the mean. For example, if the
average grade for the class on the last test was 82% and the
student's scores ranged from 40% to 100%, the variance of scores
would be greater than if the student's scores ranged from 78% to
88%.
• STDDEV: Similar to variance, standard deviation measures the
spread of data. For two sets of data with approximately the same
mean, the greater the spread, the greater the standard deviation
GROUP Functions SELECT Clause
DEPT_ID SALARY
• Group functions are written 90 24000
90 17000 The minimum salary in
in the SELECT clause: 90 17000 the EMPLOYEES
60 9000 table
SELECT column, group_function(column),
.. 60 6000
FROM table 60 4200
WHERE condition 50 5800
GROUP BY column; 50 3500 MIN
(SALARY)
50 3100
2500
• What are Group Functions? 50
50
2600
2500
AVG(COMMISSION_PCT) Zlotkey .2
Abel .3
.2125
Taylor .2
Grant .15
Mourgos -
… …
More Than One Group Function
• You can have more than one group function in the
SELECT clause, on the same or different columns.
SELECT MAX(salary), MIN(salary), MIN(employee_id)
FROM employees
WHERE department_id = 60;