# Aggregation FNS
Sum, Count, Min , Max GROUP BY can be used to aggregate data within subsets of the data.
You can GROUP BY multiple columns at once,
As with ORDER BY, you can substitute numbers for column names in the GROUP BY clause.
HAVING is the “clean” way to filter a query that has been aggregated,
# MIN VS MAX VS COUNT
Functionally, MIN and MAX are similar to COUNT in that they can be used on non-numerical columns. Depending on the column type, MIN will return the lowest number, earliest date, or non-numerical value as early in the alphabet as possible. As you might suspect, MAX does the opposite—it returns the highest number, the latest date, or the non-numerical value closest alphabetically to “Z.”
# Distinct
DISTINCT is always used in SELECT statements, and it provides the unique rows for all columns written in the SELECT statement. Therefore, you only use DISTINCT once in any particular SELECT statement.
You could write:
SELECT DISTINCT column1, column2, column3
FROM table1;
which would return the unique (or DISTINCT) rows across all three columns.
You would not write:
SELECT DISTINCT column1, DISTINCT column2, DISTINCT column3
FROM table1;
# Case
ELECT CASE WHEN total >= 2000 THEN 'At Least 2000'
WHEN total >= 1000 AND total < 2000 THEN 'Between 1000 and 2000'
ELSE 'Less than 1000' END AS order_category,
COUNT(*) AS order_count
FROM orders
GROUP BY 1;