Designing Tables Lesson 21 of 21
SQL Final Project — Sales Reports for a Sari-Sari Store
Use everything you learned to answer real business questions for Tindahan ni Aling Nena — sales per day, best sellers, top customers, utang list and restock alerts.
Aling Nena wants to understand her store. In this project, you'll answer her questions one by one with SQL. Try writing each query yourself before looking at the solution, then press Run to check.
Question 1: How much did the store sell in total?
SELECT SUM(oi.qty * p.price) AS total_sales
FROM order_items oi
JOIN products p ON p.id = oi.product_id;
Question 2: Sales per day
SELECT o.order_date,
COUNT(DISTINCT o.id) AS orders,
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
ORDER BY o.order_date DESC;
COUNT(DISTINCT o.id) counts orders, not item lines; an order with three items would otherwise be counted three times.
Question 3: The 5 best-selling products
SELECT p.name,
SUM(oi.qty) AS units_sold,
SUM(oi.qty * p.price) AS sales
FROM order_items oi
JOIN products p ON p.id = oi.product_id
GROUP BY p.id
ORDER BY sales DESC
LIMIT 5;
Change ORDER BY sales to ORDER BY units_sold. Notice the ranking changes: coffee sells many units, but rice earns more money.
Question 4: Sales per category
SELECT p.category,
SUM(oi.qty * p.price) AS sales,
ROUND(100.0 * SUM(oi.qty * p.price) /
(SELECT SUM(oi2.qty * p2.price)
FROM order_items oi2 JOIN products p2 ON p2.id = oi2.product_id), 1) AS percent
FROM order_items oi
JOIN products p ON p.id = oi.product_id
GROUP BY p.category
ORDER BY sales DESC;
The subquery computes the grand total, so each category's share can be shown as a percentage. 100.0 (with a decimal) makes sure the division isn't rounded to a whole number.
Question 5: Top customers
SELECT c.name, COUNT(DISTINCT o.id) AS visits, SUM(oi.qty * p.price) AS 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
ORDER BY spent DESC
LIMIT 3;
Question 6: The utang list
SELECT c.name,
COALESCE(c.phone, '(no phone)') AS phone,
COUNT(DISTINCT o.id) AS unpaid_orders,
SUM(oi.qty * p.price) AS amount_due
FROM customers c
JOIN orders o ON o.customer_id = c.id AND o.status = 'utang'
JOIN order_items oi ON oi.order_id = o.id
JOIN products p ON p.id = oi.product_id
GROUP BY c.id
ORDER BY amount_due DESC;
Question 7: Restock alert
SELECT name, stock,
CASE
WHEN stock = 0 THEN 'URGENT: out of stock'
WHEN stock < 25 THEN 'Restock this week'
ELSE 'Restock soon'
END AS action
FROM products
WHERE stock < 40
ORDER BY stock;
Question 8: Customers to win back
SELECT c.name, MAX(o.order_date) AS last_visit
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
GROUP BY c.id
HAVING last_visit IS NULL OR last_visit < '2026-11-10'
ORDER BY last_visit;
Question 9: Record a new sale
BEGIN TRANSACTION;
INSERT INTO orders (customer_id, order_date, status) VALUES (6, '2026-11-30', 'paid');
INSERT INTO order_items (order_id, product_id, qty) VALUES (last_insert_rowid(), 5, 2);
UPDATE products SET stock = stock - 2 WHERE id = 5;
COMMIT;
SELECT c.name, o.order_date, p.name AS product, oi.qty, p.stock AS stock_left
FROM orders o
JOIN customers c ON c.id = o.customer_id
JOIN order_items oi ON oi.order_id = o.id
JOIN products p ON p.id = oi.product_id
WHERE o.id = (SELECT MAX(id) FROM orders);
Carlo Mendoza finally makes his first purchase! The order, its item and the stock update happen together in one transaction.
Your turn: challenges
- Which barangay spends the most? (Hint: join customers → orders → items → products, group by barangay.)
- What is the average number of items per order?
- For the class record: list each student's average across all subjects, with their DepEd descriptor, ranked from highest to lowest.
- Which subject has the lowest class average?
- Create a view named
daily_salesfrom Question 2, then select only the days with sales above ₱300.
What's next?
Congratulations, you've finished the SQL course! 🎉 You can now read, filter, summarize, join and change data, and design tables. Next steps:
- The MySQL course: install a real database server and connect it to a website.
- The PHP course: build web pages that read and save data in MySQL.
- The Excel course: many of the same ideas (filters, sums, lookups) in a spreadsheet.
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04