0
0
PostgreSQLquery~20 mins

CREATE MATERIALIZED VIEW in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Materialized View Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of a simple materialized view query
Given the table sales with columns id, amount, and region, what is the output of the following query after creating the materialized view?

CREATE MATERIALIZED VIEW regional_sales AS SELECT region, SUM(amount) AS total FROM sales GROUP BY region;

Then running:
SELECT * FROM regional_sales ORDER BY region;
PostgreSQL
CREATE MATERIALIZED VIEW regional_sales AS SELECT region, SUM(amount) AS total FROM sales GROUP BY region;

SELECT * FROM regional_sales ORDER BY region;
A[]
B[{"region": "East", "total": 3000}, {"region": "West", "total": 4500}]
CSyntaxError: materialized view not refreshed
D[{"region": "East", "total": 0}, {"region": "West", "total": 0}]
Attempts:
2 left
💡 Hint
Materialized views store the result of the query at creation time.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in materialized view creation
Which option contains a syntax error when creating a materialized view in PostgreSQL?
ACREATE MATERIALIZED VIEW sales_summary SELECT region, COUNT(*) AS count FROM sales GROUP BY region;
BCREATE MATERIALIZED VIEW sales_summary AS SELECT region, COUNT(*) AS count FROM sales GROUP BY region;
CCREATE MATERIALIZED VIEW sales_summary AS SELECT region, COUNT(*) FROM sales GROUP BY region;
DCREATE MATERIALIZED VIEW sales_summary AS SELECT region, COUNT(*) AS count FROM sales GROUP BY region WITH DATA;
Attempts:
2 left
💡 Hint
Check for missing keywords in the statement.
optimization
advanced
2:00remaining
Best use of materialized view refresh
You have a materialized view that summarizes daily sales. Which option correctly refreshes the materialized view without locking reads during the refresh?
AREFRESH MATERIALIZED VIEW CONCURRENTLY sales_summary;
BALTER MATERIALIZED VIEW sales_summary REFRESH;
CUPDATE MATERIALIZED VIEW sales_summary;
DREFRESH MATERIALIZED VIEW sales_summary;
Attempts:
2 left
💡 Hint
Look for the option that allows concurrent reads during refresh.
🔧 Debug
advanced
2:00remaining
Why does this materialized view query fail?
You created a materialized view with:
CREATE MATERIALIZED VIEW mv AS SELECT id, amount FROM sales;
Later, you run:
SELECT * FROM mv WHERE amount > 100;
But you get outdated results after updating the sales table. Why?
AYou must recreate the materialized view after every update to the base table.
BThe WHERE clause is invalid on materialized views.
CMaterialized views do not update automatically; you must refresh them manually.
DMaterialized views cannot select columns with numeric data types.
Attempts:
2 left
💡 Hint
Think about how materialized views store data.
🧠 Conceptual
expert
2:00remaining
Materialized view storage and performance trade-off
Which statement best describes the main trade-off when using materialized views in PostgreSQL?
AMaterialized views are faster than indexes and replace them entirely.
BMaterialized views automatically update with base tables but slow down inserts.
CMaterialized views do not store data and always compute results on the fly.
DMaterialized views improve query speed by storing results but require manual refresh, which can be costly.
Attempts:
2 left
💡 Hint
Consider how materialized views store data and update it.