Aral Code

Working with Data Lesson 9 of 13

MySQL Joins and Reports

Build real reports in MySQL by joining tables, grouping and summarizing — sales per customer, best sellers, class rankings with window functions, and WITH ROLLUP subtotals.

Databases shine when you combine tables into reports. This lesson reviews joins and grouping with practical reports, then adds MySQL 8 features like window functions and WITH ROLLUP.

Joining three tables

SQL Sales per customer
SELECT c.name,
       COUNT(DISTINCT o.id) AS orders,
       SUM(oi.qty * p.price) AS total_spent
FROM customers c
JOIN orders o       ON o.customer_id = c.id
JOIN order_items oi ON oi.order_id = o.id
JOIN products p     ON p.id = oi.product_id
GROUP BY c.id, c.name
ORDER BY total_spent DESC;

GROUP BY c.id, c.name: MySQL's default mode (ONLY_FULL_GROUP_BY) requires every non-aggregated column in the SELECT to be grouped or functionally dependent on the grouped key.

LEFT JOIN for "including zero"

SQL Every product with units sold (0 if never sold)
SELECT p.name, COALESCE(SUM(oi.qty), 0) AS units_sold
FROM products p
LEFT JOIN order_items oi ON oi.product_id = p.id
GROUP BY p.id, p.name
ORDER BY units_sold DESC;

Window functions: ranking

MySQL 8 (and MariaDB 10.2+) support window functions, which compute values across rows without collapsing them into groups. Perfect for class rankings:

SQL Rank students within each subject
SELECT g.subject,
       s.first_name,
       g.score,
       RANK() OVER (PARTITION BY g.subject ORDER BY g.score DESC) AS rank_in_subject
FROM grades g
JOIN students s ON s.id = g.student_id
ORDER BY g.subject, rank_in_subject;
  • OVER (...) makes it a window function.
  • PARTITION BY g.subject restarts the ranking for each subject.
  • RANK() gives ties the same rank; ROW_NUMBER() always numbers 1, 2, 3…; DENSE_RANK() doesn't skip numbers after ties.

Top N per group

SQL Top 2 students per subject
WITH ranked AS (
  SELECT g.subject, s.first_name, g.score,
         ROW_NUMBER() OVER (PARTITION BY g.subject ORDER BY g.score DESC) AS rn
  FROM grades g JOIN students s ON s.id = g.student_id
)
SELECT subject, first_name, score
FROM ranked
WHERE rn <= 2
ORDER BY subject, score DESC;

Running totals

SQL Running total of sales by date
WITH daily AS (
  SELECT o.order_date, SUM(oi.qty * p.price) AS sales
  FROM orders o
  JOIN order_items oi ON oi.order_id = o.id
  JOIN products p ON p.id = oi.product_id
  GROUP BY o.order_date
)
SELECT order_date, sales,
       SUM(sales) OVER (ORDER BY order_date) AS running_total
FROM daily
ORDER BY order_date;

Subtotals with WITH ROLLUP (MySQL)

SELECT category, SUM(price * stock) AS stock_value
FROM products
GROUP BY category WITH ROLLUP;

Output

+---------------+-------------+
| category      | stock_value |
+---------------+-------------+
| Bread         |     1000.00 |
| Canned Goods  |     2910.00 |
| ...           |         ... |
| Snacks        |        0.00 |
| NULL          |    20400.00 |
+---------------+-------------+

The extra row with NULL is the grand total. Wrap it for display: COALESCE(category, 'TOTAL').

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. What does PARTITION BY subject do in a window function?
2. Which function gives tied scores the same rank?
3. What does WITH ROLLUP add?
4. Why use COUNT(DISTINCT o.id) after joining order items?

By Sir Reyel · Last updated 2026-12-04