MySQL Lab 1: E-Commerce Sales Reports

MySQL 70 min min read Updated: Nov 27, 2026 Intermediate
MySQL Lab 1: E-Commerce Sales Reports
Intermediate Topic 1 of 3

The Goal

You are the data person of a small online shop. The manager asks for reports. Try each task yourself before reading the solution. The database has customers, products, orders and order_items. An order's value is the sum of quantity * price of its items, and cancelled orders do not count as sales.

TableMain columns
customersid, name, city, email, joined
productsid, name, category, price, stock
ordersid, customer_id, order_date, status
order_itemsorder_id, product_id, quantity

Step 0: A View for Valid Sales

Nearly every report needs the same join. Save it once as a view:

sql CREATE VIEW sales AS SELECT o.id AS order_id, o.customer_id, o.order_date, p.id AS product_id, p.name AS product, p.category, oi.quantity, p.price, oi.quantity * p.price AS line_total FROM orders o JOIN order_items oi ON oi.order_id = o.id JOIN products p ON p.id = oi.product_id WHERE o.status <> 'cancelled'; SELECT COUNT(*) AS valid_lines, ROUND(SUM(line_total), 2) AS total_revenue FROM sales;

Result:

valid_linestotal_revenue
154381.00

Report 1: Revenue per Month

sql CREATE VIEW sales AS SELECT o.id AS order_id, o.customer_id, o.order_date, p.id AS product_id, p.name AS product, p.category, oi.quantity, p.price, oi.quantity * p.price AS line_total FROM orders o JOIN order_items oi ON oi.order_id = o.id JOIN products p ON p.id = oi.product_id WHERE o.status <> 'cancelled'; SELECT DATE_FORMAT(order_date, '%Y-%m') AS month, COUNT(DISTINCT order_id) AS orders, ROUND(SUM(line_total), 2) AS revenue FROM sales GROUP BY DATE_FORMAT(order_date, '%Y-%m') ORDER BY month;

Result:

monthordersrevenue
2025-0111057.00
2025-022654.00
2025-031507.00
2025-0421226.00
2025-052877.00
2025-06160.00

Report 2: Top 3 Customers by Spend

sql CREATE VIEW sales AS SELECT o.id AS order_id, o.customer_id, o.order_date, p.id AS product_id, p.name AS product, p.category, oi.quantity, p.price, oi.quantity * p.price AS line_total FROM orders o JOIN order_items oi ON oi.order_id = o.id JOIN products p ON p.id = oi.product_id WHERE o.status <> 'cancelled'; SELECT c.name, ROUND(SUM(s.line_total), 2) AS spent FROM sales s JOIN customers c ON c.id = s.customer_id GROUP BY c.id, c.name ORDER BY spent DESC LIMIT 3;

Result:

namespent
Asha Rao1349.00
Eli Brown989.00
Chen Wei706.00

Report 3: Category Share of Revenue

sql CREATE VIEW sales AS SELECT o.id AS order_id, o.customer_id, o.order_date, p.id AS product_id, p.name AS product, p.category, oi.quantity, p.price, oi.quantity * p.price AS line_total FROM orders o JOIN order_items oi ON oi.order_id = o.id JOIN products p ON p.id = oi.product_id WHERE o.status <> 'cancelled'; SELECT category, ROUND(SUM(line_total), 2) AS revenue, ROUND(100 * SUM(line_total) / SUM(SUM(line_total)) OVER (), 1) AS pct_of_total FROM sales GROUP BY category ORDER BY revenue DESC;

Result:

categoryrevenuepct_of_total
Electronics3470.0079.2
Furniture726.0016.6
Stationery185.004.2

SUM(SUM(...)) OVER () is a window function over the grouped result: it gives the grand total on every row, so you can divide.

Report 4: Repeat Customers

sql SELECT c.name, COUNT(*) AS valid_orders FROM customers c JOIN orders o ON o.customer_id = c.id WHERE o.status <> 'cancelled' GROUP BY c.id, c.name HAVING COUNT(*) >= 2 ORDER BY valid_orders DESC, c.name;

Result:

namevalid_orders
Asha Rao3
Ben Carter2
Chen Wei2

Report 5: Products Never Sold

sql SELECT p.name, p.stock FROM products p WHERE NOT EXISTS (SELECT 1 FROM order_items oi JOIN orders o ON o.id = oi.order_id WHERE oi.product_id = p.id AND o.status <> 'cancelled') ORDER BY p.id;

Result:

Result

(0 rows)

Every product has been sold at least once in this small sample, so the result is empty. That is a valid answer. Check the logic by adding a new product:

sql INSERT INTO products VALUES (9, 'Webcam', 'Electronics', 59.00, 40); SELECT p.name, p.stock FROM products p WHERE NOT EXISTS (SELECT 1 FROM order_items oi JOIN orders o ON o.id = oi.order_id WHERE oi.product_id = p.id AND o.status <> 'cancelled') ORDER BY p.id;

Result:

namestock
Webcam40

Report 6: Best Seller per Category

sql CREATE VIEW sales AS SELECT o.id AS order_id, o.customer_id, o.order_date, p.id AS product_id, p.name AS product, p.category, oi.quantity, p.price, oi.quantity * p.price AS line_total FROM orders o JOIN order_items oi ON oi.order_id = o.id JOIN products p ON p.id = oi.product_id WHERE o.status <> 'cancelled'; SELECT category, product, units FROM ( SELECT category, product, SUM(quantity) AS units, ROW_NUMBER() OVER (PARTITION BY category ORDER BY SUM(quantity) DESC, product) AS rn FROM sales GROUP BY category, product ) t WHERE rn = 1 ORDER BY category;

Result:

categoryproductunits
ElectronicsHeadphones6
FurnitureChair3
StationeryNotebook30

Report 7: Running Revenue

sql CREATE VIEW sales AS SELECT o.id AS order_id, o.customer_id, o.order_date, p.id AS product_id, p.name AS product, p.category, oi.quantity, p.price, oi.quantity * p.price AS line_total FROM orders o JOIN order_items oi ON oi.order_id = o.id JOIN products p ON p.id = oi.product_id WHERE o.status <> 'cancelled'; WITH monthly AS (SELECT DATE_FORMAT(order_date, '%Y-%m') AS month, SUM(line_total) AS revenue FROM sales GROUP BY DATE_FORMAT(order_date, '%Y-%m')) SELECT month, ROUND(revenue, 2) AS revenue, ROUND(SUM(revenue) OVER (ORDER BY month), 2) AS cumulative FROM monthly ORDER BY month;

Result:

monthrevenuecumulative
2025-011057.001057.00
2025-02654.001711.00
2025-03507.002218.00
2025-041226.003444.00
2025-05877.004321.00
2025-0660.004381.00

Report 8: Customers by City with Spend

sql CREATE VIEW sales AS SELECT o.id AS order_id, o.customer_id, o.order_date, p.id AS product_id, p.name AS product, p.category, oi.quantity, p.price, oi.quantity * p.price AS line_total FROM orders o JOIN order_items oi ON oi.order_id = o.id JOIN products p ON p.id = oi.product_id WHERE o.status <> 'cancelled'; SELECT COALESCE(c.city, 'Unknown') AS city, COUNT(DISTINCT c.id) AS customers, ROUND(COALESCE(SUM(s.line_total), 0.00), 2) AS revenue FROM customers c LEFT JOIN sales s ON s.customer_id = c.id GROUP BY COALESCE(c.city, 'Unknown') ORDER BY revenue DESC, city;

Result:

citycustomersrevenue
Mumbai22027.00
London21648.00
Singapore1706.00
Tel Aviv10.00

Report 9: Days Between Orders per Customer

sql SELECT c.name, o.order_date, DATEDIFF(o.order_date, LAG(o.order_date) OVER (PARTITION BY o.customer_id ORDER BY o.order_date)) AS days_since_previous FROM orders o JOIN customers c ON c.id = o.customer_id WHERE o.status <> 'cancelled' AND c.id IN (1, 2) ORDER BY c.id, o.order_date;

Result:

nameorder_datedays_since_previous
Asha Rao2025-01-10NULL
Asha Rao2025-02-1435
Asha Rao2025-04-2065
Ben Carter2025-02-20NULL
Ben Carter2025-06-01101

Report 10: Low-Stock Alert with Pending Demand

sql SELECT p.name, p.stock, COALESCE(SUM(oi.quantity), 0) AS pending_units, p.stock - COALESCE(SUM(oi.quantity), 0) AS left_after_pending FROM products p LEFT JOIN order_items oi ON oi.product_id = p.id LEFT JOIN orders o ON o.id = oi.order_id AND o.status = 'pending' WHERE o.id IS NOT NULL OR oi.product_id IS NULL GROUP BY p.id, p.name, p.stock ORDER BY left_after_pending, p.name LIMIT 5;

Result:

namestockpending_unitsleft_after_pending
Desk1019
Chair25223
Headphones100397

What You Practiced

  • Views to avoid repeating a join
  • GROUP BY and HAVING
  • Window functions for share, ranking, running totals and LAG
  • NOT EXISTS for missing data
  • Safe handling of NULLs with COALESCE
  • Checking a result that comes back empty

Extra Challenges

  1. Add a column customer_segment (VIP above 1000, Regular above 200, else New) with CASE.
  2. Find the month with the best revenue growth over the previous month.
  3. Show the top product per city.
  4. Turn the view into a stored procedure with a date range parameter.

Conclusion

You built ten real reports from one reusable view, using joins, grouping, window functions and anti joins. These patterns cover most analytics requests.

Quick Summary: view, GROUP BY, HAVING, window functions, NOT EXISTS, COALESCE, LAG.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators