Database / Snowflake Interview Questions
How does Snowflake handle semi-structured data (JSON, Avro, Parquet, ORC) with the VARIANT type?
Snowflake stores semi-structured data in a VARIANT column — a flexible container that holds any valid JSON, Avro, Parquet, ORC, or XML value up to 16 MB per cell. Unlike traditional columns, VARIANT does not require a predefined schema. You can load raw JSON with an evolving structure and query the fields you need at read time via dot and bracket notation.
Internally, Snowflake parses and columnarizes VARIANT data at load time, extracting paths and types into an efficient encoding. This allows the optimizer to apply column-level statistics on frequently accessed paths, enabling partial partition pruning even for semi-structured data.
- Dot notation:
src:customer.address.city::STRING— navigates nested objects. - Bracket notation:
src['customer']['address']— equivalent to dot notation. - Array indexing:
src:items[0]:product_id::NUMBER. - FLATTEN: lateral table function that explodes arrays or objects into rows for relational processing.
- Type casting:
::STRING,::NUMBER,::TIMESTAMP_NTZcast extracted values to typed columns for better statistics and comparison performance.
-- Table with a VARIANT column for raw JSON events
CREATE TABLE events (
event_id NUMBER AUTOINCREMENT,
raw VARIANT,
loaded_at TIMESTAMP_NTZ DEFAULT CURRENT_TIMESTAMP()
);
-- Query nested JSON using dot notation with type cast
SELECT
raw:user_id::STRING AS user_id,
raw:event_type::STRING AS event_type,
raw:properties:page::STRING AS page
FROM events
WHERE raw:event_type::STRING = 'pageview';
-- FLATTEN an array of order items into rows
SELECT
o.raw:order_id::STRING AS order_id,
item.value:sku::STRING AS sku,
item.value:qty::NUMBER AS qty
FROM orders o,
LATERAL FLATTEN(INPUT => o.raw:items) item;
More Related questions...