Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any reason for GROUP BY clause without aggregation function?

Tags:

I'm (thoroughly) learning SQL at the moment and came across the GROUP BYclause.

GROUP BY aggregates or groups the resultset according to the argument(s) you give it. If you use this clause in a query you can then perform aggregate functions on the resultset to find statistical information on the resultset like finding averages (AVG()) or frequency (COUNT()).

My question is: is the GROUP BY statement in any way useful without an accompanying aggregate function?

Update Using GROUP BY as a synonym for DISTINCT is (probably) a bad idea because I suspect it is slower.

like image 383
Niels Bom Avatar asked Feb 04 '10 06:02

Niels Bom


People also ask

Can we use GROUP BY clause without aggregate function?

You can use the GROUP BY clause without applying an aggregate function.

Is aggregate function necessary with GROUP BY?

If you don't specify GROUP BY , aggregate functions operate over all the records selected. In that case, it doesn't make sense to also select a specific column like EmployeeID .

Can we use GROUP BY clause without aggregate in Oracle?

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, then Oracle applies aggregate functions in the select list to all the rows in the queried table or view.


2 Answers

is the GROUP BY statement in any way useful without an accompanying aggregate function?

Using DISTINCT would be a synonym in such a situation, but the reason you'd want/have to define a GROUP BY clause would be in order to be able to define HAVING clause details.

If you need to define a HAVING clause, you have to define a GROUP BY - you can't do it in conjunction with DISTINCT.

like image 158
OMG Ponies Avatar answered Oct 22 '22 04:10

OMG Ponies


You can perform a DISTINCT select by using a GROUP BY without any AGGREGATES.

like image 43
Adriaan Stander Avatar answered Oct 22 '22 02:10

Adriaan Stander