📖 Scenario: You are a data engineer managing a Snowflake data warehouse. You want to automate a SQL query that refreshes a summary table every day without manual intervention.
🎯 Goal: Build a Snowflake task that runs a SQL statement daily to update a summary table.
📋 What You'll Learn
Create a SQL statement that refreshes the summary table
Create a Snowflake task with a daily schedule
Enable the task to run automatically
Verify the task configuration
💡 Why This Matters
🌍 Real World
Automating data refreshes in Snowflake saves time and reduces manual errors in data pipelines.
💼 Career
Data engineers and cloud architects often schedule tasks in Snowflake to maintain up-to-date data for analytics.
Progress0 / 4 steps
1
Create the SQL statement to refresh the summary table
Create a SQL statement called refresh_summary_sql that contains the exact text: INSERT INTO summary_table SELECT * FROM raw_data WHERE date = CURRENT_DATE()
Snowflake
Hint
Use a string variable to hold the SQL statement exactly as shown.
2
Create a Snowflake task with a daily schedule
Write a SQL command to create a task named daily_refresh_task that runs the SQL in refresh_summary_sql every day at 1 AM UTC. Use the schedule string 'USING CRON 0 1 * * * UTC' and the SQL statement from refresh_summary_sql as the task body.
Snowflake
Hint
Use CREATE OR REPLACE TASK with the exact task name and schedule. Include the SQL statement inside the task.
3
Enable the Snowflake task to run automatically
Write a SQL command to enable the task named daily_refresh_task so it can run on schedule.
Snowflake
Hint
Use ALTER TASK daily_refresh_task RESUME; to enable the task.
4
Verify the task configuration
Write a SQL query to show the details of the task named daily_refresh_task using SHOW TASKS LIKE 'daily_refresh_task'.
Snowflake
Hint
Use SHOW TASKS LIKE 'daily_refresh_task'; to verify the task.
Practice
(1/5)
1. What is the main purpose of a TASK in Snowflake?
easy
A. To create user accounts
B. To store large amounts of data
C. To automate running SQL statements on a schedule
D. To manage network security
Solution
Step 1: Understand the role of TASK in Snowflake
A TASK is designed to run SQL commands automatically at set times.
Step 2: Compare TASK purpose with other options
Storing data, creating users, and managing security are handled by other Snowflake features, not TASKS.
Final Answer:
To automate running SQL statements on a schedule -> Option C
Quick Check:
Task = automate SQL schedule [OK]
Hint: Tasks run SQL automatically on schedule [OK]
Common Mistakes:
Confusing TASK with data storage
Thinking TASK creates users
Assuming TASK manages security
2. Which of the following is the correct syntax to create a simple task that runs every hour in Snowflake?
easy
A. CREATE TASK my_task WAREHOUSE = my_wh SCHEDULE = 'USING CRON 0 * * * * UTC' AS SELECT CURRENT_TIMESTAMP();
B. CREATE TASK my_task WAREHOUSE = my_wh SCHEDULE = 'EVERY 1 HOUR' AS SELECT CURRENT_TIMESTAMP();
C. CREATE TASK my_task WAREHOUSE = my_wh SCHEDULE = 'CRON 0 0 * * *' AS SELECT CURRENT_TIMESTAMP();
D. CREATE TASK my_task WAREHOUSE = my_wh SCHEDULE = 'RUN EVERY HOUR' AS SELECT CURRENT_TIMESTAMP();
Solution
Step 1: Identify correct cron syntax for hourly schedule
The cron expression '0 * * * * UTC' means at minute 0 of every hour in UTC time.
Step 2: Check Snowflake task syntax
Snowflake requires SCHEDULE = 'USING CRON ...' format, so CREATE TASK my_task WAREHOUSE = my_wh SCHEDULE = 'USING CRON 0 * * * * UTC' AS SELECT CURRENT_TIMESTAMP(); matches this correctly.
When you try to resume the task, it fails. What is the problem?
medium
A. The warehouse name is missing
B. The cron expression has an invalid minute value
C. The SQL statement is invalid
D. The task name is not allowed
Solution
Step 1: Check the cron expression
Cron minute values must be between 0 and 59. '60' is invalid.
Step 2: Validate other parts
Warehouse is specified, SQL is valid, and task name is allowed, so no issues there.
Final Answer:
The cron expression has an invalid minute value -> Option B
Quick Check:
Cron minutes must be 0-59 [OK]
Hint: Cron minutes must be 0-59, not 60 [OK]
Common Mistakes:
Using 60 as a minute in cron
Ignoring cron syntax errors
Assuming warehouse or SQL errors cause failure
5. You want to create a task that runs a cleanup SQL every day at 3:30 AM UTC and uses warehouse 'clean_wh'. Which of the following is the correct way to create and enable this task?
hard
A. CREATE TASK cleanup_task WAREHOUSE = clean_wh SCHEDULE = 'USING CRON 30 3 * * * UTC' AS DELETE FROM temp_table; ALTER TASK cleanup_task RESUME;
B. CREATE TASK cleanup_task WAREHOUSE = clean_wh SCHEDULE = 'EVERY DAY AT 3:30' AS DELETE FROM temp_table; ALTER TASK cleanup_task RESUME;
C. CREATE TASK cleanup_task WAREHOUSE = clean_wh SCHEDULE = 'USING CRON 3 30 * * * UTC' AS DELETE FROM temp_table; ALTER TASK cleanup_task RESUME;
D. CREATE TASK cleanup_task WAREHOUSE = clean_wh SCHEDULE = 'USING CRON 30 3 * * *' AS DELETE FROM temp_table;
Solution
Step 1: Verify cron expression for 3:30 AM UTC
The correct cron is '30 3 * * * UTC' meaning minute 30, hour 3.
Step 2: Check task creation and enabling
Task must be created with warehouse, schedule, SQL, and then resumed with ALTER TASK ... RESUME.
Step 3: Compare options
CREATE TASK cleanup_task WAREHOUSE = clean_wh SCHEDULE = 'USING CRON 30 3 * * * UTC' AS DELETE FROM temp_table; ALTER TASK cleanup_task RESUME; uses correct cron, warehouse, SQL, and resumes the task. CREATE TASK cleanup_task WAREHOUSE = clean_wh SCHEDULE = 'EVERY DAY AT 3:30' AS DELETE FROM temp_table; ALTER TASK cleanup_task RESUME; uses invalid schedule syntax. CREATE TASK cleanup_task WAREHOUSE = clean_wh SCHEDULE = 'USING CRON 3 30 * * * UTC' AS DELETE FROM temp_table; ALTER TASK cleanup_task RESUME; swaps hour and minute. CREATE TASK cleanup_task WAREHOUSE = clean_wh SCHEDULE = 'USING CRON 30 3 * * *' AS DELETE FROM temp_table; misses RESUME command.
Final Answer:
CREATE TASK cleanup_task WAREHOUSE = clean_wh SCHEDULE = 'USING CRON 30 3 * * * UTC' AS DELETE FROM temp_table; ALTER TASK cleanup_task RESUME; -> Option A