Oracle 19c SQL

  1. Home
  2. Docs
  3. Oracle 19c SQL
  4. 2 Parts of Oracle SQL Query
  5. 2.6 GROUP BY Clause

2.6 GROUP BY Clause

A GROUP BY clause is part of a Select Expression, groups rows into subsets that have matching values for one or more columns. In each group, no two rows have the same value for the grouping column or columns.

SELECT Deptno FROM Emp

    DEPTNO
----------
        10
        30
        10
        20
        20
        20
        20
        30
        30
        30
        30
        20
        30
        10

14 rows selected.
SELECT Deptno FROM Emp GROUP BY Deptno;

    DEPTNO
----------
	30
	20
	10

A GROUP BY clause in most cases is used along with aggregate functions. Using GROUP BY without aggregate functions gives the distinct rows.

SELECT Job FROM Emp GROUP BY Job;

JOB
---------
CLERK
SALESMAN
PRESIDENT
MANAGER
ANALYST

NULLs are considered equivalent for grouping purposes.

SELECT Comm FROM Emp GROUP BY Comm;

      COMM
----------

      1400
       500
       300
         0

The GROUP BY clause groups rows but does not guarantee the order of the result set. To order the groupings, use the ORDER BY clause.

SELECT Comm FROM Emp GROUP BY Comm ORDER BY Comm;

    DEPTNO
----------
	10
	20
	30

Was this article helpful to you? Yes No

How can we help?