Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does GROUP BY work?

Tags:

sql

oracle

Suppose I have a table Tab1 with attributes - a1, a2, ... etc. None of the attributes are unique.

What will be the nature of the following query? Will it return a single row always?

SELECT a1, a2, sum(a3) FROM Tab1 GROUP BY a1, a2
like image 822
AppleGrew Avatar asked Sep 15 '11 16:09

AppleGrew


People also ask

How does the GROUP BY work?

The GROUP BY statement groups rows that have the same values into summary rows, like "find the number of customers in each country". The GROUP BY statement is often used with aggregate functions ( COUNT() , MAX() , MIN() , SUM() , AVG() ) to group the result-set by one or more columns.

Does GROUP BY come before ORDER BY?

Using Group By and Order By Together GROUP BY goes before the ORDER BY statement because the latter operates on the final result of the query.

Does GROUP BY automatically order?

group by does not order the data neccessarily. A DB is designed to grab the data as fast as possible and only sort if necessary. So add the order by if you need a guaranteed order.


4 Answers

GROUP BY returns a single row for each unique combination of the GROUP BY fields. So in your example, every distinct combination of (a1, a2) occurring in rows of Tab1 results in a row in the query representing the group of rows with the given combination of group by field values . Aggregate functions like SUM() are computed over the members of each group.

like image 200
Mike Sokolov Avatar answered Oct 02 '22 12:10

Mike Sokolov


GROUP BY returns one row for each unique combination of fields in the GROUP BY clause. To ensure only one row, you would have to use an aggregate function - COUNT, SUM, MAX - without a GROUP BY clause.

like image 31
Mark Sherretta Avatar answered Oct 02 '22 10:10

Mark Sherretta


Analogously, not technically, to keep in mind its logic, it can be thought each grouped field having some rows is put per different table, then the aggregate function carries on the tables individually.


Ben Forta conspicuously states the following saying.

The GROUP BY clause instructs the DBMS to group the data and then perform the aggregate (function) on each group rather than on the entire result set.

Aside from the aggregate calculation statements, every column in your SELECT statement must be present in the GROUP BY clause.

The GROUP BY clause must come after any WHERE clause and before any ORDER BY clause.

My understanding reminiscent of his saying is the following.

As is DISTINCT keyword, each field specified through GROUP BY is thought as grouped and made unique at the end of the day. The aggregate function is carried out over each group, as happened in SuL's answer.

like image 27
snr Avatar answered Oct 02 '22 11:10

snr


GROUP BY groups all the identical records.

SELECT COUNT(ItemID), City
FROM Orders
GROUP BY City;

----------------------------------------
13  Sacrmento
23  Dallas
87  Los Angeles
5   Phoenix

If you don't group by City it will just display the total count of ItemID.

like image 34
SuL Avatar answered Oct 02 '22 10:10

SuL