"How much did each team spend?" Questions shaped like "how much / how many per something" are what GROUP BY is for. It sorts rows into buckets that share a value, then an aggregate function (SUM, COUNT, AVG, MIN, MAX) turns each bucket into a single row. Below, one expense table is used to build up from the basic form to the error everyone hits once.
The expense table
CREATE TABLE expenses (
exp_id INT PRIMARY KEY,
team VARCHAR(10),
category VARCHAR(10),
spent_on DATE,
amount DECIMAL(10,2)
);
INSERT INTO expenses VALUES
(1,'Sales','Travel','2026-09-04',5200), (2,'Sales','Meals','2026-09-09',1300),
(3,'Tech','Software','2026-09-12',8900), (4,'Sales','Travel','2026-10-02',4100),
(5,'Tech','Travel','2026-10-15',2700), (6,'HR','Meals','2026-10-18',900),
(7,'Tech','Software','2026-10-21',8900), (8,'HR','Training','2026-10-28',6000),
(9,'Sales','Meals','2026-10-30',NULL);
Row 9 has no amount yet (a bill still to be submitted). That NULL matters below.
One bucket per team
SELECT team,
COUNT(*) AS rows_n,
COUNT(amount) AS with_amount,
SUM(amount) AS total,
ROUND(AVG(amount), 2) AS avg_amt,
MAX(amount) AS biggest
FROM expenses
GROUP BY team
ORDER BY total DESC;
| team | rows_n | with_amount | total | avg_amt | biggest |
|---|---|---|---|---|---|
| Tech | 3 | 3 | 20500.00 | 6833.33 | 8900.00 |
| Sales | 4 | 3 | 10600.00 | 3533.33 | 5200.00 |
| HR | 2 | 2 | 6900.00 | 3450.00 | 6000.00 |
Look at Sales. COUNT(*) counts rows (4), but COUNT(amount) skips the NULL (3). AVG also ignores the NULL: 10,600 ÷ 3 = 3,533.33, not ÷ 4. If a missing amount should count as zero, write AVG(COALESCE(amount, 0)). More on this in handling NULL values in MySQL. Without ORDER BY, MySQL 8.0 does not sort grouped output; always sort explicitly.
Grouping by two columns
SELECT team, category, SUM(amount) AS total
FROM expenses
GROUP BY team, category
ORDER BY team, category;
| team | category | total |
|---|---|---|
| HR | Meals | 900.00 |
| HR | Training | 6000.00 |
| Sales | Meals | 1300.00 |
| Sales | Travel | 9300.00 |
| Tech | Software | 17800.00 |
| Tech | Travel | 2700.00 |
Each unique combination becomes a bucket. Sales Meals shows 1,300 because SUM skips the NULL row.
Error 1055: the rule behind it
SELECT team, category, SUM(amount) AS total
FROM expenses
GROUP BY team;
ERROR 1055 (42000): Expression #2 of SELECT list is not in GROUP BY clause and contains
nonaggregated column 'gb.expenses.category' which is not functionally dependent on columns
in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
Sales has two categories. Which one should MySQL print on the single Sales row? It cannot know, so it refuses. ONLY_FULL_GROUP_BY is on by default since MySQL 5.7.5. The rule: every selected column must be in GROUP BY, inside an aggregate, or determined by a grouped column (for example, other columns of the same table when you group by its primary key). There are three proper fixes:
- Add
categorytoGROUP BYif you really want one row per team and category. - Aggregate it, for example by listing all categories:
SELECT team, GROUP_CONCAT(DISTINCT category ORDER BY category SEPARATOR ', ') AS categories
FROM expenses
GROUP BY team;
| team | categories |
|---|---|
| HR | Meals, Training |
| Sales | Meals, Travel |
| Tech | Software, Travel |
- Use
ANY_VALUE(category)only when any value genuinely will do. Do not use it just to silence the error.
Turning off ONLY_FULL_GROUP_BY in sql_mode also hides the error, but then MySQL picks an arbitrary value and the report can be wrong without anyone noticing.
Filtering groups with HAVING
SELECT team, SUM(amount) AS total
FROM expenses
GROUP BY team
HAVING SUM(amount) > 10000;
| team | total |
|---|---|
| Sales | 10600.00 |
| Tech | 20500.00 |
WHERE filters rows before grouping; HAVING filters the grouped results. To total only October, put the date condition in WHERE. To keep teams over ₹10,000, put the condition in HAVING. The full comparison is in WHERE vs HAVING.
Subtotals and a grand total with ROLLUP
SELECT IF(GROUPING(team), 'ALL TEAMS', team) AS team, SUM(amount) AS total
FROM expenses
GROUP BY team WITH ROLLUP;
| team | total |
|---|---|
| HR | 6900.00 |
| Sales | 10600.00 |
| Tech | 20500.00 |
| ALL TEAMS | 38000.00 |
WITH ROLLUP adds a super-aggregate row. GROUPING() (MySQL 8.0) returns 1 on that row, so you can label it without confusing it with a real NULL team. With two grouping columns, you get a subtotal per team plus the grand total.
Grouping by month and conditional totals
SELECT DATE_FORMAT(spent_on, '%Y-%m') AS mon,
SUM(amount) AS total,
SUM(CASE WHEN category = 'Travel' THEN amount ELSE 0 END) AS travel
FROM expenses
GROUP BY mon
ORDER BY mon;
| mon | total | travel |
|---|---|---|
| 2026-09 | 15400.00 | 5200.00 |
| 2026-10 | 22600.00 | 6800.00 |
You can group by an expression or its alias. A CASE inside SUM gives a "travel only" column next to the overall total, in one pass over the table. For more on CASE, see the CASE statement with examples.
Quick reference
- Order of evaluation:
FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY → LIMIT. COUNT(DISTINCT team)counts unique values. On this table it returns 3, andCOUNT(DISTINCT team, category)returns 6 pairs.- Grouping a large table is faster with an index whose leading columns match the
GROUP BYcolumns. - The full list of aggregates is in MySQL aggregate functions.