Beginner LevelSQL & MySQL
What are SQL Views and when should you use CREATE VIEW?
❓ Interview Prompt: Explain SQL Views, `CREATE VIEW`, virtual tables, query simplification, security abstraction, and performance considerations.
A SQL View is a virtual table defined by a saved SQL query. Views do not store data physically on disk (unlike tables); they execute their underlying query whenever referenced:
Benefits:
1
Query Simplification: Encapsulates complex joins and aggregations into a simple virtual table.
2
Security & Abstraction: Restricts user access to specific rows and columns while hiding sensitive base tables.
Code Implementation Example
sql
CREATE VIEW v_active_customer_orders AS
SELECT
u.id AS user_id,
u.email,
COUNT(o.id) AS total_orders,
SUM(o.total_amount) AS total_spent
FROM users u
JOIN orders o ON u.id = o.user_id
WHERE u.status = 'ACTIVE'
GROUP BY u.id, u.email;💡Creating a virtual view summarizing active customer order histories.
🎯 Interview Key Takeaway
Use Views to simplify complex multi-table joins and encapsulate data access permissions cleanly.
#SQL#Views#CREATE VIEW#Abstraction#Beginner
Back to all SQL & MySQL Questions→