ORACLE's definition of GROUP BY:
=========================================================
Aggregate functions return a single result row based on groups of rows, rather than on single rows. Aggregate functions can appear in select lists and in ORDER BY and HAVING clauses. They are commonly used with the GROUP BY clause in a SELECT statement, where Oracle divides the rows of a queried table or view into groups. In a query containing a GROUP BY clause, the elements of the select list can be aggregate functions, GROUP BY expressions, constants, or expressions involving one of these. Oracle applies the aggregate functions to each group of rows and returns a single result row for each group.
If you omit the GROUP BY clause, Oracle applies aggregate functions in the select list to all the rows in the queried table or view. You use aggregate functions in the HAVING clause to eliminate groups from the output based on the results of the aggregate functions, rather than on the values of the individual rows of the queried table or view.
=========================================================
If you have a column in the SELECT list along with aggregate functions, that is a GROUP BY expression and it must be included in the GROUP BY at the bottom.
select job, avg(sal)
from emp
group by job
select job, mgr, avg(sal)
from emp
group by job,mgr
You can also group by values from the table being selected,even if they are not in the SELECT statement:
# Show average salary by dept without showing dept
select avg(sal)
from emp
group by job;
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.