GROUP BY with Aggregation Functions
GROUP BY clause can be used in a SELECT statement to collect data across multiple records and group the results by one or more columns.
- SUM
- MIN
- MAX
- COUNT
- GROUP BY
Based on tips from Lesson 6.13 Video: GROUP BY:
- GROUP BY can be used to aggregate data within subsets of the data. For example, grouping for different accounts, different regions, or different sales representatives.
- Any column in the SELECT statement that is not within an aggregator must be in the GROUP BY clause.
- The GROUP BY always goes between WHERE (If exists) and ORDER BY.
- ORDER BY works like SORT in spreadsheet software.
Description
GROUP BY clause can be used in a SELECT statement to collect data across multiple records and group the results by one or more columns.
Syntax
SELECT columns,
aggregate_function (aggregate_expression)
FROM tables
[WHERE conditions]
GROUP BY columns
[ORDER BY column [ ASC | DESC ]];Noting that those square brackets [ ] refers that it is optional to write.
Example - Employees Table
We will use this table to select from.
|employee_number|last_name|first_name|salary|dept_id|
| | | | | |
|1001 |Smith |John |62000 |500 |
|1002 |Anderson |Jane |57500 |500 |
|1003 |Everest |Brad |71000 |501 |
|1004 |Horvath |Jack |42000 |501 |Using GROUP BY with the SUM Function
Query:
SELECT dept_id, SUM(salary) AS total_salaries
FROM employees
GROUP BY dept_id;Result (2 records selected):
|dept_id|total_salaries|
|-------|--------------|
|500 |119500 |
|501 |113000 |In this query, we've used the SUM function to add up all of the salaries for each _dept_id_ and we've aliased the results of the SUM function as _total_salaries_. Because the _dept_id_ is not encapsulated in the SUM function, it must be listed in the GROUP BY clause.
Using GROUP BY with the MIN function
Query:
SELECT dept_id, MIN(salary) AS lowest_salary
FROM employees
GROUP BY dept_id;Result (2 records selected):
|dept_id|lowest_salary|
|-------|-------------|
|500 |57500 |
|501 |42000 |In this query, we've used the MIN function to return the lowest salary for each _dept_id_ and we've aliased the results of the MIN function as _lowest_salary_. Because the _dept_id_ is not encapsulated in the MIN function, it must be listed in the GROUP BY clause.
Using GROUP BY with the MAX function
Query:
SELECT dept_id, MAX(salary) AS highest_salary
FROM employees
GROUP BY dept_id;Result (2 records selected):
|dept_id|highest_salary|
|-------|--------------|
|500 |62000 |
|501 |71000 |In this query, we've used the MAX function to return the highest salary for each _dept_id_ and we've aliased the results of the MAX function as _highest_salary_. The _dept_id_ column must be listed in the GROUP BY clause because it is not encapsulated in the MAX function.
Example - Products Table
We will use this table to select from.
|product_id|product_name|category_id|
|----------|------------|-----------|
|1 |Pear |50 |
|2 |Banana |50 |
|3 |Orange |50 |
|4 |Apple |50 |
|5 |Bread |75 |
|6 |Sliced Ham |25 |
|7 |Kleenex |NULL |Using GROUP BY with the COUNT function
Query:
SELECT category_id, COUNT(*) AS total_products
FROM products
WHERE category_id IS NOT NULL
GROUP BY category_id
ORDER BY category_id;Result (3 records selected):
|category_id|total_products|
|-----------|--------------|
|25 |1 |
|50 |4 |
|75 |1 |In this query, we've used the COUNT function to calculate the number of products for each _category_id_ and we've aliased the results of the COUNT function as total_products. We've excluded any _category_id_ values that are NULL by filtering them out in the WHERE clause. Because the _category_id_ is not encapsulated in the COUNT function, it must be listed in the GROUP BY clause.