Using The Group Functions
Using The Group Functions
Aggregate functions perform a variety of actions such as counting all the rows in a table,
averaging a column's data, and summing numeric data. Aggregates can also search a table to find
the highest "MAX" or lowest "MIN" values in a column. As with other types of queries, you can
restrict, or filter out the rows these functions act on with the WHERE clause. For example, if a
manager needs to know how many employees work in an organization, the aggregate function
named COUNT ∗ can be used to produce this information.The COUNT ∗ function shown in the
below SELECT statement counts all rows in a table.
SELECT COUNT(*)
FROM employees;
COUNT(*)
----------
24
The result table for the COUNT ∗ function is a single column from a single row known as a scalar
result or value. Notice that the result table has a column heading that corresponds to the name of
the aggregate function specified in the SELECT clause.
COUNT(*)
MAX(expression)
MIN(expression)
The ALL and DISTINCT keywords are optional, and perform as they do with the SELECT clauses that
you have learned to write.The ALL keyword is the default where the option is allowed.The
expression listed in the syntax can be a constant,a function, or any combination of column names,
constants, and functions connected by arithmetic operators.However, aggregate functions are
most often used with a column name. Except COUNT function,all the aggregate functions consider
NULL values.
There are two rules that you must understand and follow when using aggregates:
Aggregate functions can be used in both the SELECT and HAVING clauses
theHAVINGclauseiscoveredlaterinthischapter.
Aggregate functions cannot be used in a WHERE clause. Its violation will produce the Oracle
ORA-00934 group function is not allowed here error message.
Illustrations
The below SELECT query counts the number of employees in the organization.
COUNT
-----
24
The below SELECT query returns the average of the salaries of employees in the organization.
AVERAGE_SAL
-----------
15694
The below SELECT query returns the sum of the salaries of employees in the organization.
TOTAL_SAL
---------
87472
The below SELECT query returns the oldest and latest hired dates of employees in the
organization.
OLDEST LATEST
--------- -----------
16-JAN-83 01-JUL-2012
GROUP BY
Aggregate functions are normally used in conjunction with a GROUP BY clause. The GROUP BY
clause enables you to use aggregate functions to answer more complex managerial questions
such as:
Group by function establishes data groups based on columns and aggregates the information
within a group only. The grouping criterion is defined by the columns specified in GROUP BY
clause. Following this hierarchy, data is first organized in the groups and then WHERE clause
restricts the rows in each group.
DEPARTMENT_ID,
*
ERROR at line 2:
ORA-00937: not a single-group group function
2 GROUP BY clause does not support the use of column alias, but the actual names.
3 GROUP BY clause can only be used with aggregate functions like SUM, AVG, COUNT, MAX, and
MIN.If it is used with single row functions,Oracle throws and exception as "ORA-00979: not a GROUP
BY expression".
4 Aggregate functions cannot be used in a GROUP BY clause. Oracle will return the "ORA-00934:
group function not allowed" here error message.
Similarly, below query to find sum of salaries for respective job ids in each department. Note the
group is established based on Department and Job id. So they appear in GROUP BY clause.
The below query also produces the same result. Please note that grouping is based on the
department id and job id columns but not used for display purpose.
When all rows for a group are eliminated so is the group.To summarize, the important differences
between the WHERE and HAVING clauses are:
The HAVING clause is a conditional option that is directly related to the GROUP BY clause option
because a HAVING clause eliminates rows from a result table based on the result of a GROUP BY
clause.