codegurukool.com · Free coding tutorials & online compiler

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

Steps

  1. Read the two CREATE TABLE statements.
  2. Read the INSERT statements that add the data.
  3. Press Run to see the report.
  4. 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

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;

Run this code in the online editor

← SQL injection and prepared statements