ADVERTISEMENT
Google AdSense Slot (headerLeaderboard)

Set your Publisher ID in src/config/ads.ts to display live ads.

SQL Database Tutorial• 5 min read

SQL Aggregations & GROUP BY

Summarizing data with COUNT, SUM, AVG, and GROUP BY

An aggregate function performs a calculation on a set of values and returns a single value (such as COUNT, SUM, AVG, MIN, and MAX).

GROUP BY Syntax and Purpose

The GROUP BY statement groups rows that have the same values into summary rows.

It is often used with aggregate functions (COUNT(), MAX(), MIN(), SUM(), AVG()) to group the result-set by one or more columns.

Use HAVING instead of WHERE when filtering based on aggregate values.

Customer Counts by Country
SELECT Country, COUNT(CustomerID) AS TotalCustomers
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) >= 5
ORDER BY TotalCustomers DESC;
ADVERTISEMENT
Google AdSense Slot (inContentBanner)

Set your Publisher ID in src/config/ads.ts to display live ads.

Test Yourself With Exercise

+15 XP

Which clause is used to filter results based on aggregate calculations (since WHERE cannot be used with aggregate functions)?