Aggregate Functions
Aggregate
A function that summarizes data from multiple rows into a single result.
- Ex) calculates total number of members, the average age, or the total order amount
Representative Aggregate Functions
| Function | Description | Example Use Case |
|---|---|---|
COUNT() | Counts the number of rows. | Total members, number of product types, etc. |
SUM() | The total sum of numbers. | Total payment amount, total order quantity, etc. |
AVG() | The average value. | Average age, average purchase amount, etc. |
MAX() | The maximum value. | Highest price, most recent date, etc. |
MIN() | The minimum value. | Lowest age, earliest sign-up date, etc. |
| Example |
-- 전체 회원 수 조회
SELECT COUNT(*) FROM members;
-- 평균 나이 조회
SELECT AVG(age) FROM members;
-- 가장 어린 회원의 나이
SELECT MIN(age) FROM members;GROUP BY
GROUP BYis a clause that groups rows based on a specific column, allowing aggregate functions to be applied to each group.
-- Query the number of members for each age
SELECT
age,
COUNT(*) AS count_by_age
FROM
members
GROUP BY
age;- It groups rows from the
memberstable that have the sameagevalue. - It then counts the number of people within each group (by age).
Example result
| age | cnt |
|---|---|
| 14 | 1 |
| 18 | 1 |
| 19 | 1 |
| 22 | 2 |
| 33 | 1 |
GROUP BY + Multiple Aggregate Functions
SELECT
age,
COUNT(*) AS total_members,
AVG(age) AS avg_age,
MAX(registered_at) AS latest_reg,
MIN(registered_at) AS earliest_reg
FROM
members
GROUP BY
age;- You only need to write
GROUP BYonce, and you can use multiple aggregate functions at the same time. - Using
MAX()andMIN()together allows you to easily check date ranges or numerical ranges. GROUP BY age- finds all members who have the same age and puts them into separate groups.
- Ex) a pile for all the 25-year-olds, another pile for all the 26-year-olds, etc.
SELECT ...: Finally, it calculates the following information for each of those age piles:age: The age that defines the group (e.g., 25).COUNT(*): Counts how many members are in that group.AVG(age): Calculates the average age of the group (which will simply be the group’s age, like 25).MAX(registered_at): Finds the most recent (latest) registration date from all the members within that group.MIN(registered_at): Finds the earliest registration date from all the members within that group.
GROUP BY + ORDER BY
- sorts the number of members per age group in descending order, showing the most populous age group first.
SELECT
age,
COUNT(*) AS cnt
FROM
members
GROUP BY
age
ORDER BY
cnt DESC;- The
ORDER BYclause is used to sort the results of the aggregation. - You can sort based on the aggregated column (e.g.,
cnt) or the grouping column (e.g.,age).
HAVING
This query finds a list of member IDs that have placed 2 or more orders.
SELECT
member_id,
COUNT(*) AS order_count
FROM
orders
GROUP BY
member_id
HAVING
COUNT(*) >= 2;WHEREfilters rows before aggregation, whileHAVINGfilters groups after aggregation.- In practice,
HAVINGis often used when you need to filter based on statistics. WHERE vs HAVINGWHEREis more efficient because it narrows down the scope first, and then the aggregation happens.
- When You Must Use
HAVING- when your filter condition depends on the result of an aggregate function
- ex) The condition “have placed 2 or more orders” (
COUNT(*) >= 2) can only be checked after the orders have been counted for each member.
Tip
- Unlike a normal
SELECT, when you useGROUP BY, every column in theSELECTlist must either be an aggregate function (likeCOUNT()) or one of the columns you are grouping by. - Remember the order:
WHEREfilters data before grouping, andHAVINGfilters the final grouped results after aggregation. COUNT(*)andCOUNT(column)work differently.COUNT(column)excludesNULLvalues, whileCOUNT(*)counts all rows.- If the
GROUP BYcolumn containsNULLvalues,NULLs are treated as their own single group. - When applying
GROUP BYafter aJOIN, be careful to avoid unintended duplicates in the join result that could skew your aggregate calculations.