In this medium post, we aim to discuss grouping data using SQL.
Learning Objectives:
- Perform additional aggregations using GROUP BY and HAVING.
- Discuss how NULL values aren't affected by GROUP BY and HAVING.
- Use the GROUP BY and ORDER BY clauses to sort data.
Grouping Example.
Here is a query that groups by region.
SELECT region
COUNT(customer_id) AS total_customers
FROM customers
GROUP BY region;
This would display customer_id by region. We can also group it by multiple columns.
This is done by simply adding a column name after the first declared column.
Here is an example:
SELECT region
COUNT(customer_id) AS total_customers
FROM customers
GROUP BY region, language;
Here are a few things to be careful of when using ‘GROUP BY’.
You should include every column in your ‘SELECT’ statement present in a ‘GROUP BY’ clause excluding aggregated calculations.
‘NULL’ values will be grouped if your ‘GROUP BY contains ‘NULL’ values.
HAVING Clause — Filtering for Groups.
You should bear in mind that ‘WHERE’ does not work for groups since ‘WHERE’ filters on rows. You can use ‘HAVING’ to filter based on groups.
Here is an example:
SELECT customer_id
COUNT(*) AS orders
FROM orders
GROUP BY customer_id;
HAVINT COUNT (*) >= 2
WHERE vs. HAVING.
- WHERE filters before data is grouped.
- HAVING filters after the data is grouped.
- Rows eliminated by the WHERE clause will not be included in the group.
ORDER BY with GROUP BY.
- ORDER BY sorts data.
- GROUP BY does not sort data.