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;
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
