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