100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Querying with Snowflake SQL

A practical tour of Snowflake's SQL dialect, covering joins, window functions, common table expressions, and result caching.

Loading & Querying DataIntermediate9 min readJul 10, 2026
Analogies

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.

sql
-- 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

Was this page helpful?

Topics covered

#Programming#SnowflakeStudyNotes#QueryingWithSnowflakeSQL#Querying#Snowflake#SQL#Window#StudyNotes#SkillVeris#ExamPrep