DbSchema Database Designer

SQL GROUP BY Explained with Examples



Group BY Explained

When you work with databases, you often want summaries instead of just raw rows.

  • How many orders did each customer make?
  • What’s the total sales per department?
  • What is the average grade per student?

This is where SQL GROUP BY comes in. It allows you to group rows that share the same value and then apply functions like COUNT(), SUM(), or AVG().


What You’ll Learn

In this guide, we’ll cover the main GROUP BY functions step by step:


Example Tables

Let’s use a simple shop database with customers and their orders.

Customers with Orders
order_idcustomer_idproducttotal_amount
1011Bread50
1022Milk30
1031Oranges70
1042Cheese40
1053Apples20
Grouped by Customer ID
customer_idtotal_spent
1120
270
320

GROUP BY with COUNT

Count how many orders each customer has placed.

INNER JOIN diagram
SELECT customer_id, COUNT(*) AS order_count
FROM Orders
GROUP BY customer_id;
customer_idorder_count
12
22
31

Each customer_id is grouped, and the number of orders is counted.

---

GROUP BY with SUM

Now, let’s find the total spending of each customer.

INNER JOIN diagram
SELECT customer_id, SUM(total_amount) AS total_spent
FROM Orders
GROUP BY customer_id;
customer_idtotal_spent
1120
270
320

Each customer_id is grouped, and the SUM(total_amount) gives the total money they spent.


Customers Table

To make our reports more readable, let’s also create a Customers table with names for each customer:

customer_idname
1Sarah James
2Mark White
3Olivia Reed

This table can be joined with Orders using customer_id. From now on, we’ll use JOINs so results will display names instead of IDs.


GROUP BY with AVG

You can also compute the average order value per customer (showing names via JOIN).

INNER JOIN diagram
SELECT c.name, AVG(o.total_amount) AS avg_order
FROM Customers c
JOIN Orders o ON c.customer_id = o.customer_id
GROUP BY c.name;
nameavg_order
Sarah James60
Mark White35
Olivia Reed20

Sarah placed two orders worth 50 and 70 → the average is 60.


GROUP BY with HAVING

Sometimes, you want to filter after grouping. This is where HAVING comes in (not WHERE).

INNER JOIN diagram
SELECT c.name, SUM(o.total_amount) AS total_spent
FROM Customers c
JOIN Orders o ON c.customer_id = o.customer_id
GROUP BY c.name
HAVING SUM(o.total_amount) > 50;
nametotal_spent
Sarah James120
Mark White70

Only customers with spending over 50 are included.


GROUP BY with JOIN

JOIN helps you show labels instead of IDs in grouped results.

INNER JOIN diagram
SELECT c.name, SUM(o.total_amount) AS total_spent
FROM Customers c
JOIN Orders o ON c.customer_id = o.customer_id
GROUP BY c.name;
nametotal_spent
Sarah James120
Mark White70
Olivia Reed20

Use JOIN whenever you want readable names in your reports.


GROUP BY with MIN

Find the smallest order amount per customer (names via JOIN).

INNER JOIN diagram
SELECT c.name, MIN(o.total_amount) AS min_order
FROM Customers c
JOIN Orders o ON c.customer_id = o.customer_id
GROUP BY c.name;
namemin_order
Sarah James50
Mark White30
Olivia Reed20

This returns the lowest order amount for each customer.


GROUP BY with MAX

Find the largest order amount per customer (names via JOIN).

INNER JOIN diagram
SELECT c.name, MAX(o.total_amount) AS max_order
FROM Customers c
JOIN Orders o ON c.customer_id = o.customer_id
GROUP BY c.name;

Result:

namemax_order
Sarah James70
Mark White40
Olivia Reed20

This returns the highest order amount for each customer.


How to Do This Visually in DbSchema

With DbSchema, you don’t need to type SQL from scratch - you can build GROUP BY queries visually in the Query Builder:

SQL Aggregation Functions

  1. Drag your tables (Customers and Orders) into the canvas.
  2. Connect them by their foreign key.
  3. Select the columns you want to display (e.g., name).
  4. Right-click on a numeric column (like total_amount).
  5. Choose Aggregate → SUM, COUNT, AVG, MIN, MAX.

DbSchema automatically adds the GROUP BY for you and shows the generated SQL alongside the diagram. This makes it much easier to learn SQL because you see both the visual structure and the query code at the same time.


Summary

  • GROUP BY groups rows with the same value.
  • Combine with COUNT, SUM, AVG, MIN, MAX to summarize data.
  • Use HAVING to filter groups after aggregation.
  • Perfect for totals, averages, and category-based reports.

Learn More

Check out more SQL tutorials here:

DbSchema Database Designer
Visual Design & Schema Diagram

➤ Create and manage your database schema visually through a user-friendly graphical interface.

➤ Easily arrange tables, columns, and foreign keys to simplify complex database structures, ensuring clarity and accessibility.

GIT & Collaboration
Version Control & Collaboration

➤ Manage schema changes through version control with built-in Git integration, ensuring every update is tracked and backed up.

➤ Collaborate efficiently with your team to maintain data integrity and streamline your workflow for accurate, consistent results.

Data Explorer & Query Builder
Relational Data & Query Builder

➤ Seamlessly navigate and visually explore your database, inspecting tables and their relationships.

➤ Build complex SQL queries using an intuitive drag-and-drop interface, providing instant results for quick, actionable insights.

Interactive Documentation & Reporting
HTML5 Documentation & Reporting

➤ Generate HTML5 documentation that provides an interactive view of your database schema.

➤ Include comments for columns, use tags for better organization, and create visually reports.