SQL GROUP BY Examples

Example:
Count orders per customer: SELECT customer_id, COUNT(*) FROM orders GROUP BY customer_id Total sales by product: SELECT product_id, SUM(amount) FROM sales GROUP BY product_id
GROUP BY aggregates rows with same values. Use with COUNT(), SUM(), AVG(), MIN(), MAX() functions.
Try SQL Beautifier →

FAQs

What is the GROUP BY rule?

All SELECT columns must be in GROUP BY or inside aggregate functions (COUNT, SUM, etc.).