Select Statement  «Prev  Next»

Lesson 5 Group operators
Objective Describe group operators and what they do.

Group Operators in Oracle: Function and Purpose

In Oracle 19c, as with earlier versions of Oracle Database, group operators are SQL functions that operate on a group of rows to return a single result per group. These functions are essential in aggregate data analysis, providing insights into data sets by summarizing multiple rows of information. Here's a breakdown of their function and purpose:
  1. Aggregation of Data: Group operators aggregate data from multiple rows to provide a summary result. This is particularly useful in reporting and data analysis where summarizing data, such as calculating totals or averages, is required.
  2. Types of Group Operators:
    • AVG(): Calculates the average value of a numeric column.
    • COUNT(): Returns the number of rows in a group, including or excluding NULLs based on the expression used.
    • MAX(): Finds the maximum value in a set of values.
    • MIN(): Finds the minimum value in a set of values.
    • SUM(): Calculates the sum of values.
    • GROUP_CONCAT(): (In MySQL, and similar in Oracle as LISTAGG()) Compiles data from multiple rows into a single concatenated string, often used for displaying data from multiple rows in a single line.
  3. Use in GROUP BY Clauses: Group operators are often used in conjunction with the `GROUP BY` clause in SQL queries. The `GROUP BY` clause groups rows that have the same values in specified columns into summary rows. Group operators can then perform operations on each of these groups.
  4. Enhancing Data Insights: By summarizing data, group operators allow for a more straightforward interpretation of large datasets. For example, finding the average sales amount per region or the maximum salary in a department.
  5. Data Filtering with HAVING Clause: After grouping data, the `HAVING` clause is used to filter groups based on the result of aggregate functions. Unlike the `WHERE` clause, which filters rows before grouping, `HAVING` filters after rows have been grouped.
  6. Windowing Functions: While not traditional group operators, window functions (introduced in Oracle 11g) allow for performing calculations across sets of rows related to the current row. They offer more complex analytical operations without collapsing rows into a single output, providing a sort of "group" operation while maintaining individual row details.
  7. Performance Considerations: Using group operators can impact query performance, especially on large datasets. Efficient use of indexes, partitioning, and considering the execution plan can help mitigate performance impacts.

Group operators in Oracle 19c are powerful tools for data summarization and analysis, enabling complex queries and data interpretation that support business decisions and reporting.

Group Functions

When you combine rows of information together and display summarized data, you are performing group functions on the data. The table below shows you the group functions that can be used in Oracle:

Group function Description
SUM()Adds up a total amount for the column or expression within the parentheses
MAX()Finds the maximum value for the column or expression within the parentheses
MIN()Finds the minimum value of the column or expression within the parentheses
AVG()Finds the average value (ignoring null values)
COUNT()Finds the number of rows in which the column being counted is not null
STDDEV()Calculates the standard deviation of values
VARIANCE()Calculates the variance of values

Here is a typical query that adds up sales by customer:
SELECT FIRSTNAME, LASTNAME, SUM(TOTAL_SALE_AMOUNT)
FROM CUSTOMER C, CUSTOMER_SALE CS
WHERE C.CUST_ID = CS.CUST_ID
GROUP BY FIRSTNAME, LASTNAME
ORDER BY FIRSTNAME, LASTNAME

The GROUP BY clause determines how the SUM function groups the query rows when it adds them up. In the example, each uniquecombination of FIRSTNAME and LASTNAME has a summary of the TOTAL_SALES_AMOUNT column. While GROUP BY is not required, it is a common ingredient in queries that use group functions. [1]


Whenever you use the GROUP BY clause, add a corresponding ORDER BY clause so that all rows are evaluated and grouped properly.

DISTINCT in Group Functions

All group-value functions have a DISTINCT versus ALL option. COUNT provides a good example of how this works. Here is the format for COUNT (note that | means or):
COUNT([DISTINCT | ALL] value)

Here is an example:
select COUNT(DISTINCT City), COUNT(City), COUNT(*)
from COMFORT;
COUNT(DISTINCTCITY) COUNT(CITY) COUNT(*)
------------------- ----------- --------
2                       8           8

This query shows a couple of interesting results. First, DISTINCT forces COUNT to count only the number of unique city names. If asked to count the DISTINCT midnight temperatures, it would return 7, because two of the eight temperatures were the same.
When COUNT is used on City but not forced to look at DISTINCT cities, it finds 8. This also shows that COUNT can work on a character column. It is not making a computation on the values in the column, as SUM or AVG must; it is merely counting how many rows have a value in the specified column. COUNT has another unique property: value can be an asterisk, meaning that COUNT tells you how many rows are in the table, regardless of whether any specific columns are NULL. It will count a row even if all its fields are NULL.
The other group functions do not share COUNT's ability to use an asterisk, nor its ability to use a character column for value (although MAX and MIN can). They do all share its use of DISTINCT, which forces each of them to operate only on unique values.
The next lesson describes three group operators you can use when working with groups of data.

[1] Group function: A group function is a predefined way to combine rows of information together and display summarized data. A common group function is the SUM function, which adds values into a total sum.

SEMrush Software