Day 7 is where everything comes together — real-world simulation.
You’re no longer “learning SQL”… you’re using SQL like a professional.
You will:
- Design a working schema
- Populate realistic data
- Write 25+ real-world queries
- Think like a backend developer / data analyst
PostgreSQL:
CREATE DATABASE sql_project;MySQL:
CREATE DATABASE sql_project;
USE sql_project;CREATE TABLE users (
id INT PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100) UNIQUE,
created_at DATE
);
CREATE TABLE products (
id INT PRIMARY KEY,
name VARCHAR(100),
category VARCHAR(50),
price INT
);
CREATE TABLE orders (
id INT PRIMARY KEY,
user_id INT,
order_date DATE,
FOREIGN KEY (user_id) REFERENCES users(id)
);
CREATE TABLE order_items (
order_id INT,
product_id INT,
quantity INT,
PRIMARY KEY (order_id, product_id),
FOREIGN KEY (order_id) REFERENCES orders(id),
FOREIGN KEY (product_id) REFERENCES products(id)
);INSERT INTO users VALUES
(1, 'Alice', 'alice@email.com', '2023-01-10'),
(2, 'Bob', 'bob@email.com', '2023-03-15'),
(3, 'Carol', 'carol@email.com', '2024-02-20'),
(4, 'David', 'david@email.com', '2024-05-01');
INSERT INTO products VALUES
(1, 'Laptop', 'Electronics', 1000),
(2, 'Phone', 'Electronics', 500),
(3, 'Shoes', 'Fashion', 150),
(4, 'Watch', 'Accessories', 200);
INSERT INTO orders VALUES
(1, 1, '2024-01-01'),
(2, 1, '2024-02-10'),
(3, 2, '2024-03-05'),
(4, 3, '2024-04-01');
INSERT INTO order_items VALUES
(1, 1, 1),
(1, 2, 2),
(2, 3, 1),
(3, 2, 3),
(4, 4, 1);SELECT users.name, COUNT(orders.id) AS total_orders
FROM users
JOIN orders ON users.id = orders.user_id
GROUP BY users.name
ORDER BY total_orders DESC
LIMIT 5;SELECT SUM(products.price * order_items.quantity) AS total_revenue
FROM order_items
JOIN products ON order_items.product_id = products.id;SELECT DATE_TRUNC('month', orders.order_date) AS month,
SUM(products.price * order_items.quantity) AS revenue
FROM orders
JOIN order_items ON orders.id = order_items.order_id
JOIN products ON products.id = order_items.product_id
GROUP BY month
ORDER BY month;(MySQL: use DATE_FORMAT instead)
SELECT products.name, SUM(order_items.quantity) AS total_sold
FROM products
JOIN order_items ON products.id = order_items.product_id
GROUP BY products.name
ORDER BY total_sold DESC
LIMIT 1;SELECT products.name
FROM products
LEFT JOIN order_items
ON products.id = order_items.product_id
WHERE order_items.product_id IS NULL;SELECT users.name
FROM users
LEFT JOIN orders ON users.id = orders.user_id
WHERE orders.id IS NULL;SELECT users.name,
SUM(products.price * order_items.quantity) AS total_spent
FROM users
JOIN orders ON users.id = orders.user_id
JOIN order_items ON orders.id = order_items.order_id
JOIN products ON products.id = order_items.product_id
GROUP BY users.name
ORDER BY total_spent DESC
LIMIT 1;SELECT AVG(order_total)
FROM (
SELECT orders.id,
SUM(products.price * order_items.quantity) AS order_total
FROM orders
JOIN order_items ON orders.id = order_items.order_id
JOIN products ON products.id = order_items.product_id
GROUP BY orders.id
) AS sub;SELECT products.category,
SUM(order_items.quantity) AS total_sold
FROM products
JOIN order_items ON products.id = order_items.product_id
GROUP BY products.category
ORDER BY total_sold DESC
LIMIT 1;SELECT users.name, COUNT(orders.id)
FROM users
JOIN orders ON users.id = orders.user_id
GROUP BY users.name
HAVING COUNT(orders.id) > 1;- Total number of users
- Total number of orders
- Total revenue
- Average product price
- Highest priced product
- Users who joined in 2024
- Users who never ordered
- Users with more than 1 order
- Top spender
- Products in Electronics category
- Products never sold
- Most expensive product per category
- Orders in March 2024
- Largest order (by value)
- Orders with more than 2 items
- Second highest spending user
- Products sold more than average quantity
- Monthly growth in revenue
- Customers who bought Electronics but not Fashion
- Most consistent buyer (orders every month)
How would you scale this database for millions of users?
Which columns would you index and why?
Difference between normalization and denormalization?
When would you replace subqueries with JOIN?
How does SQL connect to backend systems (Node.js / Python)?
You can now:
✔ Write complex SQL queries
✔ Use JOINs, GROUP BY, subqueries
✔ Design relational databases
✔ Understand performance (indexes)
✔ Solve interview questions
✔ Work with backend systems
Now move to:
- Node.js + SQL (e.g. with Prisma or raw queries)
- OR Python + SQL (Flask/Django)
- Query optimization (
EXPLAIN) - Transactions
- Stored procedures
- ORMs