A regular view is a saved SQL query that acts like a virtual table. It does not store data itself but shows data from underlying tables when queried.
Click to reveal answer
beginner
What is a materialized view in PostgreSQL?
A materialized view stores the result of a query physically. It saves data on disk and must be refreshed to update its content.
Click to reveal answer
intermediate
When should you choose a materialized view over a regular view?
Choose a materialized view when the query is complex and slow, and the data does not need to be real-time. It improves performance by storing precomputed results.
Click to reveal answer
intermediate
What is a downside of using materialized views?
Materialized views require manual or scheduled refresh to update data, so they can show outdated information if not refreshed regularly.
Click to reveal answer
beginner
How does a regular view handle data updates?
A regular view always shows the latest data from the underlying tables because it runs the query fresh each time it is accessed.
Click to reveal answer
Which of the following stores data physically in PostgreSQL?
AMaterialized view
BRegular view
CTemporary table
DIndex
✗ Incorrect
Materialized views store query results physically, unlike regular views which are virtual.
What must you do to update data in a materialized view?
ARun REFRESH MATERIALIZED VIEW
BUpdate the underlying tables only
CDrop and recreate the view
DNothing, it updates automatically
✗ Incorrect
Materialized views require a manual refresh command to update their stored data.
Which view type always shows the most current data?
ANeither
BMaterialized view
CBoth
DRegular view
✗ Incorrect
Regular views run the query each time, so they always show current data.
When is it better to use a regular view instead of a materialized view?
AWhen you want to store data physically
BWhen query is very slow
CWhen data changes frequently and must be current
DWhen you want to save disk space
✗ Incorrect
Regular views are better for frequently changing data that needs to be up-to-date.
What is a main benefit of materialized views?
ANo need to refresh
BFaster query performance for complex queries
CAlways up-to-date data
DNo disk space used
✗ Incorrect
Materialized views improve performance by storing precomputed results.
Explain the key differences between a materialized view and a regular view in PostgreSQL.
Think about storage and data freshness.
You got /4 concepts.
Describe scenarios when you would choose a materialized view over a regular view.
Consider performance and data update frequency.
You got /4 concepts.
Practice
(1/5)
1. Which statement best describes a regular view in PostgreSQL?
easy
A. It stores data physically and requires manual refresh.
B. It cannot be used in SELECT statements.
C. It runs the underlying query every time it is accessed.
D. It automatically updates data without running queries.
Solution
Step 1: Understand what a regular view does
A regular view is a saved SQL query that runs fresh every time you use it.
Step 2: Compare with other options
Storing data physically and requiring manual refresh describes a materialized view; saying it cannot be used in SELECT statements is false because views can be queried directly; automatically updating data without running queries is incorrect for regular views.
Final Answer:
It runs the underlying query every time it is accessed. -> Option C
Quick Check:
Regular view runs query each time [OK]
Hint: Regular views always run queries fresh on access [OK]
Common Mistakes:
Confusing materialized views with regular views
Thinking views store data physically
Believing views auto-refresh data
2. Which of the following is the correct syntax to create a materialized view in PostgreSQL?
easy
A. CREATE MATERIALIZED VIEW my_mat_view AS SELECT * FROM sales;
B. CREATE VIEW my_view AS SELECT * FROM sales;
C. CREATE TABLE my_mat_view AS SELECT * FROM sales;
D. CREATE TEMP VIEW my_mat_view AS SELECT * FROM sales;
Solution
Step 1: Identify syntax for materialized view
The correct syntax uses CREATE MATERIALIZED VIEW followed by the view name and query.
Step 2: Eliminate incorrect options
CREATE VIEW my_view AS SELECT * FROM sales; creates a regular view, CREATE TABLE my_mat_view AS SELECT * FROM sales; creates a table, CREATE TEMP VIEW my_mat_view AS SELECT * FROM sales; creates a temporary view, not materialized.
Final Answer:
CREATE MATERIALIZED VIEW my_mat_view AS SELECT * FROM sales; -> Option A
Quick Check:
Materialized view syntax includes 'MATERIALIZED' keyword [OK]
Hint: Materialized views use 'CREATE MATERIALIZED VIEW' syntax [OK]
Common Mistakes:
Using CREATE VIEW instead of CREATE MATERIALIZED VIEW
Confusing tables with materialized views
Using TEMP keyword incorrectly
3. Given a materialized view created as: CREATE MATERIALIZED VIEW sales_summary AS SELECT region, SUM(amount) AS total FROM sales GROUP BY region; What will be the result of: SELECT * FROM sales_summary; immediately after creation?
medium
A. The aggregated total sales per region based on current data.
B. An error because materialized views need manual refresh first.
C. Empty result set because no data is stored yet.
When created, a materialized view stores the query result immediately.
Step 2: Determine output of SELECT from materialized view
It returns the aggregated totals per region as per the query at creation time.
Final Answer:
The aggregated total sales per region based on current data. -> Option A
Quick Check:
Materialized view stores query result on creation [OK]
Hint: Materialized views store data on creation, so SELECT returns stored results [OK]
Common Mistakes:
Thinking materialized views are empty until refreshed
Expecting detailed rows instead of aggregated results
Assuming errors occur without manual refresh
4. You created a materialized view but notice it shows outdated data after the underlying table changes. What is the best way to fix this?
medium
A. Use SELECT * FROM materialized_view to refresh automatically.
B. Drop and recreate the materialized view every time.
C. Restart the PostgreSQL server to refresh views.
D. Run REFRESH MATERIALIZED VIEW to update the data.
Solution
Step 1: Identify how to update materialized view data
Materialized views do not auto-update; REFRESH MATERIALIZED VIEW command updates stored data.
Step 2: Evaluate other options
Dropping and recreating the materialized view every time is inefficient, using SELECT * FROM materialized_view does not refresh it, restarting the PostgreSQL server is unrelated.
Final Answer:
Run REFRESH MATERIALIZED VIEW to update the data. -> Option D
Quick Check:
Use REFRESH MATERIALIZED VIEW to update data [OK]
Hint: Use REFRESH MATERIALIZED VIEW to update stored data [OK]
Common Mistakes:
Expecting SELECT to refresh data automatically
Dropping and recreating instead of refreshing
Restarting server to fix view data
5. You have a large sales table updated hourly. You want fast reports that summarize sales by region but can tolerate data up to 1 hour old. Which is the best choice?
hard
A. Use a regular view to always get fresh data.
B. Use a materialized view refreshed every hour for faster queries.
C. Create a table with triggers to update summaries instantly.
D. Use a temporary view refreshed on each query.
Solution
Step 1: Analyze requirements for speed and data freshness
Reports need fast access and can accept data up to 1 hour old.
Step 2: Match options to requirements
Materialized views store results and can be refreshed hourly, balancing speed and freshness. Regular views run slow on large data. Triggers add complexity. Temporary views do not persist.
Final Answer:
Use a materialized view refreshed every hour for faster queries. -> Option B
Quick Check:
Materialized view + hourly refresh = best for speed and acceptable freshness [OK]
Hint: Materialized views with scheduled refresh balance speed and freshness [OK]
Common Mistakes:
Choosing regular views despite slow queries
Using triggers unnecessarily complex
Confusing temporary views with materialized views
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.