๐ SQL Project Series #27
Stock Market Analytics ๐
Analyze stocks, investors, portfolios, trades, and market performance using SQL to gain insights into trading behavior, portfolio performance, and investment trends.
๐ฏ Business Objectives
โ
Analyze stock trading activity
โ
Track investor portfolios
โ
Monitor stock price movements
โ
Measure portfolio performance
โ
Identify top-performing stocks
โ
Analyze trading volume
โ
Calculate investment returns
โ
Build executive investment dashboards
๐ Step 1: Create Database
CREATE DATABASE stock_market_db;
USE stock_market_db;
๐ Step 2: Create Investors Table
CREATE TABLE investors (
investor_id INT PRIMARY KEY,
investor_name VARCHAR(100),
city VARCHAR(50),
account_open_date DATE
);
๐ Step 3: Create Stocks Table
CREATE TABLE stocks (
stock_id INT PRIMARY KEY,
stock_symbol VARCHAR(20),
company_name VARCHAR(100),
sector VARCHAR(50)
);
๐ Step 4: Create Trades Table
CREATE TABLE trades (
trade_id INT PRIMARY KEY,
investor_id INT,
stock_id INT,
trade_date DATE,
transaction_type VARCHAR(10),
quantity INT,
price DECIMAL(10,2),
FOREIGN KEY (investor_id) REFERENCES investors(investor_id),
FOREIGN KEY (stock_id) REFERENCES stocks(stock_id)
);
๐ Step 5: Create Daily Prices Table
CREATE TABLE daily_prices (
price_id INT PRIMARY KEY,
stock_id INT,
price_date DATE,
open_price DECIMAL(10,2),
close_price DECIMAL(10,2),
high_price DECIMAL(10,2),
low_price DECIMAL(10,2),
volume BIGINT,
FOREIGN KEY (stock_id) REFERENCES stocks(stock_id)
);
๐ Step 6: Insert Sample Investors
INSERT INTO investors VALUES
(1,'Rahul Sharma','Mumbai','2023-01-15'),
(2,'Priya Verma','Delhi','2023-04-20'),
(3,'Amit Patel','Pune','2024-02-10'),
(4,'Sneha Joshi','Bangalore','2024-05-05'),
(5,'Rohan Gupta','Hyderabad','2024-07-18');
๐ Step 7: Insert Sample Stocks
INSERT INTO stocks VALUES
(101,'TCS','Tata Consultancy Services','IT'),
(102,'INFY','Infosys','IT'),
(103,'RELIANCE','Reliance Industries','Energy'),
(104,'HDFCBANK','HDFC Bank','Banking'),
(105,'TATAMOTORS','Tata Motors','Automobile');
๐ Step 8: Insert Sample Trades
INSERT INTO trades VALUES
(1001,1,101,'2025-01-05','BUY',10,4200),
(1002,2,102,'2025-01-06','BUY',20,1800),
(1003,3,103,'2025-01-07','SELL',5,2900),
(1004,4,104,'2025-01-08','BUY',15,1650),
(1005,5,105,'2025-01-09','BUY',25,980);
๐ Step 9: Insert Sample Daily Prices
INSERT INTO daily_prices VALUES
(1,101,'2025-01-05',4180,4215,4235,4170,1200000),
(2,102,'2025-01-06',1785,1810,1822,1778,980000),
(3,103,'2025-01-07',2880,2910,2925,2870,2100000),
(4,104,'2025-01-08',1640,1665,1672,1635,1750000),
(5,105,'2025-01-09',970,990,995,965,3100000);
๐ง SQL Concepts You'll Practice
โ DDL & DML
โ INNER JOIN
โ LEFT JOIN
โ Aggregate Functions
โ GROUP BY
โ HAVING
โ CASE WHEN
โ Date Functions
โ CTEs
โ Window Functions
โ Ranking Functions
๐ Business KPIs You Can Build
๐ Total Investors
๐ Total Trades
๐ Buy vs Sell Transactions
๐ Total Trading Volume
๐ Portfolio Value
๐ Profit & Loss (P&L)
๐ Return on Investment (ROI)
๐ Average Buy Price
๐ Average Sell Price
๐ Top Performing Stocks
๐ Worst Performing Stocks
๐ Daily Price Change
๐ Monthly Trading Volume
๐ Sector-wise Performance
๐ Most Traded Stocks
๐ Investor Portfolio Diversification
๐ Average Holding Size
๐ Stock Volatility
๐ Market Capitalization Trend
๐ Executive Investment Dashboard
๐ฏ Double Tap โค๏ธ For More
Post #2643
2.27K
- โค 8
- ๐ 1