Home › SQL Tutorial › Live project: Mini shop database
Live project: Mini shop database
Design tables and report sales with a join.
Create tables, insert data and write a report query with JOIN, SUM, GROUP BY and ORDER BY.
What you will build
- A
productstable and asalestable - Sample data
- A revenue report sorted from highest to lowest
Steps
- Read the two CREATE TABLE statements.
- Read the INSERT statements that add the data.
- Press Run to see the report.
- Change a price or a quantity and run again.
Run it
Press Run to see the project work. Then change the data in the editor and run it again.
Challenge
- Add a
categorycolumn and group by it. - Show only products with revenue above 500.
Example code
CREATE TABLE products (id INTEGER PRIMARY KEY, name TEXT, price INTEGER);
CREATE TABLE sales (id INTEGER PRIMARY KEY, product_id INTEGER, qty INTEGER);
INSERT INTO products VALUES (1, 'Pen', 10), (2, 'Notebook', 45), (3, 'Bag', 500);
INSERT INTO sales VALUES (1, 1, 30), (2, 2, 12), (3, 3, 2), (4, 1, 20);
SELECT p.name, SUM(s.qty) AS sold, SUM(s.qty * p.price) AS revenue
FROM sales s JOIN products p ON p.id = s.product_id
GROUP BY p.name
ORDER BY revenue DESC;