TGViewer
SQL Programming Resources SQL Programming Resources @sqlanalyst ยท 76.7K subscribers
Post #2643 2.27K
๐Ÿš€ 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
  • โค 8
  • ๐Ÿ‘ 1
More from @sqlanalyst
  1. Oct 8, 2026๐ŸŽ“ ๐— ๐—ถ๐—ฐ๐—ฟ๐—ผ๐˜€๐—ผ๐—ณ๐˜ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ผ๐˜‚๐—ฟ๐˜€๐—ฒ๐˜€ ๐˜„๐—ถ๐˜๐—ต ๐—–๐—ฒ๐—ฟ๐˜๐—ถ๐—ณ๐—ถ๐—ฐ๐—ฎ๐˜๐—ฒ๐˜€! ๐Ÿš€๐Ÿ”ฅ Upgrโ€ฆ
  2. Oct 7, 2026๐Ÿš€๐—ฃ๐—ฎ๐˜† ๐—”๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฃ๐—น๐—ฎ๐—ฐ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด | ๐—•๐—ฒ๐—ฐ๐—ผ๐—บ๐—ฒ ๐—ฎ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐโ€ฆ
  3. Oct 7, 2026SQL Interview Series โ€” Part 4 ๐Ÿ“Œ Question 4: Find the Highest Salary in Each Department Suโ€ฆ
  4. Oct 7, 2026๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ฃ๐—ผ๐˜„๐—ฒ๐—ฟ ๐—•๐—œ ๐—ณ๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜! ๐Ÿ”ฅ Learn Power BI through these FREE learninโ€ฆ
  5. Sep 29, 2026SQL Interview Series โ€” Part 2 ๐Ÿ“Œ Question 2: Find Duplicate Records Suppose you have an Emโ€ฆ
  6. Sep 29, 2026๐—™๐—ฅ๐—˜๐—˜ ๐—ฅ๐—ฒ๐˜€๐—ผ๐˜‚๐—ฟ๐—ฐ๐—ฒ๐˜€ ๐—ง๐—ผ ๐—Ÿ๐—ฒ๐—ฎ๐—ฟ๐—ป ๐—”๐—œ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ๐Ÿš€ โ€‹ Explore 6 free resourceโ€ฆ
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’