#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
MySQL Tutorial · Data Analytics

MySQL GROUP BY Clause Explained with Examples

"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;
teamrows_nwith_amounttotalavg_amtbiggest
Tech3320500.006833.338900.00
Sales4310600.003533.335200.00
HR226900.003450.006000.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;
teamcategorytotal
HRMeals900.00
HRTraining6000.00
SalesMeals1300.00
SalesTravel9300.00
TechSoftware17800.00
TechTravel2700.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:

SELECT team, GROUP_CONCAT(DISTINCT category ORDER BY category SEPARATOR ', ') AS categories
FROM expenses
GROUP BY team;
teamcategories
HRMeals, Training
SalesMeals, Travel
TechSoftware, Travel

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;
teamtotal
Sales10600.00
Tech20500.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;
teamtotal
HR6900.00
Sales10600.00
Tech20500.00
ALL TEAMS38000.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;
montotaltravel
2026-0915400.005200.00
2026-1022600.006800.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

Become a Job-Ready Data Analyst

Master MySQL, Advanced Excel, Power BI, Tableau and Python with Uncodemy's Data Analytics Training in Noida — live projects, expert mentors and Dedicated Placement Support.

Enroll in Data Analytics Course →