Introduction to Snowflake SQL
George Boorman
Senior Curriculum Manager, DataCamp
Example of structured data
| cust_id | cust_name | cust_age | cust_email |
|---------|-----------|----------|-----------------------|
| 1 | cust1 | 40 | cust1***@gmail.com |
| 2 | cust2 | 35 | cust2***@gmail.com |
| 3 | cust3 | 42 | cust3***@gmail.com |
Example of semi-structured data
cust_id: 1
Comparisons:
JSONB
VARIANT
VARIANT
supports OBJECT and ARRAY data types{ "key": "value"}
["list", "of", "values"]
CREATE TABLE cust_info_json_data (
customer_id INT,
customer_info VARIANT -- VARIANT data type
);
PARSE_JSON
expr
: JSON data in string formatVARIANT
type, valid JSON objectExample:
SELECT PARSE_JSON(
-- Enclosed in strings
'{
"cust_id": 1,
"cust_name": "cust1",
"cust_age": 40,
"cust_email":"cust1***@gmail.com"
}
'-- Enclosed in strings
) AS customer_info_json
OBJECT_CONSTRUCT
OBJECT_CONSTRUCT( [<key1>, <value1> [, <keyN>, <valueN> ...]] )
SELECT OBJECT_CONSTRUCT(
-- Comma separated values rather than : notation
'cust_id', 1,
'cust_name', 'cust1',
'cust_age', 40,
'cust_email', 'cust1***@gmail.com'
)
Simple JSON
:
SELECT
customer_info:cust_age, -- Use colon to access cust_age from column
customer_info:cust_name,
customer_info:cust_email,
FROM
cust_info_json_data;
Example of nested JSON
:
.
Accessing values using colon notation
<column>:<level1_element>:<level2_element>:<level3_element>
SELECT
customer_info:address:street AS street_name
FROM
cust_info_json_data
Accessing values using dot notation
<column>:<level1_element>.<level2_element>.<level3_element>
SELECT
customer_info:address.street AS street_name
FROM
cust_info_json_data
Introduction to Snowflake SQL