CertSafari
    Snowflake SnowPro Advanced: Data Scientist (DSA-C03)· Lessons

    Domain 2 · Lesson 6/16

    Exploratory data analysis in Snowflake with SQL and Snowpark

    Perform exploratory data analysis in Snowflake.

    21 min read
    6.75% of exam
    13 sources
    Published 20 Oct 2026
    Docs as of 18 Oct 2026

    What you will be able to do

    • Get a first statistical view of a result set in Snowsight without writing a query
    • Compute centre, range and spread with MIN, MAX, AVG, STDDEV and VARIANCE, choosing the sample or population version
    • Profile a large, wide table in a single scan using exact and approximate aggregates
    • Use window functions and ordered TOP n to get per-row statistics and repeatable row choices
    • Fit a quick least-squares line with REGR_SLOPE and REGR_INTERCEPT, with the variables in the right order
    • Explore Snowflake data from an external Jupyter notebook through a Snowpark session that pushes work to the warehouse

    Key concept

    Summarise where the data lives — In Snowflake, exploratory statistics are computed by the warehouse next to the data, and only small summaries come back to the analyst. Each technique below is a way to ask a bigger question in fewer, cheaper scans without moving raw rows to a client.

    1.A first look in Snowsight

    Exploratory data analysis (EDA) is the step where you learn what a table actually contains before you decide how to clean it, which features to build or which model to try. You want to know its ranges, its gaps, its most common values and how its columns move together. Getting that wrong early is costly, because every later step inherits the misunderstanding.

    Snowsight gives you that first look without any extra SQL. When a worksheet query returns, the results grid works like a spreadsheet: you can select columns, cells, rows or ranges. For result sets of up to 1 million rows, Snowsight generates statistics for whatever you select, along with overall statistics for the result. Run SELECT * FROM customers SAMPLE (1) and click a column, and you see its shape straight away.

    These statistics only describe the result set that came back. They do not describe the table behind it. A filtered query, a sample or a result truncated at the row limit can each show a different picture from the full table. Use the panel to decide what to ask next, then answer that question with aggregate SQL that runs over every row.

    Checkpoint 1 of 10· Check yourself

    You run SELECT * FROM customers SAMPLE (1) in a Snowsight worksheet. You click the credit_limit column, and the statistics panel shows values from 500 to 20,000. What should you conclude, and what should you do next?

    Sources1

    2.Centre, range and spread with SQL aggregates

    Snowflake's general aggregate functions give you descriptive statistics for any number of rows in one pass. MIN and MAX give the range, AVG gives the centre, and the variance and standard deviation functions give the spread. Each of them can also run as a window function, which comes up later.

    The sample/population split is the detail that catches people. VAR_SAMP returns the sample variance of the non-NULL values: the sum of squared differences from the mean, divided by n − 1. VARIANCE and VARIANCE_SAMP are aliases of VAR_SAMP. Use the sample version when the rows are a sample drawn from a larger population, such as last month's orders standing in for customer behaviour in general. Use VAR_POP, which divides by n, when the rows are the whole population. Standard deviation is the square root of variance, and it has the same pair: STDDEV_SAMP and STDDEV_POP.

    Variance functions and their divisors
    FunctionDivides byUse whenSingle non-NULL row returns
    VAR_SAMP (aliases VARIANCE, VARIANCE_SAMP)n − 1The rows are a sample from a larger populationNULL
    VAR_POPnThe rows are the entire population—

    Two behaviours affect how you read the output. All of these functions skip NULLs, so the spread describes only the values that are present. Report it next to a count of what is missing. Also, a group with only one non-NULL value returns NULL from VAR_SAMP, VARIANCE and VARIANCE_SAMP, not 0. If a GROUP BY produces many single-row groups, expect a column full of NULL spreads.

    Checkpoint 2 of 10· Check yourself

    A feature table holds 500 million card transactions. Before you build outlier features, you need the range, centre and spread of amount for each merchant_category. Which approach fits best?

    Sources23

    3.Approximate aggregates for large tables

    On large tables, exact distinct counts and exact frequency tables are the expensive part of profiling. Snowflake offers approximate versions that trade a small, known error for far less memory and time. Use them whenever an estimate is enough to make the decision in front of you.

    HyperLogLog estimates how many distinct values there are. APPROX_COUNT_DISTINCT (also available as HLL) can replace COUNT(DISTINCT col). Snowflake's implementation has an average relative error of 1.62338%. Where an exact count would return 1,000,000, the estimate typically falls between 983,767 and 1,016,234. Its state needs at most 4096 bytes per aggregation group, and as little as about 32 bytes when cardinality is small. That makes it practical to estimate cardinality over millions of groups, using orders of magnitude less memory and CPU than COUNT(DISTINCT …).

    HyperLogLog states can be saved and combined, which solves the rollup problem. Distinct counts do not add up: a user active on Monday and Tuesday appears in both daily counts, so summing seven daily figures overstates weekly users. Instead, keep the daily state with HLL_ACCUMULATE. Later, merge the days of a week or month with HLL_COMBINE and read off the estimate with HLL_ESTIMATE. Returning users are counted once, and the raw events are never scanned again. In one documented comparison, aggregating stored HLL states took 1.3 seconds against 149 seconds over the base data.

    APPROX_TOP_K estimates the most frequent values using the Space-Saving algorithm. How accurate it is depends on the data. The more skewed the distribution, and the more counters the algorithm uses, the closer the output is to the true counts. Each counter costs about 100 bytes per aggregation group, and both the number of counters and k are capped at 100 thousand. Answering 'which ten error codes dominate these 20 billion log rows?' therefore needs a small, bounded amount of memory instead of an exact count for every distinct code.

    Exact and approximate options for cardinality and frequency
    FunctionReturnsReach for it when
    COUNT(DISTINCT col)Exact number of distinct valuesThe table is small or the exact figure matters
    APPROX_COUNT_DISTINCT / HLLDistinct count within about 1.6% on averageThe input is large and an estimate is enough
    HLL_ACCUMULATE, HLL_COMBINE, HLL_ESTIMATEStored states that merge into longer periodsDaily distinct counts must roll up to weeks or months without rescanning
    APPROX_TOP_KApproximate most frequent values with countsYou need the top values of a high-volume column and approximate counts are acceptable

    Checkpoint 3 of 10· Match them up

    Match each profiling need to the Snowflake function, or set of functions, that fits it best.

    Tap a term, then the definition that fits it.

    Sources45

    4.Window functions and ordered TOP n

    A window function computes a statistic for each row without collapsing the rows. An aggregate function takes a group of rows and returns one row. A window function takes each row in a partition and returns one row for each input row. Adding OVER (PARTITION BY menu_category) to AVG(menu_cogs_usd) keeps all 60 menu items and puts the category average beside each one. You can then compare an item with its peers in the same query.

    Adding ORDER BY and a window frame inside OVER turns the statistic into a rolling one. The query below computes a moving average over the current row and the two rows after it, within each category. For the last row in a partition there are no following rows, so its 'average' is just its own value.

    A moving average: AVG over a three-row frame, with menu_cogs_usd as a tiebreaker in the ORDER BYsql
    SELECT menu_category, menu_price_usd, menu_cogs_usd,
        AVG(menu_cogs_usd) OVER(PARTITION BY menu_category
          ORDER BY menu_price_usd, menu_cogs_usd ROWS BETWEEN CURRENT ROW and 2 FOLLOWING) avg_cogs
      FROM menu_items
      ORDER BY menu_category, menu_price_usd, menu_cogs_usd
      LIMIT 15;

    Several rules in that query are easy to get wrong:

    • The ordering must be deterministic. Several items share a price, so menu_cogs_usd is added as a tiebreaker. Without it, the rows that land in each frame could change from one run to the next.
    • The ORDER BY inside OVER controls only the order in which the window function processes rows. The final ORDER BY sorts the output, and it is a separate clause.
    • Ordinal positions do not work inside OVER. In OVER (ORDER BY 2), the 2 is the constant 2, not the second column.
    • For some functions, an ORDER BY implies a window frame. Snowflake recommends declaring frames explicitly so the behaviour is not hidden.

    The same determinism rule applies to picking the 'top' rows of a whole result. TOP n and LIMIT n are equivalent. Without an ORDER BY, the rows returned are non-deterministic, because a result set has no inherent order. SELECT TOP 10 customer_id, SUM(spend) ... GROUP BY customer_id can return a different ten customers each time it runs. Adding ORDER BY total DESC with a unique tiebreaker makes it the actual top ten. Keep TOP n and its ORDER BY at the same nesting level, because splitting them across a subquery and an outer query can make the results unpredictable. For profiling, the consequence is that LIMIT 1000 returns an arbitrary thousand rows, not a representative slice of the table.

    Checkpoint 4 of 10· Fill the gap

    This query should keep all 60 menu items and put a three-row moving average of cost beside each one, with the average restarting for each menu category. Which keyword fills the blank?

    SELECT menu_category, menu_price_usd, menu_cogs_usd,
        AVG(menu_cogs_usd) OVER( ?  BY menu_category
          ORDER BY menu_price_usd, menu_cogs_usd ROWS BETWEEN CURRENT ROW and 2 FOLLOWING) avg_cogs
      FROM menu_items
      ORDER BY menu_category, menu_price_usd, menu_cogs_usd
      LIMIT 15;

    Sources637

    5.Profiling a wide table in one scan

    Profiling a table with hundreds of millions of rows comes down to how many times you scan it. Every aggregate above can share one SELECT. A single query over the table can return COUNT(*) and, for each column, COUNT_IF(col IS NULL) (or COUNT(col)), MIN(col), MAX(col) and APPROX_COUNT_DISTINCT(col). Null ratio, value range and approximate cardinality for all 30 columns then come from one pass. Add AVG and STDDEV_SAMP for the numeric columns while you are at it.

    The shortcuts that look cheaper all give you less information or cost more:

    • Reading the first thousand rows with LIMIT gives you an arbitrary slice with no ORDER BY. Its null counts say nothing reliable about the other 399,999,000 rows.
    • Running SELECT DISTINCT col once per column means 30 scans. Each one returns every distinct value, which is the most expensive way to learn a cardinality, and none of them measures completeness.
    • Pulling the whole table into a local pandas DataFrame moves every row across the network into client memory, just to compute numbers the warehouse could have returned as one row.

    Checkpoint 5 of 10· Check yourself

    Before feature engineering, a teammate has to report the null ratio, value range and approximate number of distinct values for all 30 columns of a 400-million-row events table. Which plan gives all of this most efficiently?

    Checkpoint 6 of 10· Exam question

    A data scientist must profile a 400M-row CUSTOMERS table with 30 columns before building a churn model, scanning the table as few times as possible. Select TWO approaches that fit this goal.(Select 2)

    0 of 2 selected

    Sources87

    6.A quick regression line with REGR_SLOPE and REGR_INTERCEPT

    Once you have a candidate relationship, such as revenue against ad spend, Snowflake can fit a least-squares line in SQL. That tells you whether the relationship is worth modelling before you build anything. The model itself is covered in Linear regression. In EDA you need only the slope and the intercept.

    Argument order is the dependent variable first: REGR_SLOPE(y, x) and REGR_INTERCEPT(y, x), where y is the variable being explained and x the explanatory one. The slope is COVAR_POP(x,y) / VAR_POP(x). Swap the arguments and the denominator becomes the variance of the other column, so you get a different line: the line that predicts ad spend from revenue. To predict revenue from ad_spend, write REGR_SLOPE(revenue, ad_spend).

    Slope per group; group 1 has no complete (y, x) pair, so it returns NULLsql
    CREATE OR REPLACE TABLE aggr(k INT, v DECIMAL(10,2), v2 DECIMAL(10, 2));
    INSERT INTO aggr VALUES(1, 10, null);
    INSERT INTO aggr VALUES(2, 10, 11), (2, 20, 22), (2, 25, null), (2, 30, 35);
    
    SELECT k, REGR_SLOPE(v, v2) FROM aggr GROUP BY k;

    Not unless you filter first. The REGR functions use only non-NULL pairs. Group 2 returns a slope of 0.831408776 from three complete pairs, and its intercept is AVG(y) - REGR_SLOPE(y,x) * AVG(x) evaluated over those same pairs. A plain AVG(v) also counts the (25, null) row, so the mean moves from 20 to 21.25 and the hand-computed intercept is wrong. Add WHERE v IS NOT NULL AND v2 IS NOT NULL and the manual formula matches REGR_INTERCEPT. Group 1 has no complete pair, so every REGR function returns NULL for it.

    REGR_R2(y, x) reports how well the line fits, but it cannot tell you which variable is dependent. It equals POWER(CORR(y,x), 2), and correlation is symmetric, so R² comes out the same in either argument order (0.9976905312 for group 2), even though slope and intercept change. The choice of dependent variable comes from the question you are asking, and the argument order has to match it. As window functions, all three accept PARTITION BY but not an ORDER BY or an explicit frame inside OVER.

    Checkpoint 7 of 10· Check yourself

    An analyst wants a quick least-squares line that predicts weekly revenue from ad_spend, before deciding whether the relationship is worth modelling. Which statement is correct?

    Sources91011

    7.Exploring from a Jupyter notebook through Snowpark

    A Jupyter notebook on a laptop can explore Snowflake data over a live connection. You do not need exports, local copies or a special runtime. You create a Snowpark session from connection parameters (account, user, credentials, role, warehouse, database, schema), and from then on the notebook builds queries that run in Snowflake. Sessions and the multi-language API are covered in Snowpark sessions and the multi-language Snowpark API.

    Opening a Snowpark session from connection parameters loaded in the notebookpython
    session = Session.builder.configs(connection_parameters).create()

    Snowpark DataFrames are evaluated lazily, which is why this is efficient. session.table('ORDERS').filter(...).group_by('REGION').agg(...) sends nothing to Snowflake while you chain the calls. Each call adds to a description of the query. Only an action runs it:

    • collect returns the result as a list of Row objects.
    • count returns the number of rows.
    • show prints rows, 10 by default.
    • save_as_table writes the result to a table.

    When the action runs, the whole chain goes to the server as one SQL statement. The warehouse filters and aggregates next to the data, and the notebook receives only the per-region averages. There are no intermediate tables, and nothing is filtered on the client.

    The same rule applies with any client library: write the query so the aggregation or sample happens in Snowflake, and bring back only the small result for plotting or pandas work. Inside Snowsight, Snowflake Notebooks with open-source plotting libraries run the same EDA queries in cells and pass results to Python through the notebook's built-in session, so there is no connection to set up.

    Checkpoint 8 of 10· Check yourself

    From a Jupyter notebook with a Snowpark session open, you want to plot the average order amount per region for a 300-million-row ORDERS table. Which approach returns only what the plot needs?

    Checkpoint 9 of 10· Exam question

    A Snowpark Python script runs df = session.table('ORDERS').filter(col('STATUS') == 'SHIPPED').group_by('REGION').agg(avg('AMOUNT')) in a Jupyter notebook. Select TWO statements that correctly describe when and how Snowflake executes this work.(Select 2)

    0 of 2 selected

    Checkpoint 10 of 10· Exam question

    Daily distinct-user counts must roll up into weekly and monthly figures without rescanning raw events and without double-counting users who return on several days. Which approach is MOST appropriate?

    Sources1213

    Practise it for real

    Fit and check a least-squares line in SQL, and see how argument order and NULLs change the answer

    1. 1.Create the aggr table and insert the five rows: CREATE OR REPLACE TABLE aggr(k INT, v DECIMAL(10,2), v2 DECIMAL(10, 2)); then the two INSERT statements from the regression section.

      Why: Group 1 has no complete pair and group 2 has one row with a NULL independent value, which covers both NULL cases.

      You should see: Table created, with five rows inserted.

    2. 2.Run SELECT k, REGR_SLOPE(v, v2), REGR_INTERCEPT(v, v2) FROM aggr GROUP BY k;

      Why: v is the dependent variable, so it goes first.

      You should see: k = 1 returns NULL for both. k = 2 returns a slope of 0.831408776 and an intercept of 1.154734411.

    3. 3.Run SELECT k, AVG(v) - REGR_SLOPE(v, v2) * AVG(v2) FROM aggr GROUP BY k; then run it again with WHERE v IS NOT NULL AND v2 IS NOT NULL.

      Why: The REGR functions use only non-NULL pairs, but a plain AVG does not.

      You should see: Only the filtered version reproduces 1.154734411 for k = 2.

    4. 4.Run SELECT k, REGR_SLOPE(v2, v), REGR_R2(v, v2), REGR_R2(v2, v) FROM aggr GROUP BY k;

      Why: Swapping the arguments shows which statistics depend on the choice of dependent variable.

      You should see: The swapped slope for k = 2 is not 0.831408776, while both R² values are 0.9976905312.

    Stuck? Get a nudge

    If the hand-computed intercept is close but not equal, check whether the (2, 25, null) row is still in your averages.

    Sources

    (13)

    Every claim above is drawn from one of these pages, quoted as it was written on the date shown.

    1. 1.
      “For up to 1 million rows of results, you can review generated statistics that display contextual information for any selection”
      ↩︎ A first look in Snowsight
    2. 2.
      “Returns the sample variance of non-NULL records in a group.”
      ↩︎ Centre, range and spread with SQL aggregates
      “For single-record inputs, VAR_SAMP, VARIANCE, and VARIANCE_SAMP all return NULL.”
      ↩︎ Centre, range and spread with SQL aggregates
      “Use the sample variance when the values are a sample drawn from a larger population.”
      ↩︎ Exam trap 1
      “When the values are the entire population, use VAR_POP, which divides by the number of values (n).”
      ↩︎ Prediction
    3. 3.
      “COUNT, SUM, MIN, MAX, AVG STDDEV, STDDEV_SAMP, STDDEV_POP (and aliases)”
      ↩︎ Centre, range and spread with SQL aggregates
      “The ORDER BY clause for window functions does not support the use of an ordinal position”
      ↩︎ Window functions and ordered TOP n
    4. 4.
      “We recommend using HyperLogLog whenever the input is potentially large and an approximate result is acceptable.”
      ↩︎ Approximate aggregates for large tables
      “The average relative error of our HyperLogLog implementation is 1.62338%”
      ↩︎ Approximate aggregates for large tables
      “aggregation of the HLL structures is significantly faster than aggregation over the base data”
      ↩︎ Approximate aggregates for large tables
      “HLL_ACCUMULATE: Skips the final estimation step and returns the HyperLogLog state at the end of an aggregation.”
      ↩︎ Exam trap 2
      “HLL_ACCUMULATE: Skips the final estimation step and returns the HyperLogLog state at the end of an aggregation.”
      ↩︎ Checkpoint
      “We recommend using HyperLogLog whenever the input is potentially large and an approximate result is acceptable.”
      ↩︎ Checkpoint
    5. 5.
      “As data becomes more skewed, or more counters are used, the output will be more accurate.”
      ↩︎ Approximate aggregates for large tables
      “The maximum number of k is 100 thousand.”
      ↩︎ Approximate aggregates for large tables
    6. 6.
      “For a window function, the input is each row within a partition, and the output is one row per input row.”
      ↩︎ Window functions and ordered TOP n
      “When using window functions with ORDER BY clauses, ensure that the ordering is deterministic.”
      ↩︎ Window functions and ordered TOP n
      “An ORDER BY clause within an OVER clause controls only the order in which the window function processes rows”
      ↩︎ Window functions and ordered TOP n
      “For an aggregate function, the input is a group of rows, and the output is one row.”
      ↩︎ Checkpoint
      “For an aggregate function, the input is a group of rows, and the output is one row.”
      ↩︎ Checkpoint
      “For an aggregate function, the input is a group of rows, and the output is one row.”
      ↩︎ Checkpoint
    7. 7.
      “TOP n and LIMIT count are equivalent.”
      ↩︎ Window functions and ordered TOP n
      “When TOP <n> and ORDER BY are at different nesting levels in a query, results can be unpredictable.”
      ↩︎ Window functions and ordered TOP n
      “without an ORDER BY clause, the results are non-deterministic because results within a result set are not necessarily in any particular order.”
      ↩︎ Profiling a wide table in one scan
      “without an ORDER BY clause, the results are non-deterministic because results within a result set are not necessarily in any particular order.”
      ↩︎ Exam trap 3
    8. 8.
      “If you just need an approximate count of distinct values, you can use the HyperLogLog functions (e.g. APPROX_COUNT_DISTINCT).”
      ↩︎ Profiling a wide table in one scan
    9. 9.
      “Note the order of the arguments; the dependent variable is first.”
      ↩︎ A quick regression line with REGR_SLOPE and REGR_INTERCEPT
      “Note the order of the arguments; the dependent variable is first.”
      ↩︎ Exam trap 4
    10. 10.
      “Returns the intercept of the univariate linear regression line for non-null pairs in a group.”
      ↩︎ A quick regression line with REGR_SLOPE and REGR_INTERCEPT
    11. 12.
      “An action causes the DataFrame to be evaluated and sends the corresponding SQL statement to the server for execution.”
      ↩︎ Exploring from a Jupyter notebook through Snowpark
      “An action causes the DataFrame to be evaluated and sends the corresponding SQL statement to the server for execution.”
      ↩︎ Key concept

    Ready to test yourself?

    Practise the 24 questions on this subdomain.

    Spotted a mistake, or was something unclear? Tell us.