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
Flatten Nested Data Using FLATTEN in Snowflake
📖 Scenario: You work with a Snowflake database that stores customer orders. Each order has a list of items purchased, stored as a nested array. You need to extract each item as a separate row to analyze item sales.
🎯 Goal: Build a Snowflake SQL query that uses the FLATTEN function to convert nested arrays of order items into individual rows for easier analysis.
📋 What You'll Learn
Create a table called orders with columns order_id (integer) and items (variant) containing nested arrays
Insert sample data with nested arrays of items
Write a query using FLATTEN to extract each item from the nested items array
Select order_id and the flattened item values as separate rows
💡 Why This Matters
🌍 Real World
Many cloud data platforms store nested JSON data. Flattening helps analyze each nested element as a separate row for reporting and insights.
💼 Career
Data engineers and analysts often use FLATTEN in Snowflake to work with semi-structured data efficiently.
Progress0 / 4 steps
1
Create the orders table with nested items data
Create a table called orders with columns order_id as INTEGER and items as VARIANT. Insert two rows with order_id values 1 and 2. For items, insert the JSON arrays ["apple", "banana"] for order 1 and ["orange", "grape", "melon"] for order 2.
Snowflake
Hint
Use CREATE OR REPLACE TABLE to create the table. Use PARSE_JSON to insert JSON arrays into the items column.
2
Add a query to flatten the items array
Write a SELECT query that uses the FLATTEN function on the items column from the orders table. Alias the FLATTEN function as f. Select order_id and f.value as item.
Snowflake
Hint
Use LATERAL FLATTEN(input => items) f in the FROM clause to expand the nested array.
3
Filter items to only include those starting with the letter 'g'
Modify the SELECT query to add a WHERE clause that filters the flattened item values to only include those starting with the letter 'g'. Use the LIKE operator with 'g%'.
Snowflake
Hint
Use WHERE f.value LIKE 'g%' to filter items starting with 'g'.
4
Complete the query by ordering results by order_id and item
Add an ORDER BY clause to the SELECT query to sort the results by order_id ascending and then by item ascending.
Snowflake
Hint
Use ORDER BY order_id ASC, item ASC to sort the results.
Practice
(1/5)
1. What does the FLATTEN function do in Snowflake when working with nested data?
easy
A. It encrypts nested data for security.
B. It compresses data to save storage space.
C. It converts nested arrays or objects into simple rows.
D. It creates a backup of nested data.
Solution
Step 1: Understand the purpose of FLATTEN
FLATTEN is designed to take nested arrays or objects and turn them into individual rows so they are easier to query.
Step 2: Compare options to FLATTEN's function
Options A, B, and D describe encryption, compression, and backup, which are unrelated to FLATTEN's role.
Final Answer:
It converts nested arrays or objects into simple rows. -> Option C
Quick Check:
FLATTEN = convert nested data to rows [OK]
Hint: FLATTEN breaks nested data into rows for easy reading [OK]
Common Mistakes:
Thinking FLATTEN compresses or encrypts data
Confusing FLATTEN with backup or storage functions
Assuming FLATTEN changes data format instead of structure
2. Which of the following is the correct syntax to use FLATTEN on a JSON column named data in Snowflake?
easy
A. SELECT FLATTEN(data) FROM table;
B. SELECT * FROM FLATTEN(input => data);
C. SELECT FLATTEN(input = data) FROM table;
D. SELECT * FROM table, LATERAL FLATTEN(input => data);
Solution
Step 1: Recall FLATTEN usage in FROM clause
FLATTEN is used as a table function in the FROM clause with LATERAL to expand nested data.
Step 2: Analyze each option's syntax
SELECT * FROM table, LATERAL FLATTEN(input => data); correctly uses FROM table, LATERAL FLATTEN(input => data). Options A and C misuse FLATTEN as a scalar function. SELECT * FROM FLATTEN(input => data); misses the table reference.
Final Answer:
SELECT * FROM table, LATERAL FLATTEN(input => data); -> Option D
Quick Check:
FLATTEN used with LATERAL in FROM clause [OK]
Hint: Use FLATTEN with LATERAL in FROM clause for nested data [OK]
Common Mistakes:
Using FLATTEN as a scalar function in SELECT
Omitting LATERAL keyword
Not specifying input parameter correctly
3. Given the JSON column data with value '{"items": ["apple", "banana", "cherry"]}', what will the query below return?
SELECT f.value FROM table, LATERAL FLATTEN(input => data:items) f;
medium
A. Rows with values: apple, banana, cherry
B. A single row with the entire array as a string
C. An error because data:items is invalid syntax
D. Rows with keys and values of the JSON object
Solution
Step 1: Understand FLATTEN on JSON array
FLATTEN(input => data:items) expands the array under 'items' into multiple rows, each with one element.
Step 2: Analyze the query output
The query selects f.value, which will be each element: 'apple', 'banana', 'cherry' as separate rows.
Final Answer:
Rows with values: apple, banana, cherry -> Option A
Quick Check:
FLATTEN on array returns each element as a row [OK]
Hint: FLATTEN on JSON array returns each element as a separate row [OK]
Common Mistakes:
Expecting a single row with the whole array
Confusing keys with values in FLATTEN output
Misreading JSON path syntax
4. You wrote this query to flatten nested JSON data:
SELECT f.value FROM table, FLATTEN(input => data:items) f;
But it returns an error. What is the likely cause?
medium
A. Missing LATERAL keyword before FLATTEN
B. Incorrect JSON path syntax in input
C. FLATTEN cannot be used on JSON arrays
D. SELECT statement missing WHERE clause
Solution
Step 1: Identify FLATTEN usage requirements
FLATTEN is a table function that requires LATERAL when used with another table to expand nested data.
Step 2: Check query syntax
The query misses the LATERAL keyword before FLATTEN, causing a syntax error.
Final Answer:
Missing LATERAL keyword before FLATTEN -> Option A
Quick Check:
FLATTEN needs LATERAL in FROM clause [OK]
Hint: Always add LATERAL before FLATTEN in FROM clause [OK]
Common Mistakes:
Forgetting LATERAL keyword
Assuming FLATTEN works without LATERAL
Blaming JSON path syntax instead of syntax structure
5. You have a table with a column orders storing nested JSON arrays of items per order. You want to list each item with its order ID. Which query correctly uses FLATTEN to achieve this?
hard
A. SELECT order_id, FLATTEN(orders) FROM orders_table;
B. SELECT order_id, f.value FROM orders_table, LATERAL FLATTEN(input => orders) f;
C. SELECT order_id, f.value FROM orders_table JOIN FLATTEN(input => orders) f ON TRUE;
D. SELECT order_id, f.value FROM orders_table, FLATTEN(orders) f;
Solution
Step 1: Understand how to join FLATTEN with table
FLATTEN must be used with LATERAL in the FROM clause to expand nested arrays per row.
Step 2: Evaluate each option's correctness
SELECT order_id, f.value FROM orders_table, LATERAL FLATTEN(input => orders) f; correctly uses FROM orders_table, LATERAL FLATTEN(input => orders) f, selecting order_id and each item value. Options A and D misuse FLATTEN syntax. SELECT order_id, f.value FROM orders_table JOIN FLATTEN(input => orders) f ON TRUE; uses JOIN incorrectly without LATERAL.
Final Answer:
SELECT order_id, f.value FROM orders_table, LATERAL FLATTEN(input => orders) f; -> Option B
Quick Check:
Use LATERAL FLATTEN with table to list nested items [OK]
Hint: Use LATERAL FLATTEN(input => column) to expand nested arrays per row [OK]