Online SQL Tools
Back to Learn SQL
MySQL BasicsIntermediate9 min

GROUP BY and HAVING

Aggregate rows with GROUP BY and filter aggregated results with HAVING.

What You Will Learn

  • Use GROUP BY to summarize records by a dimension.
  • Use HAVING for aggregated conditions like COUNT(*) > 5.
  • Use clear aliases for aggregated columns in analytics queries.

Sample SQL

SELECT user_id, COUNT(*) AS total_orders
FROM orders
GROUP BY user_id
HAVING COUNT(*) > 5
ORDER BY total_orders DESC;

Next Step

Try rewriting this query in your own schema, then use our tools to format and refine it.