MariaDB GROUP BY: Count and sum by team or month

MariaDB GROUP BY returns one result row for each distinct combination of the columns or expressions you group by. Group by team for a team report, or by team and month for a monthly breakdown. WHERE selects source rows for aggregation. HAVING filters the groups after aggregation.

Set up the data and choose what one row means

Use a MariaDB server, a SQL client, and a practice database named learning. Run the setup once and keep the same connection for the queries. If the table name is already in use, use a separate practice database. Here sales_month is a simple YYYY-MM string and a NULL amount represents an unknown value.

USE learning;
CREATE TABLE sales_group_demo (
    id INT PRIMARY KEY,
    team VARCHAR(10) NULL,
    sales_month CHAR(7) NOT NULL,
    amount DECIMAL(10,2) NULL
);
INSERT INTO sales_group_demo VALUES
    (1, 'A', '2025-01', 100),
    (2, 'A', '2025-01', 200),
    (3, 'A', '2025-02', 50),
    (4, 'B', '2025-01', 400),
    (5, 'B', '2025-02', NULL),
    (6, 'C', '2025-01', NULL);

Read counts, sums, and averages by team

Team A has three source rows, team B has two, and team C has one. Use GROUP BY team so that each output row describes a team. Specify ORDER BY team to make the order explicit and easy to compare with the table. MariaDB Docs · GROUP BY

SELECT team, COUNT(*) AS row_count, COUNT(amount) AS known_count,
       SUM(amount) AS total_amount, ROUND(AVG(amount), 2) AS avg_amount
FROM sales_group_demo
GROUP BY team
ORDER BY team;
team row_count known_count total_amount avg_amount
A 3 3 350.00 116.67
B 2 1 400.00 400.00
C 1 0 NULL NULL

COUNT(*) counts every row, while COUNT(amount) counts rows with a known amount. SUM() and AVG() skip NULLs. Team B’s average is 400.00. Team C’s sum and average are NULL. A recorded zero does enter the average, so replace NULL with zero only when that matches the business meaning. MariaDB Docs · COUNT() MariaDB Docs · SUM() MariaDB Docs · AVG()

Compare WHERE and HAVING on the same data

SELECT team, COUNT(*) AS row_count, SUM(amount) AS total_amount
FROM sales_group_demo
WHERE amount >= 100
GROUP BY team
ORDER BY team;

WHERE amount >= 100 leaves out team A’s 50 and all unknown amounts before grouping. Team A has two qualifying rows totaling 300.00 and team B has one totaling 400.00. Team C disappears because no source row for it qualifies.

SELECT team, COUNT(*) AS row_count, SUM(amount) AS total_amount
FROM sales_group_demo
WHERE amount >= 100
GROUP BY team
HAVING SUM(amount) >= 350
ORDER BY total_amount DESC, team;

Add HAVING SUM(amount) >= 350 to keep only team B. Change 350 to 300 and team A returns. For another comparison, restore the threshold to 350 and remove WHERE. Team A’s amount of 50 is counted again, so A now qualifies. MariaDB Docs · SELECT

Group by both team and month

SELECT team, sales_month, SUM(amount) AS total_amount
FROM sales_group_demo
GROUP BY team, sales_month
ORDER BY team, sales_month;

GROUP BY team, sales_month returns five rows. A has 300.00 in January and 50.00 in February. B has 400.00 in January and NULL in February. C has NULL in January. A collation is the set of rules for comparing and sorting text. If team labels merge unexpectedly, check whether the column collation treats case variants as equal.

Diagnose empty or misleading aggregates

SELECT team, COUNT(*) AS row_count
FROM sales_group_demo
WHERE amount > 1000
GROUP BY team;

SELECT COUNT(*) AS row_count, SUM(amount) AS total_amount
FROM sales_group_demo
WHERE amount > 1000;

The first query below has no qualifying rows and produces no groups. The second aggregates the whole matching set without a grouping key and returns one row containing a count of 0 and a sum of NULL.

In SELECT team, id, SUM(amount) … GROUP BY team, a team can have several IDs. ONLY_FULL_GROUP_BY rejects the query. Without that mode, the chosen ID is undefined. MIN(id) finds the smallest ID in the group. It does not select other fields from that same source row. MariaDB Docs · SQL_MODE

For a list of unique team names, use SELECT DISTINCT team. If totals seem too high, inspect the grouping key and WHERE conditions, and check whether a JOIN multiplies the rows fed into the aggregate. The overall average here is 187.50, calculated across the four known amounts. Averaging the team averages would give a different result because the teams have different numbers of known amounts.