Grouping Data: GROUP BY & ONLY_FULL_GROUP_BY in MySQL 8.0
Overview & Learning Objectives
Aggregating an entire table into a single row gives overall totals, but executive decision-making requires segmented breakdowns: revenue by store, sales by month, or ticket size by loyalty tier. The `GROUP BY` clause collapses rows sharing identical key values into summary groups. In MySQL 8.0, the strict `ONLY_FULL_GROUP_BY` SQL mode is enabled by default, requiring every non-aggregated column in `SELECT` to be explicitly named in `GROUP BY`.
- Partition tabular data into categories using single and multi-column `GROUP BY`.
- Understand the relational reason behind MySQL 8.0's `ONLY_FULL_GROUP_BY` SQL mode.
- Combine multiple aggregate metrics (`COUNT`, `SUM`, `AVG`) within categorical groups.
Industry Standard
Standard: Production database standards at Meta and Shopify enforce `ONLY_FULL_GROUP_BY` mode to guarantee deterministic query results and prevent arbitrary column value selection.
Real-World Example: ScoopCraft Dark-Store Regional Performance Matrix
ScoopCraft groups transactions by dark store location and payment mode to evaluate store-level UPI adoption.
| dark_store | payment_mode | order_count | total_sales | avg_ticket |
|---|---|---|---|---|
| Koramangala Hub | UPI | 1,240 | ₹420,000.00 | ₹338.70 |
| Koramangala Hub | Card | 480 | ₹210,000.00 | ₹437.50 |
| Indiranagar Hub | UPI | 1,850 | ₹610,000.00 | ₹329.72 |
| Indiranagar Hub | Card | 620 | ₹290,000.00 | ₹467.74 |
Syntax & Core SQL Implementation
-- Multi-column GROUP BY in MySQL 8.0
SELECT
dark_store,
payment_mode,
COUNT(order_id) AS total_orders,
ROUND(SUM(order_total), 2) AS total_sales,
ROUND(AVG(order_total), 2) AS avg_ticket
FROM orders
WHERE order_status = 'Delivered'
GROUP BY dark_store, payment_mode
ORDER BY dark_store, total_sales DESC;
Parameters & Arguments Breakdown
GROUP BY col1, col2: Identifies the categorical grouping keys that determine row aggregation.ONLY_FULL_GROUP_BY: MySQL 8.0 SQL mode requiring every column in SELECT to either appear in GROUP BY or be inside an aggregate function.
Sample Walkthrough (Q&A)
Question: What causes Error 1055: "Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column..."?
SQL Pattern: SELECT store_id, customer_name, SUM(total) FROM orders GROUP BY store_id;
Result: Error 1055 (42000): Incompatible with ONLY_FULL_GROUP_BY
Explanation: Because a store has thousands of customers, MySQL cannot deterministically pick which single customer_name to return for the group. customer_name must be added to GROUP BY or aggregated.
Common Mistakes & Limitations
- Disabling `ONLY_FULL_GROUP_BY` globally instead of fixing the SQL query, leading to non-deterministic data outputs.
- Forgetting that NULL values form their own single group under `GROUP BY`.
Keyboard Shortcuts (MySQL Workbench)
| Action | Windows | Mac |
|---|---|---|
| Execute Query | Ctrl + Enter | Cmd + Enter |
Pro Tips & Modern Best Practices
- In MySQL, you can group by column expressions directly (e.g. `GROUP BY DATE(order_date)`).
- Use `ANY_VALUE(col_name)` in MySQL 8.0 if you intentionally want the engine to pick an arbitrary value for a non-grouped column.