Querying with Snowflake SQL
Snowflake's query language is standard ANSI SQL with a large set of practical extensions layered on top, including native support for semi-structured data access, generous built-in functions for dates and strings, and QUALIFY, a clause unique among most databases that lets you filter directly on window function results without wrapping the query in a subquery. Because Snowflake compiles and optimizes every query against its cloud services layer before execution, the SQL you write looks familiar to anyone who has used PostgreSQL or MySQL, but a few conveniences like QUALIFY and native JSON operators make certain analytical patterns noticeably shorter to express.
Cricket analogy: Like a franchise coach who mostly follows standard T20 tactics but has a few signature plays not seen elsewhere — most of the playbook is familiar cricket, but a couple of moves like a deliberate slower-ball yorker sequence set the team apart.
Window Functions, CTEs, and QUALIFY
Window functions like ROW_NUMBER(), RANK(), and LAG() compute a value across a set of related rows (the 'window') without collapsing them into a single aggregated row, which is essential for tasks like ranking each customer's orders by date or comparing a row to the previous one in a sequence. Common table expressions, defined with WITH, let you break a complex query into named, readable steps that reference each other, and QUALIFY then lets you filter on the result of a window function directly in the outer query, avoiding the common pattern of wrapping a ROW_NUMBER() query in a subquery just to filter WHERE rn = 1.
Cricket analogy: Like ranking every batsman's innings within a series without collapsing the series into one team total — a window function keeps each innings visible, and QUALIFY then lets you filter straight to 'show me only each player's highest score' without a separate results table.
Result Caching and Query Performance
Snowflake maintains a result cache at the cloud services layer that stores the output of every query for 24 hours; if an identical query text runs again against unchanged underlying data, Snowflake returns the cached result instantly without spinning up any warehouse compute at all, making that repeat query effectively free. Separately, each virtual warehouse maintains a local data cache of recently scanned micro-partitions in memory and SSD, so even non-identical queries touching the same table tend to run faster on a 'warm' warehouse than on one that just resumed from suspension.
Cricket analogy: Like a broadcaster replaying an already-produced highlight clip instantly instead of re-editing raw footage from scratch — the result cache is that finished clip, while a warm warehouse is like an editing suite that already has today's footage loaded and ready to cut faster.
-- CTE + window function + QUALIFY: latest order per customer
WITH recent_orders AS (
SELECT
customer_id,
order_id,
order_date,
total_amount
FROM sales.orders
WHERE order_date >= DATEADD(month, -6, CURRENT_DATE())
)
SELECT
customer_id,
order_id,
order_date,
total_amount
FROM recent_orders
QUALIFY ROW_NUMBER() OVER (
PARTITION BY customer_id
ORDER BY order_date DESC
) = 1;The result cache only serves a repeat query when the query text is byte-for-byte identical (whitespace and casing differences can still match after normalization, but logic changes cannot) and the underlying tables have not changed since the cached result was produced. Any DML on the source table, even an unrelated update to a different row, invalidates the cache for queries against that table.
- Snowflake SQL is standard ANSI SQL extended with conveniences like QUALIFY and native JSON operators.
- Window functions compute values across related rows without collapsing them into a single aggregate.
- CTEs (WITH clauses) break complex queries into named, readable, composable steps.
- QUALIFY filters directly on window function results, avoiding a wrapper subquery around ROW_NUMBER().
- The result cache serves identical repeat queries instantly for 24 hours without using warehouse compute.
- Each warehouse keeps a local data cache of recently scanned micro-partitions for faster warm-warehouse queries.
- Any change to a source table invalidates the result cache for queries against that table.
Practice what you learned
1. What does the QUALIFY clause let you do in Snowflake SQL?
2. What is the key difference between a window function and a standard aggregate function like SUM() with GROUP BY?
3. How long does Snowflake's result cache typically retain a query's result?
4. What invalidates the result cache for queries against a given table?
5. What kind of query construct do CTEs (WITH clauses) provide?
Was this page helpful?
You May Also Like
Semi-Structured Data: JSON and VARIANT
Learn how Snowflake's VARIANT data type stores semi-structured JSON, and how to query, flatten, and optimize access to nested data.
Views and Materialized Views
Understand the difference between standard views, secure views, and materialized views in Snowflake, and when to use each.
Loading Data with COPY INTO
Learn how Snowflake's COPY INTO command bulk-loads data from staged files into tables, including file formats, load options, parallelism, and load history.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics
ProgrammingPowerShell Study Notes
Programming · 30 topics