Cert EspressoLearn. Practise. Find your flow.

A LITTLE FOCUS.
A BIG NEXT STEP.

Your pace. Your space.
Snowflake · SnowPro Core

Aggregates, Window Functions, QUALIFY, and CTEs

SnowPro Core · Lesson 24 of 27 · 2:56:36

YouTube loads only after you press play. This chapter opens at its timestamp in the full course. Watch on YouTube ↗

Learning with these videos? Subscribe for new courses and exam-style questions.

In this lesson

Prepare for the SnowPro Core Certification exam (COF-C03). In this lesson: aggregate and estimation functions, window functions and QUALIFY, common table expressions, and efficient SQL. Follows Foxglove Apparel, a fictional online clothing retailer. Includes three original practice questions.

Study notes

The text of this lesson, to read at your own pace. Information as of November 2026.

Four questions from analysts

Foxglove's analysts want revenue by region, the top three products in each region, a running total of daily sales, and the number of distinct visitors among billions of clicks. Each question calls for a different kind of SQL function. Choosing the right one makes the query simpler, and often much cheaper.

Aggregate functions

Aggregate functions, such as SUM, AVG, COUNT, MIN, MAX, and MEDIAN, turn many rows into a single value. With GROUP BY, they return one row per group, and HAVING filters those groups after the aggregation. Most aggregate functions ignore NULL values, so the average of one, five, and NULL is three, not two. LISTAGG joins values into a string, and ARRAY_AGG gathers them into an array.

Estimation functions

Counting distinct values exactly, across billions of rows, takes a lot of memory and time. APPROX_COUNT_DISTINCT, also called HLL, uses the HyperLogLog algorithm, with an average relative error of about 1.6 percent. Snowflake recommends it whenever the input is potentially large and an approximate result is acceptable. Other estimation functions cover the most frequent values, with APPROX_TOP_K, and percentiles, with APPROX_PERCENTILE.

Window functions

A window function computes a value for each row, from a window of related rows, without collapsing the rows. The OVER clause defines the window: PARTITION BY splits the rows into groups, ORDER BY sorts them inside each group, and a frame can limit the rows used. ROW_NUMBER, RANK, and DENSE_RANK rank rows, and LAG and LEAD read the previous or the next row. Aggregate functions such as SUM and AVG also work over a window, for running totals and moving averages.

QUALIFY

QUALIFY does for window functions what HAVING does for aggregates: it filters rows on their results. It is evaluated after the window functions, after WHERE, GROUP BY, and HAVING, and before DISTINCT, ORDER BY, and LIMIT. Without it, you would compute the window in a subquery, and filter it in an outer query. ROW_NUMBER gives unique numbers, while RANK gives tied rows the same rank and skips the following ones, and DENSE_RANK leaves no gaps.

Common table expressions

A common table expression, or CTE, is a named subquery defined in a WITH clause, at the start of a query. It makes long queries readable, step by step, and the main query can reference it several times. A recursive CTE references itself, with an anchor clause, then UNION ALL, then a recursive clause. It walks a hierarchy, such as a product category tree, without knowing in advance how many levels there are.

Writing efficient SQL

Select only the columns you need, since storage is columnar, and filter as early as possible, ideally on columns that prune well. Join on complete conditions to avoid exploding joins, and prefer UNION ALL when duplicates do not matter. Choose estimation functions when an estimate is enough, and QUALIFY to filter window results without nesting. Then check the Query Profile, since the plan shows what really helped.

Recap

Let's recap. Aggregate functions turn rows into one value per group, HAVING filters the groups, and most aggregates ignore NULL values. APPROX_COUNT_DISTINCT approximates distinct counts with an average error of about 1.6 percent. Window functions compute a value for each row, over a partition, an order, and an optional frame. QUALIFY filters on window results, and ROW_NUMBER, RANK, and DENSE_RANK treat ties differently. And CTEs, selective columns, early filters, and UNION ALL keep SQL readable and efficient.

Quick checks for this lesson

The 3 questions of this lesson, as plain text. The button above runs them with shuffled choices and the reason behind every choice.

  1. In a Foxglove table, the discount column holds four values: 10, 20, NULL, NULL. What does AVG(discount) return?

    • 7.5
    • NULL
    • 15
    • 30
    Show the answer

    15

    AVG ignores the NULL values, so it divides thirty by two. “7.5” divides by four, as if the NULL values counted as rows. “NULL” would be the result only if every value were NULL. And “30” is the sum of the values, not their average.

  2. Foxglove wants exactly one row per customer, with that customer’s latest order, even when two orders share the same timestamp. Which clause fits?

    • QUALIFY RANK() OVER (PARTITION BY cust_id ORDER BY ts DESC) = 1
    • QUALIFY ROW_NUMBER() OVER (PARTITION BY cust_id ORDER BY ts DESC) = 1
    • HAVING ts = MAX(ts)
    • WHERE ROW_NUMBER() OVER (PARTITION BY cust_id ORDER BY ts DESC) = 1
    Show the answer

    QUALIFY ROW_NUMBER() OVER (PARTITION BY cust_id ORDER BY ts DESC) = 1

    ROW_NUMBER gives each row in the partition a unique number, so exactly one row per customer passes the filter. “QUALIFY RANK() OVER (PARTITION BY cust_id ORDER BY ts DESC) = 1” gives tied orders the same rank, so a customer could return two rows. “HAVING ts = MAX(ts)” is not valid, because HAVING filters groups, and order time is not grouped. And “WHERE ROW_NUMBER() OVER (PARTITION BY cust_id ORDER BY ts DESC) = 1” is not valid either, since WHERE is evaluated before window functions.

  3. Foxglove’s marketing team counts distinct visitors per day across 200 billion clicks. The exact count is slow, and a 2% error is acceptable. Which function fits best?

    • COUNT(DISTINCT visitor_id)
    • COUNT(visitor_id)
    • LISTAGG(DISTINCT visitor_id)
    • APPROX_COUNT_DISTINCT(visitor_id)
    Show the answer

    APPROX_COUNT_DISTINCT(visitor_id)

    APPROX_COUNT_DISTINCT approximates the count with HyperLogLog, with an average error under two percent, far faster. “COUNT(DISTINCT visitor_id)” is the exact count, which is the slow query they already have. “COUNT(visitor_id)” counts rows that have a visitor, not distinct visitors. And “LISTAGG(DISTINCT visitor_id)” builds a long string of values, without counting them.