MySQL in MySQL is best learned by connecting the rule to an order-management database. Start with the smallest SQL statement, observe the output, and then add one realistic constraint so the concept becomes practical.
The key habit for this lesson is to watch table rows, constraints, and indexes as it changes. That makes the topic easier to debug, easier to explain in interviews, and easier to use in real code without memorizing isolated syntax.
The GROUP BY clause groups rows that share the same value in one or more columns, then applies aggregate functions to each group. Common aggregate functions are COUNT, SUM, AVG, MIN, MAX, and GROUP_CONCAT.
-- Count products per category
SELECT category, COUNT(*) AS product_count
FROM products
GROUP BY category;
-- Total stock and average price per category
SELECT
category,
COUNT(*) AS product_count,
SUM(stock) AS total_stock,
AVG(price) AS avg_price,
MIN(price) AS min_price,
MAX(price) AS max_price
FROM products
GROUP BY category
ORDER BY product_count DESC;
-- Total revenue per order
SELECT
order_id,
SUM(quantity * unit_price) AS order_total,
COUNT(*) AS item_count
FROM order_items
GROUP BY order_id;
-- GROUP_CONCAT: combine values from a group into a comma-separated string
SELECT
category,
GROUP_CONCAT(name ORDER BY name SEPARATOR ', ') AS product_names
FROM products
GROUP BY category;
The HAVING clause filters groups after aggregation - it's the GROUP BY equivalent of WHERE. Use WHERE to filter individual rows before grouping, and HAVING to filter groups after aggregation.
-- Categories with more than 2 products
SELECT category, COUNT(*) AS product_count
FROM products
GROUP BY category
HAVING product_count > 2;
-- Customers who have placed more than 1 order
SELECT customer_id, COUNT(*) AS order_count, SUM(total) AS total_spent
FROM orders
GROUP BY customer_id
HAVING order_count > 1
ORDER BY total_spent DESC;
-- WHERE vs HAVING:
-- WHERE filters rows BEFORE grouping (faster, uses indexes)
-- HAVING filters groups AFTER aggregation
-- Only count active products, then show categories with avg price > 50
SELECT category, COUNT(*) AS product_count, AVG(price) AS avg_price
FROM products
WHERE stock > 0 -- WHERE: filter rows before grouping
GROUP BY category
HAVING avg_price > 30 -- HAVING: filter groups after aggregation
ORDER BY avg_price DESC;
-- Group by multiple columns: orders per customer per status
SELECT customer_id, status, COUNT(*) AS order_count
FROM orders
GROUP BY customer_id, status
ORDER BY customer_id, status;
-- WITH ROLLUP: adds subtotal and grand total rows
-- NULL in the grouping column indicates a subtotal/grand total row
SELECT
COALESCE(category, 'TOTAL') AS category,
COUNT(*) AS product_count,
SUM(stock) AS total_stock
FROM products
GROUP BY category WITH ROLLUP;
-- DISTINCT vs GROUP BY (equivalent for simple deduplication)
-- These two queries return the same result:
SELECT DISTINCT category FROM products;
SELECT category FROM products GROUP BY category;
-- Monthly sales summary
SELECT
YEAR(ordered_at) AS year,
MONTH(ordered_at) AS month,
COUNT(*) AS order_count,
SUM(total) AS monthly_revenue,
AVG(total) AS avg_order_value
FROM orders
WHERE status = 'delivered'
GROUP BY YEAR(ordered_at), MONTH(ordered_at)
ORDER BY year DESC, month DESC;
-- Top 5 best-selling products by quantity
SELECT
p.name,
SUM(oi.quantity) AS total_sold,
SUM(oi.quantity * oi.unit_price) AS total_revenue
FROM order_items oi
INNER JOIN products p ON oi.product_id = p.product_id
GROUP BY oi.product_id, p.name
ORDER BY total_sold DESC
LIMIT 5;
Use MySQL when the program needs a clear answer to a specific problem, not because the keyword looks familiar. In a real MySQL task, first name the input, then name the transformation, then name the output. This small discipline shows whether the topic is being used correctly or only copied from an example.
A reliable practice flow is: create the smallest working SQL statement, add one normal case, add one edge case such as missing, repeated, empty, or boundary input, and then confirm the result with result set and EXPLAIN output. If the result surprises you, reduce the code until the behavior is visible again.
The most common trap here is copying the syntax before understanding the behavior. Avoid it by writing one sentence before the code that explains why MySQL is the right choice. After the code runs, verify the lesson by doing this: change one input and explain the changed output.
Copying the syntax before understanding the behavior.
Write the expected behavior first, then make the example prove it.
Practicing only the perfect input.
Also test missing, repeated, empty, or boundary input before considering the lesson complete.
Looking only at the final output.
Trace table rows, constraints, and indexes through each important step.
Use it when the problem matches the behavior shown in the example and when the result can be verified through result set and EXPLAIN output.
Start with a tiny case, then test missing, repeated, empty, or boundary input. The main warning sign is copying the syntax before understanding the behavior.
Trace table rows, constraints, and indexes, predict the result, run the example, and compare your prediction with the actual output.
Explore 500+ free tutorials across 20+ languages and frameworks.