SQL View ka Fund

Understand how SQL Views works with Fullstackgada

📊 Orders Table
ID Customer ID Product ID Quantity Date
1 101 201 2 2024-01-15
2 102 202 1 2024-01-16
3 101 203 3 2024-01-17
👥 Customers Table
ID Name City Email Phone
101 राम शर्मा मुंबई ram@email.com 9876543210
102 श्याम गुप्ता दिल्ली shyam@email.com 9876543211
103 गीता पटेल पुणे geeta@email.com 9876543212
📦 Products Table
ID Name Price (₹) Category Stock
201 लैपटॉप 50,000 Electronics 25
202 मोबाइल 20,000 Electronics 50
203 हेडफोन 2,500 Electronics 100
🔍View Creation
📋 Order Report View
CREATE VIEW order_report AS SELECT o.id, c.name as customer_name, p.name as product_name, o.quantity, o.quantity * p.price as total_amount, o.date FROM orders o JOIN customers c ON o.customer_id = c.id JOIN products p ON o.product_id = p.id;
View का इस्तेमाल कैसे करें:
-- View बनाना CREATE VIEW simple_report AS SELECT customer_name, product_name, total_amount FROM order_report WHERE total_amount > 10000; -- View को use करना (बिल्कुल table की तरह) SELECT * FROM simple_report; -- View को update करना CREATE OR REPLACE VIEW simple_report AS SELECT customer_name, product_name, total_amount, date FROM order_report WHERE total_amount > 15000; -- View को delete करना DROP VIEW simple_report;
Table vs View:
विशेषता Table View
Data Storage ✅ Physical storage ❌ Virtual (No storage)
Space Usage 💾 Disk space लेता है 🪶 कोई space नहीं
Data Freshness ⏰ Manual update 🔄 हमेशा fresh
Complex Queries 🔍 हर बार लिखना पड़ता है ✨ एक बार define करके reuse
Performance ⚡ Fast (direct access) 🐌 Slower (query execution)
Practical Examples:
🎯 Simple View (Updatable)
CREATE VIEW mumbai_customers AS SELECT id, name, email FROM customers WHERE city = 'मुंबई'; -- यह view में data update कर सकते हैं UPDATE mumbai_customers SET email = 'newemail@example.com' WHERE id = 101;
🔒 Complex View (Read-only)
CREATE VIEW monthly_sales AS SELECT DATE_FORMAT(date, '%Y-%m') as month, COUNT(*) as total_orders, SUM(quantity * price) as total_revenue FROM orders o JOIN products p ON o.product_id = p.id GROUP BY DATE_FORMAT(date, '%Y-%m'); -- यह view read-only है (GROUP BY के कारण) SELECT * FROM monthly_sales;
🎮 Interactive Demo
Click buttons to see how views work:

👆 Upar ke buttons par click karke dekhein ki Views kaise kaam karte hain!"