0
0
PostgreSQLquery~20 mins

Materialized views concept in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Materialized Views Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of Refreshing a Materialized View

Consider a materialized view mv_sales_summary created from a sales table. After new sales data is inserted, what will be the output of the following query immediately after running REFRESH MATERIALIZED VIEW mv_sales_summary;?

SELECT COUNT(*) FROM mv_sales_summary;
PostgreSQL
CREATE MATERIALIZED VIEW mv_sales_summary AS SELECT product_id, SUM(quantity) AS total_quantity FROM sales GROUP BY product_id;
-- Assume 100 rows in sales before insert
INSERT INTO sales (product_id, sale_date, quantity) VALUES (101, '2024-06-01', 5);
REFRESH MATERIALIZED VIEW mv_sales_summary;
SELECT COUNT(*) FROM mv_sales_summary;
AThe query causes an error because REFRESH MATERIALIZED VIEW is not allowed
BThe count remains the same as before the insert because the view is not refreshed
CThe query returns zero rows because materialized views do not store data
DThe count reflects the updated number of distinct products including the new sale
Attempts:
2 left
💡 Hint

Think about what REFRESH MATERIALIZED VIEW does to the stored data.

🧠 Conceptual
intermediate
1:30remaining
Understanding Materialized View Storage

Which statement best describes how a materialized view stores data compared to a regular view?

AA regular view stores data physically, but a materialized view only stores the query definition
BA materialized view stores the query result physically on disk, while a regular view does not store data but runs the query on demand
CNeither materialized nor regular views store any data
DBoth materialized and regular views store data physically on disk
Attempts:
2 left
💡 Hint

Think about when the data is saved and when the query runs.

📝 Syntax
advanced
1:30remaining
Correct Syntax to Create a Materialized View

Which of the following SQL statements correctly creates a materialized view named mv_customer_orders that summarizes total orders per customer?

ACREATE MATERIALIZED VIEW mv_customer_orders AS SELECT customer_id, COUNT(*) AS total_orders FROM orders GROUP BY customer_id;
BCREATE VIEW MATERIALIZED mv_customer_orders AS SELECT customer_id, COUNT(*) AS total_orders FROM orders GROUP BY customer_id;
CCREATE MATERIALIZED VIEW mv_customer_orders SELECT customer_id, COUNT(*) AS total_orders FROM orders GROUP BY customer_id;
DCREATE MATERIALIZED VIEW mv_customer_orders AS SELECT customer_id, COUNT(*) total_orders FROM orders;
Attempts:
2 left
💡 Hint

Remember the correct order of keywords and the need for AS.

optimization
advanced
1:30remaining
Performance Benefit of Materialized Views

Why might using a materialized view improve query performance compared to querying the base tables directly?

ABecause materialized views use less disk space than base tables
BBecause materialized views automatically update in real-time with every base table change
CBecause the materialized view stores precomputed results, queries run faster without recalculating data
DBecause querying materialized views bypasses the database optimizer
Attempts:
2 left
💡 Hint

Think about what happens when you query a materialized view versus the original tables.

🔧 Debug
expert
2:00remaining
Error When Refreshing a Materialized View Concurrently

Given the command REFRESH MATERIALIZED VIEW CONCURRENTLY mv_report;, which error is most likely if the materialized view does not have a unique index?

AERROR: materialized view "mv_report" must have a unique index to use CONCURRENTLY
BERROR: syntax error near CONCURRENTLY
CERROR: cannot refresh materialized view because it is locked
DNo error; the refresh runs successfully
Attempts:
2 left
💡 Hint

Check the requirements for using CONCURRENTLY with materialized views.