Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

SQL-04: Group By & Aggregation Fns

Sunday, August 21, 2022 5:57 PM

• Group by internally generates key, value pairs


• When we do:
SELECT
cust_id, SUM(marks)
FROM customers
GROUP BY cust_id

It creates various groups with key as customer_id for each group and value is the set of all rows that have customer_id = that
cust_id, we can apply any aggregate function (like SUM, MIN, MAX, COUNT, AVG etc.) on that group and get the output

UNIQUE VALUE ---> Key


All the associated rows with that key ---> Value

• Aggregate function is always applied on the VALUE part


• When we do:

select
<selected_columns>,
agg(colD), agg(colE)
from
tbl
GROUP BY
<group_by_cols>

Here, <selected_columns> need to be unique for each group


That is, <selected_columns> should be a subset of <group_by_cols>

However, agg() can be applied on any column

[MySQL doesn't throw an error even when <selected_columns> is not a subset of <group_by_cols>]
It shows the data corresponding to the first row of that column
But it's not accurate

• WHERE condition is applicable only on rows before grouping


• HAVING is usually applied to filter using aggregates generated by GROUP BY but it also works as an alternate to WHERE

You might also like