CREATE
- SQL course
- Advanced
- Lesson 31/81
- 4 min
Create a materialized view.
CREATE
With CREATE MATERIALIZED VIEW you physically store a summary result.
CREATE MATERIALIZED VIEW daily_revenue AS
SELECT sale_date, SUM(amount) AS revenue
FROM sales GROUP BY sale_date;This query stores the daily revenue summary.
The materialized view daily_revenue is created. sale_date, SUM(amount) AS revenue is selected from sales. Rows are grouped by sale_date; aggregate functions are computed per group.
CREATE — details
When created, the query runs and the result is stored. It can later be queried like a table.
Even if the underlying tables change later, the stored result does not; so you make a deliberate trade-off between freshness and speed.
Practice CREATE
Create the daily_revenue materialized view with daily revenue.
Starter code
-- daily revenue mat view
CREATE MATERIALIZED VIEW daily_revenueSample solution
CREATE MATERIALIZED VIEW daily_revenue AS
SELECT sale_date, SUM(amount) AS revenue
FROM sales GROUP BY sale_date;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 Add AS
CREATE MATERIALIZED VIEW daily_revenue ____
SELECT sale_date, SUM(amount) AS revenue FROM sales GROUP BY sale_date;
CREATE MATERIALIZED VIEW daily_revenue AS
SELECT sale_date, SUM(amount) AS revenue FROM sales GROUP BY sale_date;
Explanation: The form is CREATE MATERIALIZED VIEW name AS query; the result of the query after AS is stored.
2 How is a materialized view queried?
- Like a view
- With a procedure
- Like a table Correct answer
- With a trigger
Explanation: A materialized view is queried like a table.
3 CREATE: match the terms
| Term | Meaning |
|---|---|
CREATE |
create |
MATERIALIZED |
physical |
AS |
define |
GROUP BY |
group |