A View is a virtual table based on the result-set of an SQL statement. It looks and acts like a table but does not store data itself.
Types of Views
- Simple View: Based on a single table. Often updatable.
- Complex View: Contains joins, aggregates, or multiple tables. Usually read-only.
- Materialized View: A physical copy of the query result stored on disk. It must be refreshed manually or on a schedule.
Creating a View
CREATE VIEW ActiveCustomers AS
SELECT id, name, email, last_purchase_date
FROM customers
WHERE status = 'active';
Once created, you query it like a regular table:
SELECT * FROM ActiveCustomers WHERE last_purchase_date > '2024-01-01';
Why Use Views?
- Security: Show only specific columns/rows — users can access the view without knowing the underlying table structure.
- Simplicity: Hide complex joins behind a simple name.
- Consistency: Provide a stable interface even if the underlying table structure changes.
- Reusability: Write a complex query once and reference it from multiple applications.
Updatable Views
A view is updatable if it is based on a single table without aggregates, DISTINCT, GROUP BY, or set operations:
CREATE VIEW HighValueOrders AS
SELECT * FROM orders WHERE amount > 1000;
-- This updates the underlying orders table
UPDATE HighValueOrders SET status = 'processed' WHERE id = 42;
Materialized Views
A materialized view stores the query result physically on disk. It is much faster than a regular view for complex queries, but the data can become stale.
-- PostgreSQL
CREATE MATERIALIZED VIEW MonthlySales AS
SELECT EXTRACT(YEAR FROM sale_date) AS year,
EXTRACT(MONTH FROM sale_date) AS month,
SUM(amount) AS total
FROM sales
GROUP BY year, month;
-- Refresh on demand
REFRESH MATERIALIZED VIEW MonthlySales;
Use cases for materialized views:
- Pre-computed dashboards and reports.
- Data warehouse aggregates.
- Caching expensive joins for read-heavy systems.
View Limitations
- Performance: Every query against a regular view runs the underlying query — if the base query is slow, the view is slow.
- Nesting: Views based on other views can become unmanageable and perform poorly.
- No indexes: Regular views cannot be indexed directly (the underlying tables can be indexed).
Regular View vs Materialized View vs Table
| Feature | View | Materialized View | Table |
|---|---|---|---|
| Stores data | No | Yes | Yes |
| Always fresh | Yes | No (until refresh) | Yes |
| Indexable | No | Yes | Yes |
| Write speed | Depends on base table | Slow (refresh) | Normal |
Q: Difference between View and Materialized View?
A:
View:A virtual alias for a query. Running a query on a view always fetches fresh data from the base tables.Materialized View:A physical table that stores the result of the query. It is much faster for complex joins but data can become “stale” until refreshed.
Q: Can you update data through a View?
A: Yes, but with restrictions. The view must be “simple” (point to one table, no joins, no aggregates, no DISTINCT). Updating the view actually updates the underlying base table.
Q: Why use a View instead of a Table?
A:
- Security: Show only certain columns/rows to a user.
- Simplicity: Hide complex joins from the application.
- Consistency: Provide a stable interface even if the underlying table structure changes.
Q: Can a View be indexed?
A: A regular view cannot be indexed directly. However, SQL Server supports “indexed views” (similar to materialized views). PostgreSQL supports indexing materialized views.
1. Create revenue summary view.
CREATE VIEW DailyRevenue AS
SELECT sale_date, SUM(amount) as total_revenue
FROM sales
GROUP BY sale_date;2. Create Materialized View (PostgreSQL syntax).
CREATE MATERIALIZED VIEW MonthlyStats AS
SELECT month, category, COUNT(*)
FROM orders
GROUP BY month, category;
-- Refreshing the data
REFRESH MATERIALIZED VIEW MonthlyStats;3. Update data via view.
CREATE VIEW ActiveUsers AS SELECT * FROM users WHERE status = 'active';
-- This will update the 'users' table
UPDATE ActiveUsers SET last_login = NOW() WHERE id = 101;4. Create view based on another view.
CREATE VIEW HighValueCustomers AS
SELECT * FROM CustomerStats WHERE total_spent > 1000;5. Drop a view.
DROP VIEW IF EXISTS DailyRevenue;6. Create a view to restrict column access.
CREATE VIEW PublicUserData AS
SELECT id, name, city FROM users;
-- email, password, phone are hiddenPremium Content
Unlock Views & Materialized Views and all premium lessons with a subscription.
From ₹199.99/year — See plans