SQL GROUP BY & Aggregate Functions Explained Simply

SQL GROUP BY & Aggregate Functions Explained Simply

Meta Title: SQL GROUP BY & Aggregate Functions — Beginner’s Guide With Examples
Meta Description: Learn how SQL GROUP BY and aggregate functions like COUNT, SUM, AVG work — with plain-English explanations and real examples. Perfect for beginners.
Target Keyword: SQL GROUP BY
Secondary Keywords: SQL aggregate functions, SQL COUNT SUM AVG, GROUP BY explained, SQL GROUP BY examples, SQL HAVING clause


You’ve learned how to fetch rows, filter them, and join tables together.

But what if you don’t want individual rows — you want summaries?

  • How many orders did each customer place?
  • What’s the total revenue per country?
  • What’s the average salary by department?
  • Which product category has the most items?

This is where GROUP BY and aggregate functions come in. They’re the tools that turn raw data into real insights — and they’re what data analysts use every single day.

Let’s break them down simply.


What Are Aggregate Functions?

An aggregate function takes a group of values and returns a single result.

Think of it like a calculator that works on an entire column at once instead of one cell at a time.

SQL has five core aggregate functions:

FunctionWhat it doesExample result
COUNT()Counts the number of rows142 orders
SUM()Adds up all values$8,450 total revenue
AVG()Calculates the average$59.50 average order
MIN()Finds the smallest value$5.00 cheapest order
MAX()Finds the largest value$299.00 most expensive order

Let’s see each one in action.


The Data We’ll Use

Throughout this guide we’ll use a simple orders table from an online store:

idcustomercountryproductamount
1SaraEgyptSQL Handbook19
2JohnUKSQL Handbook19
3SaraEgyptDB Cheat Sheet9
4MeiChinaSQL Course49
5CarlosMexicoSQL Handbook19
6JohnUKSQL Course49
7AishaEgyptDB Cheat Sheet9
8MeiChinaSQL Handbook19

COUNT — How Many Rows?

COUNT counts rows. It’s the most used aggregate function.

How many total orders do we have?

SELECT COUNT(*) FROM orders;

Result: 8

The * means “count all rows regardless of content.” You can also count a specific column — which skips NULL values:

SELECT COUNT(amount) FROM orders;

Count orders per country:

SELECT country, COUNT(*) AS total_orders
FROM orders
GROUP BY country;

Result:

countrytotal_orders
Egypt3
UK2
China2
Mexico1

This is the real power — COUNT alone gives you one number, but paired with GROUP BY it breaks that number down by category.


SUM — What’s the Total?

SUM adds up all values in a column.

What’s the total revenue across all orders?

SELECT SUM(amount) AS total_revenue FROM orders;

Result: 192

Total revenue per country:

SELECT country, SUM(amount) AS total_revenue
FROM orders
GROUP BY country
ORDER BY total_revenue DESC;

Result:

countrytotal_revenue
China68
Egypt37
UK68
Mexico19

AVG — What’s the Average?

AVG calculates the mean value of a column.

What’s the average order amount?

SELECT AVG(amount) AS avg_order FROM orders;

Result: 24.00

Average order amount per country:

SELECT country, AVG(amount) AS avg_order
FROM orders
GROUP BY country;

Result:

countryavg_order
Egypt12.33
UK34.00
China34.00
Mexico19.00

MIN and MAX — Smallest and Largest

MIN finds the lowest value. MAX finds the highest.

What’s the cheapest and most expensive order?

SELECT MIN(amount) AS cheapest, MAX(amount) AS most_expensive
FROM orders;

Result:

cheapestmost_expensive
949

Most expensive order per country:

SELECT country, MAX(amount) AS top_order
FROM orders
GROUP BY country;

Result:

countrytop_order
Egypt19
UK49
China49
Mexico19

GROUP BY — The Engine Behind It All

You’ve been using GROUP BY throughout this article. Now let’s understand exactly what it does.

GROUP BY groups rows that share the same value in a column — then aggregate functions run on each group separately.

Without GROUP BY, SUM(amount) adds up everything into one number. With GROUP BY country, it adds up the amounts within each country separately.

The basic pattern is always:

SELECT group_column, AGGREGATE(value_column)
FROM table
GROUP BY group_column;

You can group by multiple columns too:

SELECT country, product, COUNT(*) AS times_sold
FROM orders
GROUP BY country, product
ORDER BY country;

Result:

countryproducttimes_sold
ChinaSQL Course1
ChinaSQL Handbook1
EgyptDB Cheat Sheet2
EgyptSQL Handbook1
MexicoSQL Handbook1
UKSQL Course1
UKSQL Handbook1

Now you can see exactly which product sells in which country.


Combining Multiple Aggregate Functions

You’re not limited to one aggregate function per query. Use as many as you need:

SELECT
  country,
  COUNT(*) AS total_orders,
  SUM(amount) AS total_revenue,
  AVG(amount) AS avg_order,
  MAX(amount) AS biggest_order
FROM orders
GROUP BY country
ORDER BY total_revenue DESC;

Result:

countrytotal_orderstotal_revenueavg_orderbiggest_order
China26834.0049
UK26834.0049
Egypt33712.3319
Mexico11919.0019

One query. A complete business summary by country. This is exactly the kind of report that data analysts build every day.


HAVING — Filtering After Grouping

You already know WHERE — it filters rows before grouping.

But what if you want to filter after grouping? For example: show only countries with more than 1 order?

That’s what HAVING is for.

SELECT country, COUNT(*) AS total_orders
FROM orders
GROUP BY country
HAVING COUNT(*) > 1;

Result:

countrytotal_orders
Egypt3
UK2
China2

Mexico disappears — it only has 1 order.

WHERE vs HAVING — the simple rule:

  • WHERE filters individual rows before grouping
  • HAVING filters groups after grouping

You can use both in the same query:

SELECT country, SUM(amount) AS total_revenue
FROM orders
WHERE product != 'DB Cheat Sheet'
GROUP BY country
HAVING SUM(amount) > 20
ORDER BY total_revenue DESC;

This query:

  1. Excludes “DB Cheat Sheet” orders (WHERE)
  2. Groups remaining orders by country (GROUP BY)
  3. Keeps only countries with revenue over $20 (HAVING)
  4. Sorts by revenue highest to lowest (ORDER BY)

The ORDER of Clauses in SQL

One thing that trips up beginners: SQL clauses must appear in a specific order. Here’s the full sequence:

SELECT   -- what columns to show
FROM     -- which table
WHERE    -- filter rows (before grouping)
GROUP BY -- group the rows
HAVING   -- filter groups (after grouping)
ORDER BY -- sort the results
LIMIT    -- how many rows to return

You don’t need all of them every time — but when you use them, they must be in this order.


A Real-World Practice Exercise

Try this in SQLiteOnline.com (free, no signup):

Create this table and data:

CREATE TABLE sales (
  id INTEGER,
  salesperson TEXT,
  region TEXT,
  amount INTEGER
);

INSERT INTO sales VALUES
(1, 'Ahmed', 'North', 500),
(2, 'Sara', 'South', 300),
(3, 'Ahmed', 'North', 700),
(4, 'Mei', 'East', 450),
(5, 'Sara', 'South', 600),
(6, 'Mei', 'East', 800),
(7, 'Ahmed', 'North', 200),
(8, 'Sara', 'South', 400);

Then answer these questions using SQL:

  1. What is the total sales amount per salesperson?
  2. What is the average sale per region?
  3. Which salesperson has the highest total sales?
  4. Which regions have total sales above 1000?

Try writing each query before scrolling to check. The exercise will make GROUP BY stick permanently.


Summary

Here’s everything you learned in this guide:

ConceptWhat it does
COUNT()Counts the number of rows
SUM()Adds up all values in a column
AVG()Returns the average value
MIN()Returns the smallest value
MAX()Returns the largest value
GROUP BYGroups rows by a column so aggregates run per group
HAVINGFilters groups after GROUP BY (like WHERE but for groups)

These tools transform raw data into real business intelligence. Once you’re comfortable with them, you can answer almost any “how many”, “how much”, and “what’s the average” question from any dataset.


What’s Next?

You’ve now covered the core of SQL. The next step is learning how to design a database from scratch.

👉 Read next: [How to Design Your First Database — A Beginner’s Guide]

Or revisit the previous lesson:

👉 [SQL JOINs Explained Simply — Connecting Tables for Beginners]


Published on SimplifyDatabase.com — where databases are explained the easy way.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top