Quick Search:
 
 The Oracle PL/SQL GROUP BY Clause      [Return To Index] Jump to:  

Term: GROUP BY

Definition:
The Oracle PL/SQL GROUP BY clause enables establishing data groups based on columns. The grouping criterion is defined by the GROUP BY clause, which follows the WHERE clause in the SQL clause hierarchy. Following this hierarchy, result set rows are grouped based on like values of grouping columns and the WHERE clause restricts the entries in each group.

Example Syntax:

SELECT 
<COLUMN LIST>, <GROUP BY FUNCTIONS>
FROM <TABLE_NAME>
WHERE <FILTER CONDITIONS>
GROUP BY <COLUMN_LIST>
HAVING <CONDITION BASED ON GROUP BY FUNCTIONS>


Notes:
  1. All the columns used besides the aggregate functions must be included in the GROUP BY clause.
  2. The GROUP BY clause does not support the use of column aliases- you must use the actual column names.
  3. The GROUP BY columns may or may not appear in the SELECT list.
  4. The 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, the Oracle error message appears as "ORA-00979: not a GROUP BY expression".

Example Usage:

The SQL query below shows the sum of SALARY within the group formed by a department and job ID (i.e. for respective job IDs in each department).

SELECT DEPTNO, JOB_ID, SUM (SALARY)
FROM EMPLOYEE
GROUP BY DEPTNO JOB_ID


Related Links:

Related Code Snippets:
 
   Home |    Search |    Code Library |    Sponsors |    Privacy |    Terms of Use |    Contact Us © 2003 - 2024 psoug.org