COLUMN/AGGREGATE
FUNCTIONS
By
Neil A. Basabe
COLUMN FUNCTIONS
• Column functions are used to summarize
columns or expressions.
09/30/2020 Prepared by: SB Satorre 2
COLUMN FUNCTIONS - 1
1. SUM - returns the total value of the values in a given column|
expression.
SUM(column-name|expression)
2. AVG - retruns the average value of the values in a given
column|expression.
AVG(column-name|expression)
3. MIN - returns the smallest value of the values in a given
column|expression.
MIN(column-name|expression)
09/30/2020 Prepared by: SB Satorre 3
COLUMN FUNCTIONS - 2
4. MAX - returns the largest value of the values in a given
column|expression.
MAX(column-name|expression)
5. COUNT(*) - returns the number of the rows in a given
column.
6. COUNT DISTINCT - returns the number of unique non-
null rows of a column.
COUNT (DISTINCT column-name)
09/30/2020 Prepared by: SB Satorre 4
EXAMPLE
SELECT SUM(SALARY) AS "TOTAL-EMP-SALARY",
AVG(SALARY) AS "AVERAGE-EMP-SALARY",
MIN(SALARY) AS "SMALLEST-EMP-SALARY",
MAX(SALARY) AS "LARGEST-EMP-SALARY",
COUNT(*) AS "NO. OF EMPLOYEES",
COUNT (DISTINCT WORKDEPT) AS "NO. OF DEPARTMENTS"
FROM EMPLOYEE
09/30/2020 Prepared by: SB Satorre 5
THE COMPLETE SQL SELECT STATEMENT
SELECT . . .
FROM . . .
[WHERE . . .]
[GROUP BY . . .]
[HAVING . . .]
[ORDER BY . . .]
[FETCH FIRST . . . ONLY]
09/30/2020 Prepared by: SB Satorre 6
GROUP BY
The GROUP BY clause tells DBM which rows of
information are to be grouped together based
upon values in a column(s).
Columns with the same values are group
together as one row or record.
09/30/2020 Prepared by: SB Satorre 7
HAVING
The HAVING clause tells DBM which group(s) of
information are to be returned in the cursor
based upon some conditions.
The grouped rows that satisfy the condition are
returned in the cursor.
09/30/2020 Prepared by: SB Satorre 8
CAUTION!
GROUP BY and/or HAVING
Only applies to queries with COLUMN
FUNCTIONS in the SELECT clause
09/30/2020 Prepared by: SB Satorre 9
Laboratory Exercises