BeginnerCoder

Lesson 25 — Ultimate: sales report

Boss level: a real sales report

Time to combine everything: JOIN to get names, WHERE to drop cancelled orders, GROUP BY per product, aggregates for units and revenue, HAVING to hide tiny sellers, and ORDER BY + LIMIT for a top list. Build it clause by clause and run it after each step.

SELECT p.name, SUM(o.qty) AS units, SUM(o.qty * p.price) AS revenue
FROM orders o JOIN products p ON p.id = o.product_id
...

Why it matters: this is the kind of query a business owner actually asks for: "what are my best products this month?".

Want it another way?
Task

Write the missing WHERE, HAVING and ORDER BY lines (see the TODOs). Expected: Lamp first, then Mug.

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.

Keep going with Databases & SQL

Ready for more? Optional paid resources that pick up where this lesson leaves off.

Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.