| name | snowflake-sql |
| description | Snowflake-specific SQL patterns: QUALIFY for window filtering, LATERAL FLATTEN for arrays, semi-structured VARIANT data, ILIKE for case-insensitive matching, date functions, and time travel. |
| type | skill |
Snowflake SQL Skill
1. Window Function Filtering - Use QUALIFY
Instead of wrapping in a subquery, use QUALIFY:
SELECT customer_id, order_date, amount
FROM orders
QUALIFY ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_date DESC) = 1;
SELECT product_id, total_sales
FROM sales_summary
QUALIFY DENSE_RANK() OVER (ORDER BY total_sales DESC) <= 5;
2. Case-Insensitive Matching - Use ILIKE
WHERE product_name ILIKE '%widget%'
WHERE UPPER(status) = 'ACTIVE'
WHERE status ILIKE 'active'
3. Arrays and Semi-Structured Data - LATERAL FLATTEN
SELECT t.id, f.value AS item
FROM table t,
LATERAL FLATTEN(input => t.array_col) f;
SELECT col:field_name::STRING AS field_value
FROM table;
SELECT PARSE_JSON(json_col):key::STRING AS val
FROM table;
4. Date Functions
DATEADD(day, 7, order_date)
DATEADD(month, -1, current_date())
DATEDIFF(day, start_date, end_date)
DATEDIFF(month, start_date, end_date)
DATE_TRUNC('month', event_ts)
DATE_TRUNC('year', event_ts)
CURRENT_TIMESTAMP()
CURRENT_DATE()
5. String Functions
SPLIT_PART(col, '/', 1)
REGEXP_SUBSTR(col, '[0-9]+')
TRIM(col)
LTRIM(col, '0')
UPPER(col) / LOWER(col)
CONCAT(col1, '-', col2)
6. Null-Safe Equality
col1 IS NOT DISTINCT FROM col2
COALESCE(col, 'unknown')
7. Time Travel (querying historical data)
SELECT * FROM my_table AT (OFFSET => -3600);
SELECT * FROM my_table AT (TIMESTAMP => '2024-01-01'::TIMESTAMP);
8. Common Anti-Patterns to Avoid
- Do NOT use
= NULL - use IS NULL
- Do NOT use
<> for NULL comparison - use IS NOT NULL
- Prefer
QUALIFY over subquery wrapping for window filters
- When accessing VARIANT fields, always cast:
col:field::STRING
9. Benchmark Patterns
- Numeric precision: Snowflake returns DECIMAL/NUMBER with configurable precision. Do NOT cast to FLOAT unless needed - precision loss fails exact-match evaluation.
- IDENTIFIER case: Snowflake upper-cases identifiers by default. Use double-quotes
"lower_case_col" when column names are lowercase in source. Always check with describe_table.
- LISTAGG: Use
LISTAGG(col, ',') WITHIN GROUP (ORDER BY col) for string aggregation (not GROUP_CONCAT).
- TRY_CAST / TRY_TO_NUMBER: Use for safe type conversion that returns NULL instead of error.
- OBJECT_KEYS / ARRAY_SIZE: Useful for introspecting semi-structured data before querying.