← Back to Questions
SQL

What is a materialized view?

Learn What is a materialized view? with simple explanations, real-time examples, interview tips and practical use cases.

A Materialized View is a database object that stores the result of a query physically in the database.

In simple words:

A materialized view stores precomputed query results to improve performance and speed.


Simple Understanding

Normal View:

  • Stores only SQL query

Materialized View:

  • Stores actual query result data

Why Materialized Views are Important

Large enterprise systems execute:

  • Complex JOIN queries
  • Heavy analytics queries
  • Aggregation reports

Running these queries repeatedly can:

  • Slow applications
  • Increase CPU usage
  • Increase database load

Materialized views solve this problem by:

  • Precomputing and storing results

Real-Life Example

Think about:

  • Daily sales report in a company

Instead of:

  • Calculating millions of sales records every time

Database stores:

  • Precalculated summary

Materialized View Internal Architecture

Original Tables
       |
       v
Execute Complex Query
       |
       v
Store Query Result Physically
       |
       v
Users Read Precomputed Data

How Materialized View Works

Materialized view:

  • Executes query once
  • Stores result physically
  • Returns stored result directly

Result

  • Very fast query execution

Difference Between View and Materialized View

Feature View Materialized View
Stores Data No Yes
Query Execution Every time Precomputed
Performance Slower Faster
Storage Required No Yes
Refresh Needed No Yes

Normal View Flow

User Query
      |
      v
Execute SQL Query Again
      |
      v
Fetch Latest Data

Materialized View Flow

User Query
      |
      v
Read Stored Result Directly
      |
      v
Return Data Faster

Real-Time Example

Orders Table

Order ID Customer Amount
1 Naresh 1000
2 Rahul 2000
3 Arjun 3000

Requirement

Management dashboard needs:

  • Total sales report

Normal Query

SELECT customer,
       SUM(amount)

FROM orders

GROUP BY customer;

Problem

If orders table contains:

100 million rows

query becomes:

  • Very slow

Solution

Create:

Materialized View


Materialized View Syntax

CREATE MATERIALIZED VIEW sales_summary AS

SELECT customer,
       SUM(amount) AS total_sales

FROM orders

GROUP BY customer;

What Happens Internally?

Database:

  • Executes query once
  • Stores result physically

Stored Result Example

Customer Total Sales
Naresh 1000
Rahul 2000
Arjun 3000

Performance Benefit

Now database reads:

  • Stored summary directly

instead of:

  • Recalculating large query

Why Materialized Views are Faster

Because:

  • Expensive computations already completed

Materialized View Query Flow

Complex Query
      |
      v
Precompute Results
      |
      v
Store Physically
      |
      v
Fast Retrieval

Refresh in Materialized View

Materialized view data may become:

  • Outdated

when original tables change.


Why Refresh is Needed

Because:

  • Stored data does not update automatically in some databases

Refresh Syntax

REFRESH MATERIALIZED VIEW sales_summary;

Types of Refresh

  • Complete Refresh
  • Incremental Refresh

1. Complete Refresh

Database:

  • Rebuilds entire materialized view

2. Incremental Refresh

Database:

  • Updates only changed data

Materialized View vs Table

Feature Materialized View Table
Stores Data Yes Yes
Derived from Query Yes No
Auto Generated Yes Manual Data Insert

Materialized View vs Index

Feature Materialized View Index
Purpose Store query results Speed up searches
Stores Data Yes Index structure only
Used For Analytics and reports Query optimization

When Materialized Views are Used

  • Reporting systems
  • Analytics dashboards
  • Business intelligence
  • Data warehouses
  • Large aggregations

Real-Time Banking Example

Banking systems use materialized views for:

  • Daily transaction summaries
  • Customer balance reports
  • Fraud analytics

Example

Daily Customer Balance Summary

Real-Time E-Commerce Example

E-commerce platforms use materialized views for:

  • Sales dashboards
  • Product analytics
  • Revenue reports

Example

Top Selling Products Report

Real-Time Learning Platform Example

Learning platforms use materialized views for:

  • Course analytics
  • Student progress reports
  • Leaderboard dashboards

Example

Course Completion Statistics

Materialized Views in Microservices

Microservices architectures use materialized views for:

  • Read optimization
  • Analytics APIs
  • CQRS read models

Example

GET /api/reports/monthly-sales

may use:

Materialized View


Advantages of Materialized Views

  • Very fast query performance
  • Reduces database load
  • Improves reporting speed
  • Optimizes analytical queries

Disadvantages of Materialized Views

  • Consumes storage space
  • Needs refresh management
  • Can contain stale data

Performance Consideration

Materialized views are excellent for:

  • Read-heavy systems

But not ideal for:

  • Rapidly changing real-time data

Best Practices

  • Use for heavy reporting queries
  • Refresh periodically
  • Index materialized views if needed
  • Use incremental refresh when possible

Common Interview Mistake

Many developers think:

  • Materialized view and normal view are same

Reality

Normal view:

  • Stores query only

Materialized view:

  • Stores actual query result data

Related Learning Topics


Professional Interview Answer

A Materialized View is a database object that physically stores the result of a SQL query. Unlike a normal view, which executes the query every time it is accessed, a materialized view stores precomputed query results for faster data retrieval. Materialized views are commonly used in reporting systems, analytics dashboards, business intelligence applications, and data warehouses to improve performance for complex JOINs, aggregations, and large analytical queries. Since materialized views store data physically, they require periodic refresh operations to synchronize with underlying table changes.


Why Interviewers Like This Answer

  • Clearly explains physical storage concept
  • Shows performance optimization understanding
  • Includes enterprise reporting use cases
  • Explains refresh mechanisms
  • Demonstrates analytical database knowledge

Frequently Asked Questions

What is a materialized view?

A materialized view physically stores query results for faster access.

How is materialized view different from normal view?

Normal views store only query definition, while materialized views store actual data.

Why materialized views are faster?

Because query results are precomputed and stored physically.

Do materialized views require refresh?

Yes, materialized views must be refreshed to reflect latest data changes.

Where are materialized views commonly used?

Reporting systems, analytics dashboards, business intelligence, and data warehouses.

Why this SQL question is important?

This interview question helps candidates understand real-time backend development concepts, practical problem solving, coding fundamentals, system design basics and production-ready application behavior.

Practice this question carefully for Java backend roles, Spring Boot developer interviews, microservices interviews, company interviews and full-stack developer preparation.

About the Author

Naresh Kumar is a Senior Java Backend Engineer with experience building enterprise applications using Java, Spring Boot, Microservices, Docker, Kubernetes and Cloud technologies.