Member-only story
Intermediate SQL Topics-Part 2
SQL Group BY and HAVING
SQL GROUP BY?
GROUP BY is used in combination with aggregate functions like SUM(), COUNT(), AVG(), MIN(), and MAX() to group rows with the same values in specified columns into summary rows.
🧠 Syntax:
SELECT
column1,
column2,
aggregate_function (column3)
FROM
table_name
GROUP BY
column1,
column2;The
GROUP BYis an optional clause of theSELECTstatement. TheGROUP BYclause allows you to group rows based on values of one or more columns. It returns one row for each group.
The table on the left side has two columns id and fruit. When you apply the GROUP BY clause to the fruit column, it returns the result set that includes unique values from the fruit column:
SELECT
fruit
FROM
sample_table
GROUP BY
fruit;In practice, you often use the GROUP BY clause with an aggregate function such as MIN, MAX, AVG, SUM, or COUNT to calculate a measure that provides the information for each group.








