Class 44 of 58 • Advance SQL Analytics

SQL Views & Materialized Views: Virtual Schemas & Caching

In production database architectures, exposing raw base tables directly to application developers or reporting tools creates security vulnerabilities and code duplication. A SQL View is a stored, named virtual table that encapsulates complex joins, aggregates, and column masking rules behind a clean, simple abstraction.


Creating a Standard View

CREATE OR REPLACE VIEW view_name AS
SELECT columns FROM tables WHERE condition;
-- Create a secure student directory masking private contact details
CREATE OR REPLACE VIEW v_student_roster AS
SELECT 
    s.student_id,
    CONCAT(s.first_name, ' ', s.last_name) AS student_name,
    d.dept_name AS department,
    s.course,
    s.gpa
FROM students s
LEFT JOIN departments d ON s.dept_id = d.dept_id;

-- Application developers can query the view just like a real table!
SELECT * FROM v_student_roster WHERE gpa >= 3.80;

Materialized Views: Physical Query Caching

In high-traffic analytics systems, re-evaluating joins across 10 million rows on every dashboard refresh causes server latency. In engines like PostgreSQL, a Materialized View writes the result set physically to disk:

-- PostgreSQL: Create physical cached view
CREATE MATERIALIZED VIEW mv_monthly_revenue_summary AS
SELECT 
    DATE_TRUNC('month', order_date) AS sales_month,
    COUNT(order_id) AS total_orders,
    SUM(total_amount) AS revenue
FROM orders
GROUP BY sales_month;

-- Refresh cached data on a nightly cron schedule:
REFRESH MATERIALIZED VIEW mv_monthly_revenue_summary;

Next, let us examine the core engine structure responsible for lightning-fast database performance: SQL Indexes & B-Tree Internals!


Frequently Asked Questions (FAQ)

A View is a virtual table defined by a stored SELECT query. It contains no physical data of its own on disk; whenever a view is queried, the database dynamically executes the underlying query.

A Standard View computes its query dynamically on every read. A Materialized View (supported in PostgreSQL and Oracle) physically caches the query results on disk and can be indexed, delivering instantaneous read performance on complex joins.

Views allow DBAs to restrict user access to sensitive columns (such as credit cards or salaries) by granting permissions only on a sanitized View rather than the raw base table.

Next Step in Your SQL Database Learning Path:

Proceed to the next class in sequence to build production mastery of relational database architecture.

Continue to Class 45: Indexes & B-Tree → Previous: ← LEAD & LAG Functions | Full Syllabus


Related SQL Masterclass Lessons & Recommended Next Topics

Views vs CTEs →

Reusable database schema objects vs query-scoped CTEs.

Views for Security Abstraction →

Hiding sensitive salary/PII columns behind restricted views.

Materialized Views & Caching →

Pre-computing complex join queries to disk for instant reads.

Indexed Views →

Building unique clustered indexes on materialized views.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Reddit

You may also like this!