What is a materialized view?
- SQL course
- Advanced
- Lesson 30/81
- 4 min
Learn the view that stores its result.
What is a materialized view?
A materialized view is a view that stores its result. It physically holds data when created.
CREATE MATERIALIZED VIEW sales_summary AS
SELECT category, SUM(amount) AS total
FROM sales GROUP BY category;This query stores the summary result.
The materialized view sales_summary is created. category, SUM(amount) AS total is selected from sales. Rows are grouped by category; aggregate functions are computed per group.
What is a materialized view? — details
A materialized view does not recompute on every query. It stores the result, refreshed with REFRESH.
It is ideal for dashboard reports that only need hourly or daily updates: the heavy aggregation runs once and every reader gets the stored result instantly.
Practice What is a materialized view?
Create the sales_summary materialized view.
Starter code
-- create mat view
CREATESample solution
CREATE MATERIALIZED VIEW sales_summary AS
SELECT category, SUM(amount) AS total
FROM sales GROUP BY category;Expected output
Materialized view oluşturuldu
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What does a materialized view store?
- The query
- The result Correct answer
- The index
- The trigger
Explanation: A materialized view stores the result.
2 Add MATERIALIZED
CREATE ____ VIEW sales_summary AS
SELECT category, SUM(amount) FROM sales GROUP BY category;
CREATE MATERIALIZED VIEW sales_summary AS
SELECT category, SUM(amount) FROM sales GROUP BY category;
Explanation: MATERIALIZED is the right answer.
3 What is a materialized view: match the terms
| Term | Meaning |
|---|---|
materialized |
physical |
result |
store |
REFRESH |
update |
view |
virtual |