Oracle / PLSQL: GROUP BY Clause
This Oracle tutorial explains how to use the Oracle GROUP BY clause with syntax and examples.
The Oracle GROUP BY clause is used in a SELECT statement to collect data across multiple records and group the results by one or more columns.
The syntax for the GROUP BY clause in Oracle/PLSQL is:
Example – Using SUM function
Let’s look at an Oracle GROUP BY query example that uses the SUM function.
This Oracle GROUP BY example uses the SUM function to return the name of the product and the total sales (for the product).
SELECT product, SUM(sale) AS "Total sales" FROM order_details GROUP BY product;
Because you have listed one column (the product field) in your SELECT statement that is not encapsulated in the SUM function, you must use the GROUP BY clause. The product field must, therefore, be listed in the GROUP BY clause.