What is GROUP BY in SQL?

Answer

GROUP BY divides query result rows into groups based on the values of one or more columns, typically to apply aggregate functions to each group. Every non-aggregated column in the SELECT list must appear in the GROUP BY clause. Query execution order: FROM → JOIN → WHERE → GROUP BY → HAVING → SELECT → ORDER BY → LIMIT. Example: SELECT category, COUNT(*) as product_count, AVG(price) as avg_price FROM products GROUP BY category HAVING COUNT(*) > 5 ORDER BY avg_price DESC. GROUP BY is fundamental for analytics and reporting queries.