How to Use GROUP BY in MariaDB

Galaxy Glossary

How do I properly use the GROUP BY clause in MariaDB to summarize data?

GROUP BY clusters rows sharing the same values into summary rows so you can apply aggregate functions like COUNT, SUM, AVG, MIN, and MAX.

Sign up for the latest in SQL knowledge from the Galaxy Team!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Description

Table of Contents

MariaDB’s GROUP BY lets you turn raw rows into summarized insights with just one clause.

What does GROUP BY do in MariaDB?

GROUP BY partitions result-set rows that share identical values in one or more columns. Once grouped, aggregate functions can compute metrics such as total sales per customer or average order value per day.

When should I use GROUP BY?

Use GROUP BY whenever you need rolled-up numbers—totals, counts, averages, minimums, maximums—across logical buckets like customers, products, or dates.

What is the basic syntax?

The clause appears after FROM/WHERE and before ORDER BY or LIMIT. You list one or more grouping columns followed by optional HAVING to filter aggregated rows.

SELECT column1, aggregate_fn(col2)
FROM table
WHERE ...
GROUP BY column1 [, columnN]
HAVING aggregate_fn(col2) > value;

How do I aggregate sales by customer?

Query Orders and OrderItems to compute each customer’s spend.

SELECT c.id,
c.name,
SUM(oi.quantity * p.price) AS total_spent
FROM Customers c
JOIN Orders o ON o.customer_id = c.id
JOIN OrderItems oi ON oi.order_id = o.id
JOIN Products p ON p.id = oi.product_id
GROUP BY c.id, c.name
ORDER BY total_spent DESC;

Can I filter grouped rows with HAVING?

Yes. HAVING acts like WHERE for aggregated data. For example, return only customers spending over $1,000.

... GROUP BY c.id HAVING SUM(oi.quantity * p.price) > 1000;

How does GROUP BY interact with ORDER BY?

GROUP BY groups; ORDER BY sorts the final aggregated result. Always reference selected columns or aliases in ORDER BY to avoid ambiguity.

Best practices for performance?

Index columns used in GROUP BY and JOIN predicates, avoid grouping unnecessary columns, and pre-filter rows with WHERE before grouping to shrink input data.

Common mistakes and fixes

Non-aggregated column in SELECT that isn’t in GROUP BY. MariaDB rejects this unless ONLY_FULL_GROUP_BY is disabled. Fix by adding the column to GROUP BY or wrapping it in an aggregate.

Using HAVING for non-aggregated filters. HAVING runs after grouping and can slow queries. Move simple filters to WHERE whenever possible.

Frequently asked questions

Can I GROUP BY an alias?

No. In MariaDB you must repeat the full expression or column name; aliases are resolved after GROUP BY.

Does GROUP BY always sort the result?

MariaDB may internally sort, but the output order is not guaranteed. Use ORDER BY for deterministic ordering.

Why How to Use GROUP BY in MariaDB is important

How to Use GROUP BY in MariaDB Example Usage


-- Total revenue per day in the last month
SELECT  DATE(o.order_date) AS order_day,
        SUM(oi.quantity * p.price) AS daily_revenue
FROM    Orders      o
JOIN    OrderItems  oi ON oi.order_id   = o.id
JOIN    Products    p  ON p.id          = oi.product_id
WHERE   o.order_date >= CURDATE() - INTERVAL 1 MONTH
GROUP BY order_day
ORDER BY order_day;

How to Use GROUP BY in MariaDB Syntax


SELECT select_list
FROM   table_reference
[WHERE condition]
GROUP BY [{ROLLUP | CUBE}] grouping_column [, grouping_column]
[HAVING aggregate_condition]
[ORDER BY column_or_alias [ASC|DESC]]
[LIMIT n];

Common Mistakes

Frequently Asked Questions (FAQs)

Is GROUP BY required when using aggregate functions?

No; without GROUP BY the entire result set is treated as a single group.

Can I group by multiple columns?

Yes. Separate columns with commas to create composite groups, e.g., GROUP BY customer_id, product_id.

What is ROLLUP in GROUP BY?

ROLLUP adds subtotal rows at increasing levels of aggregation, useful for multi-level reports.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.