Bird
Raised Fist0
Snowflakecloud~20 mins

SELECT with Snowflake functions - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Snowflake SELECT Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
service_behavior
intermediate
2:00remaining
Understanding the output of a SELECT with DATE_TRUNC
What is the output of this query when run on 2024-06-15 14:35:20?

SELECT DATE_TRUNC('month', '2024-06-15 14:35:20'::TIMESTAMP) AS truncated_date;
Snowflake
SELECT DATE_TRUNC('month', '2024-06-15 14:35:20'::TIMESTAMP) AS truncated_date;
A2024-06-01 00:00:00.000
B2024-06-15 00:00:00.000
C2024-01-01 00:00:00.000
D2024-06-15 14:35:20.000
Attempts:
2 left
💡 Hint
DATE_TRUNC cuts off smaller parts of the date to the start of the specified part.
🧠 Conceptual
intermediate
2:00remaining
Effect of TRY_CAST on invalid data
What will be the result of this query?

SELECT TRY_CAST('abc' AS INTEGER) AS result;
Snowflake
SELECT TRY_CAST('abc' AS INTEGER) AS result;
AError: invalid input syntax for integer
B0
Cabc
DNULL
Attempts:
2 left
💡 Hint
TRY_CAST returns NULL instead of error when conversion fails.
Configuration
advanced
2:30remaining
Using LATERAL FLATTEN to extract array elements
Given a table with a VARIANT column named data containing JSON arrays, which query correctly extracts each element as a separate row?

Assume table name is events and column is data.

Options:
Snowflake
SELECT value FROM events, LATERAL FLATTEN(input => data);
ASELECT FLATTEN(data) FROM events;
BSELECT data.value FROM events FLATTEN(data);
CSELECT value FROM events, LATERAL FLATTEN(input => data);
DSELECT value FROM events FLATTEN(input => data);
Attempts:
2 left
💡 Hint
LATERAL FLATTEN is used with FROM clause and input parameter.
security
advanced
2:00remaining
Preventing SQL injection with Snowflake functions
Which Snowflake function helps safely handle user input to prevent SQL injection when constructing dynamic SQL queries?
AIDENTIFIER()
BTO_VARCHAR()
CPARSE_JSON()
DTRY_CAST()
Attempts:
2 left
💡 Hint
This function safely treats input as an object name, not code.
Architecture
expert
3:00remaining
Optimizing query performance with clustering keys
You have a large Snowflake table frequently queried by a date column. Which approach best improves query performance for date range filters?
ACreate a materialized view without clustering
BDefine a clustering key on the date column
CUse a multi-cluster warehouse without clustering keys
DPartition the table by date using standard SQL partition syntax
Attempts:
2 left
💡 Hint
Clustering keys physically organize data to speed up range queries.

Practice

(1/5)
1. What does the COUNT(*) function do in a Snowflake SELECT query?
easy
A. Calculates the sum of all numeric values in a column
B. Counts only rows with non-null values in a specific column
C. Counts all rows in the selected table or result set
D. Formats a date value into a string

Solution

  1. Step 1: Understand COUNT(*) function

    The COUNT(*) counts every row regardless of nulls or values.
  2. Step 2: Compare with other options

    Options B, C, and D describe different functions like COUNT(column), SUM(), and TO_CHAR().
  3. Final Answer:

    Counts all rows in the selected table or result set -> Option C
  4. Quick Check:

    COUNT(*) counts all rows [OK]
Hint: COUNT(*) counts every row, no matter what [OK]
Common Mistakes:
  • Confusing COUNT(*) with COUNT(column)
  • Thinking COUNT(*) ignores nulls
  • Mixing COUNT with SUM or TO_CHAR
2. Which of the following is the correct syntax to convert a date column order_date to a string in format 'YYYY-MM-DD' using Snowflake?
easy
A. SELECT FORMAT_DATE(order_date, 'YYYY-MM-DD') FROM orders;
B. SELECT TO_CHAR(order_date, 'YYYY-MM-DD') FROM orders;
C. SELECT TO_STRING(order_date, 'YYYY-MM-DD') FROM orders;
D. SELECT CAST(order_date AS STRING, 'YYYY-MM-DD') FROM orders;

Solution

  1. Step 1: Identify the correct function for date to string

    Snowflake uses TO_CHAR() to format dates as strings.
  2. Step 2: Check syntax correctness

    SELECT TO_CHAR(order_date, 'YYYY-MM-DD') FROM orders; uses TO_CHAR(order_date, 'YYYY-MM-DD'), which is correct syntax. Others use invalid or non-existent functions.
  3. Final Answer:

    SELECT TO_CHAR(order_date, 'YYYY-MM-DD') FROM orders; -> Option B
  4. Quick Check:

    TO_CHAR formats dates to strings [OK]
Hint: Use TO_CHAR to format dates as strings [OK]
Common Mistakes:
  • Using TO_STRING instead of TO_CHAR
  • Trying FORMAT_DATE which doesn't exist in Snowflake
  • Incorrect CAST syntax for formatting
3. Given the table sales with column amount, what is the result of this query?
SELECT SUM(amount) FROM sales WHERE amount > 100;
medium
A. Sum of amounts greater than 100
B. Average of amounts greater than 100
C. Count of rows where amount is greater than 100
D. Sum of all amounts including those less or equal to 100

Solution

  1. Step 1: Understand the WHERE clause effect

    The WHERE clause filters rows to only those with amount > 100.
  2. Step 2: Understand SUM function

    SUM adds all values of amount from the filtered rows.
  3. Final Answer:

    Sum of amounts greater than 100 -> Option A
  4. Quick Check:

    SUM with WHERE filters sums filtered rows [OK]
Hint: WHERE filters rows before SUM calculation [OK]
Common Mistakes:
  • Including amounts less or equal to 100
  • Confusing SUM with COUNT or AVG
  • Ignoring WHERE clause effect
4. What is wrong with this Snowflake query?
SELECT TO_CHAR(order_date, YYYY-MM-DD) FROM orders;
medium
A. The date format string is not enclosed in quotes
B. TO_CHAR cannot be used on dates
C. Missing FROM clause
D. TO_CHAR requires three arguments

Solution

  1. Step 1: Check TO_CHAR syntax

    The format string must be enclosed in single quotes, like 'YYYY-MM-DD'.
  2. Step 2: Identify the error in the query

    The query uses YYYY-MM-DD without quotes, causing a syntax error.
  3. Final Answer:

    The date format string is not enclosed in quotes -> Option A
  4. Quick Check:

    Format strings need quotes in TO_CHAR [OK]
Hint: Always quote format strings in TO_CHAR [OK]
Common Mistakes:
  • Forgetting quotes around format strings
  • Thinking TO_CHAR needs more arguments
  • Ignoring syntax errors from missing quotes
5. You want to find the average order amount rounded to 2 decimal places from the orders table. Which query correctly achieves this in Snowflake?
hard
A. SELECT AVG(ROUND(order_amount, 2)) FROM orders;
B. SELECT AVG(CAST(order_amount AS NUMBER(10,2))) FROM orders;
C. SELECT TO_CHAR(AVG(order_amount), '0.00') FROM orders;
D. SELECT ROUND(AVG(order_amount), 2) FROM orders;

Solution

  1. Step 1: Understand rounding average correctly

    Rounding the average after calculating it gives the correct average rounded to 2 decimals.
  2. Step 2: Analyze each option

    SELECT ROUND(AVG(order_amount), 2) FROM orders; rounds the AVG result, which is correct. SELECT AVG(ROUND(order_amount, 2)) FROM orders; rounds each value before averaging, which changes the result. SELECT TO_CHAR(AVG(order_amount), '0.00') FROM orders; converts to string, not numeric. SELECT AVG(CAST(order_amount AS NUMBER(10,2))) FROM orders; casts each value before averaging, which changes the result.
  3. Final Answer:

    SELECT ROUND(AVG(order_amount), 2) FROM orders; -> Option D
  4. Quick Check:

    Round after AVG for correct decimal rounding [OK]
Hint: Round the average, not individual values [OK]
Common Mistakes:
  • Rounding before averaging changes results
  • Using TO_CHAR instead of numeric rounding
  • Casting individuals before averaging changes results