Bird
Raised Fist0
Snowflakecloud~10 mins

Materialized views for repeated queries in Snowflake - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a materialized view named 'mv_sales_summary'.

Snowflake
CREATE MATERIALIZED VIEW [1] AS SELECT product_id, SUM(sales) AS total_sales FROM sales_data GROUP BY product_id;
Drag options to blanks, or click blank then click option'
Amv_sales_summary
Bsales_summary_view
Csummary_mv
Dsales_mv
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different view name than 'mv_sales_summary'.
Forgetting to specify 'MATERIALIZED' in the view creation.
2fill in blank
medium

Complete the code to refresh the materialized view 'mv_sales_summary'.

Snowflake
ALTER MATERIALIZED VIEW mv_sales_summary [1];
Drag options to blanks, or click blank then click option'
AREFRESH
BRECOMPILE
CUPDATE
DREBUILD
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'REFRESH' which is not valid for materialized views in Snowflake.
Using 'UPDATE' or 'RECOMPILE' which are incorrect commands here.
3fill in blank
hard

Fix the error in the materialized view creation by completing the missing clause.

Snowflake
CREATE MATERIALIZED VIEW mv_customer_orders AS SELECT customer_id, COUNT(order_id) AS order_count FROM orders [1] customer_id;
Drag options to blanks, or click blank then click option'
AGROUP BY
BORDER BY
CPARTITION BY
DHAVING
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'ORDER BY' instead of 'GROUP BY'.
Omitting the grouping clause entirely.
4fill in blank
hard

Fill both blanks to create a materialized view that filters and groups data.

Snowflake
CREATE MATERIALIZED VIEW mv_active_products AS SELECT product_id, SUM(quantity) AS total_quantity FROM sales_data WHERE status = '[1]' [2] product_id;
Drag options to blanks, or click blank then click option'
Aactive
Binactive
CGROUP BY
DORDER BY
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'inactive' instead of 'active' in the filter.
Using 'ORDER BY' instead of 'GROUP BY' for aggregation.
5fill in blank
hard

Fill all three blanks to create a materialized view with a filter, grouping, and having clause.

Snowflake
CREATE MATERIALIZED VIEW mv_top_customers AS SELECT customer_id, SUM(amount) AS total_spent FROM transactions WHERE transaction_date >= '[1]' [2] customer_id [3] SUM(amount) > 1000;
Drag options to blanks, or click blank then click option'
A2024-01-01
BGROUP BY
CORDER BY
DHAVING
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'ORDER BY' instead of 'HAVING' for filtering.
Omitting the 'GROUP BY' clause.
Using an incorrect date format.

Practice

(1/5)
1. What is the main benefit of using a materialized view in Snowflake for repeated queries?
easy
A. It allows users to write queries without SQL knowledge.
B. It automatically deletes old data to save storage space.
C. It encrypts data to improve security during queries.
D. It stores the query results physically to speed up repeated queries.

Solution

  1. Step 1: Understand what a materialized view does

    A materialized view saves the results of a query physically, unlike a normal view which runs the query every time.
  2. Step 2: Identify the benefit for repeated queries

    Since the results are saved, repeated queries run faster because they read stored data instead of recalculating.
  3. Final Answer:

    It stores the query results physically to speed up repeated queries. -> Option D
  4. Quick Check:

    Materialized view = stored results for speed [OK]
Hint: Materialized views save query results for faster repeated access [OK]
Common Mistakes:
  • Confusing materialized views with normal views
  • Thinking materialized views automatically delete data
  • Assuming materialized views improve security by encryption
2. Which of the following is the correct syntax to create a materialized view in Snowflake?
easy
A. CREATE MATERIALIZED my_view VIEW AS SELECT * FROM sales;
B. CREATE VIEW MATERIALIZED my_view AS SELECT * FROM sales;
C. CREATE MATERIALIZED VIEW my_view AS SELECT * FROM sales;
D. CREATE VIEW my_view MATERIALIZED AS SELECT * FROM sales;

Solution

  1. Step 1: Recall Snowflake syntax for materialized views

    The correct syntax starts with CREATE MATERIALIZED VIEW followed by the view name and query.
  2. Step 2: Match syntax with options

    CREATE MATERIALIZED VIEW my_view AS SELECT * FROM sales; matches the correct order and keywords exactly.
  3. Final Answer:

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

    CREATE MATERIALIZED VIEW = correct syntax [OK]
Hint: Materialized views start with CREATE MATERIALIZED VIEW [OK]
Common Mistakes:
  • Mixing order of keywords 'MATERIALIZED' and 'VIEW'
  • Placing MATERIALIZED after VIEW keyword
  • Using incorrect keyword order causing syntax errors
3. Given a materialized view created as CREATE MATERIALIZED VIEW mv_sales AS SELECT region, SUM(amount) AS total FROM sales GROUP BY region;, what will be the result of SELECT * FROM mv_sales WHERE region = 'West'; if the sales table has 3 rows for 'West' with amounts 100, 200, and 300?
medium
A. Empty result set
B. [{"region": "West", "total": 600}]
C. [{"region": "West", "total": 100}]
D. [{"region": "West", "total": 300}]

Solution

  1. Step 1: Understand the materialized view query

    The view groups sales by region and sums the amount for each region.
  2. Step 2: Calculate sum for 'West' region

    Amounts 100 + 200 + 300 = 600, so total for 'West' is 600.
  3. Final Answer:

    [{"region": "West", "total": 600}] -> Option B
  4. Quick Check:

    Sum amounts for region 'West' = 600 [OK]
Hint: Sum grouped amounts to find materialized view result [OK]
Common Mistakes:
  • Selecting single amount instead of sum
  • Ignoring GROUP BY effect
  • Expecting empty result when data exists
4. You created a materialized view but notice it returns outdated data after updates to the base table. What is the best way to fix this in Snowflake?
medium
A. Snowflake automatically refreshes the materialized view in the background.
B. Drop and recreate the materialized view every time data changes.
C. Run VACUUM command on the materialized view.
D. Restart the Snowflake warehouse to update the view.

Solution

  1. Step 1: Identify how to update materialized views

    Snowflake materialized views are automatically and continuously refreshed in the background when base tables change. Temporary staleness is normal due to refresh latency.
  2. Step 2: Use the correct behavior

    Snowflake handles refreshes automatically--no manual command needed.
  3. Final Answer:

    Snowflake automatically refreshes the materialized view in the background. -> Option A
  4. Quick Check:

    Snowflake MV = automatic background refresh [OK]
Hint: Snowflake automatically refreshes materialized views [OK]
Common Mistakes:
  • Unnecessarily dropping and recreating the view
  • Using VACUUM which is for table maintenance
  • Restarting warehouse does not trigger refresh
5. You want to optimize a dashboard that runs a complex query repeatedly on a large sales table. Which approach using materialized views is best to improve performance while keeping data reasonably fresh?
hard
A. Create a materialized view with aggregated data and schedule periodic refreshes during low-usage hours.
B. Create a materialized view without aggregation and never refresh it.
C. Use a normal view instead of a materialized view to always get fresh data.
D. Create multiple materialized views with full data copies for each query variation.

Solution

  1. Step 1: Consider performance and freshness needs

    Aggregated data reduces size and speeds queries; periodic refresh balances freshness and cost.
  2. Step 2: Evaluate options for dashboard optimization

    Create a materialized view with aggregated data and schedule periodic refreshes during low-usage hours. uses aggregation and scheduled refresh, best for performance and reasonable freshness.
  3. Final Answer:

    Create a materialized view with aggregated data and schedule periodic refreshes during low-usage hours. -> Option A
  4. Quick Check:

    Aggregate + scheduled refresh = best practice [OK]
Hint: Aggregate data and refresh periodically for dashboard speed [OK]
Common Mistakes:
  • Not refreshing materialized views causing stale data
  • Using normal views which are slower for repeated queries
  • Creating many full-copy views wasting storage
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