Bird
Raised Fist0
PostgreSQLquery~20 mins

CREATE MATERIALIZED VIEW in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of a CREATE MATERIALIZED VIEW in PostgreSQL?
easy
A. To create a temporary table that disappears after the session
B. To create an index on a table column
C. To define a virtual table that always runs the query on access
D. To store the result of a query physically for faster access

Solution

  1. Step 1: Understand what a materialized view does

    A materialized view stores the result of a query physically, unlike a normal view which runs the query every time.
  2. Step 2: Compare options with this definition

    To store the result of a query physically for faster access matches this behavior exactly. Options A and C describe temporary tables and normal views, not materialized views. To create an index on a table column is unrelated.
  3. Final Answer:

    To store the result of a query physically for faster access -> Option D
  4. Quick Check:

    Materialized view = stored query result [OK]
Hint: Materialized views save query results physically for speed [OK]
Common Mistakes:
  • Confusing materialized views with normal views
  • Thinking materialized views are temporary tables
  • Assuming materialized views auto-update without refresh
2. Which of the following is the correct syntax to create a materialized view named sales_summary from a table sales?
easy
A. CREATE MATERIALIZED VIEW sales_summary AS SELECT * FROM sales;
B. CREATE VIEW MATERIALIZED sales_summary AS SELECT * FROM sales;
C. CREATE MATERIALIZED VIEW sales_summary SELECT * FROM sales;
D. CREATE MATERIALIZED VIEW sales_summary FROM sales;

Solution

  1. Step 1: Recall the correct syntax for creating materialized views

    The correct syntax is: CREATE MATERIALIZED VIEW view_name AS SELECT ...
  2. Step 2: Check each option against this syntax

    CREATE MATERIALIZED VIEW sales_summary AS SELECT * FROM sales; matches exactly. CREATE VIEW MATERIALIZED sales_summary AS SELECT * FROM sales; swaps keywords incorrectly. CREATE MATERIALIZED VIEW sales_summary SELECT * FROM sales; misses the AS keyword. CREATE MATERIALIZED VIEW sales_summary FROM sales; uses FROM instead of AS and SELECT.
  3. Final Answer:

    CREATE MATERIALIZED VIEW sales_summary AS SELECT * FROM sales; -> Option A
  4. Quick Check:

    Syntax = CREATE MATERIALIZED VIEW ... AS SELECT ... [OK]
Hint: Use 'AS SELECT' after materialized view name [OK]
Common Mistakes:
  • Omitting the AS keyword
  • Swapping VIEW and MATERIALIZED keywords
  • Using FROM instead of AS SELECT
3. Given the following commands:
CREATE TABLE products (id INT, price INT);
INSERT INTO products VALUES (1, 100), (2, 200);
CREATE MATERIALIZED VIEW cheap_products AS SELECT * FROM products WHERE price < 150;
SELECT * FROM cheap_products;

What will be the output of the SELECT query?
medium
A. Rows with id=1 and price=100
B. Rows with id=2 and price=200
C. No rows returned
D. Syntax error

Solution

  1. Step 1: Understand the data and materialized view filter

    The table has two rows: (1,100) and (2,200). The materialized view selects rows where price < 150, so only (1,100) qualifies.
  2. Step 2: Query the materialized view

    Since the materialized view stores the filtered rows, selecting from it returns the row with id=1 and price=100.
  3. Final Answer:

    Rows with id=1 and price=100 -> Option A
  4. Quick Check:

    Filter price < 150 = row with id=1 [OK]
Hint: Materialized view stores filtered rows, returns matching data [OK]
Common Mistakes:
  • Expecting all rows instead of filtered ones
  • Confusing materialized view with normal view behavior
  • Assuming syntax error due to multiple commands
4. Consider this SQL:
CREATE MATERIALIZED VIEW recent_orders AS SELECT * FROM orders WHERE order_date > CURRENT_DATE - INTERVAL '7 days';
REFRESH MATERIALIZED VIEW recent_orders;

Which of the following is a common mistake that would cause an error or unexpected behavior?
medium
A. Using REFRESH MATERIALIZED VIEW without CONCURRENTLY
B. Creating the materialized view without a SELECT statement
C. Not refreshing the materialized view after data changes
D. Using CURRENT_DATE in the WHERE clause

Solution

  1. Step 1: Analyze each option for errors

    Not refreshing the materialized view after data changes causes stale data but no error. Using REFRESH MATERIALIZED VIEW without CONCURRENTLY is allowed but locks the view during refresh. Creating the materialized view without a SELECT statement is invalid syntax because materialized views require a SELECT query. Using CURRENT_DATE in the WHERE clause is valid usage.
  2. Step 2: Identify the syntax error

    Creating a materialized view without a SELECT statement causes a syntax error, so Creating the materialized view without a SELECT statement is the mistake causing error.
  3. Final Answer:

    Creating the materialized view without a SELECT statement -> Option B
  4. Quick Check:

    Materialized view requires SELECT query [OK]
Hint: Materialized views must have a SELECT query [OK]
Common Mistakes:
  • Skipping the SELECT query in CREATE MATERIALIZED VIEW
  • Ignoring need to refresh after data changes
  • Assuming REFRESH must use CONCURRENTLY always
5. You have a large table sales_data that updates daily. You want to create a materialized view monthly_summary that shows total sales per month. Which approach correctly creates and maintains this materialized view efficiently?
hard
A. CREATE MATERIALIZED VIEW monthly_summary AS SELECT date_trunc('month', sale_date) AS month, SUM(amount) FROM sales_data GROUP BY 1; REFRESH MATERIALIZED VIEW monthly_summary;
B. CREATE VIEW monthly_summary AS SELECT date_trunc('month', sale_date) AS month, SUM(amount) FROM sales_data GROUP BY 1;
C. CREATE MATERIALIZED VIEW monthly_summary AS SELECT date_trunc('month', sale_date) AS month, SUM(amount) FROM sales_data GROUP BY 1; REFRESH MATERIALIZED VIEW CONCURRENTLY monthly_summary;
D. CREATE MATERIALIZED VIEW monthly_summary AS SELECT * FROM sales_data; REFRESH MATERIALIZED VIEW monthly_summary;

Solution

  1. Step 1: Understand the goal and data size

    We want a summary per month from a large table that updates daily. Using a materialized view with aggregation is good for performance.
  2. Step 2: Choose the best refresh method

    CREATE MATERIALIZED VIEW monthly_summary AS SELECT date_trunc('month', sale_date) AS month, SUM(amount) FROM sales_data GROUP BY 1; REFRESH MATERIALIZED VIEW monthly_summary; refreshes but locks the view, blocking reads. CREATE MATERIALIZED VIEW monthly_summary AS SELECT date_trunc('month', sale_date) AS month, SUM(amount) FROM sales_data GROUP BY 1; REFRESH MATERIALIZED VIEW CONCURRENTLY monthly_summary; uses REFRESH MATERIALIZED VIEW CONCURRENTLY, allowing reads during refresh, which is efficient for large data. CREATE VIEW monthly_summary AS SELECT date_trunc('month', sale_date) AS month, SUM(amount) FROM sales_data GROUP BY 1; is a normal view, recalculating every time, slow for large data. CREATE MATERIALIZED VIEW monthly_summary AS SELECT * FROM sales_data; REFRESH MATERIALIZED VIEW monthly_summary; stores all raw data, not aggregated, so not efficient.
  3. Final Answer:

    CREATE MATERIALIZED VIEW with aggregation and REFRESH MATERIALIZED VIEW CONCURRENTLY -> Option C
  4. Quick Check:

    Use CONCURRENTLY to refresh without blocking [OK]
Hint: Use CONCURRENTLY to refresh materialized views without locking [OK]
Common Mistakes:
  • Using normal views for large aggregated data
  • Refreshing materialized views without CONCURRENTLY causing locks
  • Creating materialized views without aggregation for summaries
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard