CertSafari
    Databricks Certified Data Analyst Associate· Lessons

    Domain 4 · Lesson 13/39

    Mean, Median and Standard Deviation in Databricks SQL

    Perform aggregate operations such as count, approximate count distinct, mean, and summary statistics.

    9 min read
    2.56% of exam
    5 sources
    Published 3 Oct 2026
    Docs as of 30 Sep 2026

    What you will be able to do

    • Use avg and mean interchangeably and predict their result type and their behaviour with NULLs, empty groups and DISTINCT
    • Handle overflow in an average with try_avg instead of avg
    • Compute summary statistics with median and stddev, and explain when each one returns NULL

    1.avg and mean: one function under two names

    In Databricks SQL, avg and mean are the same function. The avg page says it 'is a synonym for mean aggregate function', and the mean page says the reverse. Both have the form avg([ALL | DISTINCT] expr) [FILTER (WHERE cond)], and both can be used as window functions with OVER. If an exam option uses mean where you expected avg, it is not a trick.

    avg over a group with one NULL; the documented result is 1.5sql
    SELECT avg(col) FROM VALUES (1), (2), (NULL) AS tab(col);

    The answer is 1.5. NULLs are not counted as zero. They are left out of both the sum and the row count: 'Nulls within the group are ignored. If a group is empty or consists only of nulls, the result is NULL.' That second sentence also matters. A group with no non-NULL values returns NULL, not 0. DISTINCT works as it did with count: 'the average is computed after duplicates have been removed.' So avg(DISTINCT col) over 1, 1, 2 is 1.5, while a plain avg over the same values would give a different answer.

    The type of the result depends on the type of the input. For DECIMAL inputs, Databricks adds four digits of precision and four of scale. If the maximum DECIMAL precision is reached, it limits the increase in scale so that no significant digits are lost. Averaging intervals returns an interval: two year-month intervals of 1 and 2 years average to 1-6, or one year and six months. Everything else becomes a DOUBLE, which is why avg over the integers 1, 2, 3 returns 2.0 and not 2.

    Result type of avg / mean for each input type
    Input typeResult type
    DECIMAL(p, s)DECIMAL(p + 4, s + 4)
    year-month intervalINTERVAL YEAR TO MONTH
    day-time intervalINTERVAL DAY TO SECOND
    Any other numeric typeDOUBLE

    A wider result type still has limits. If the average overflows its result type, avg raises ARITHMETIC_OVERFLOW. The documented example averages two DECIMAL(38, 0) values of 5e37. try_avg is the safe version: on overflow it returns NULL instead of failing the query. In Databricks Runtime with spark.sql.ansi.enabled set to false, avg itself also returns NULL on overflow. The documented example shows the error happening in ANSI mode.

    Checkpoint 1 of 5· Fill the gap

    This average of two very large DECIMAL values would overflow. Which function makes the query return NULL instead of raising ARITHMETIC_OVERFLOW?

    SELECT  ? (col) FROM VALUES (5e37::DECIMAL(38, 0)), (5e37::DECIMAL(38, 0)) AS tab(col);

    Checkpoint 2 of 5· Check yourself

    A WHERE clause leaves a region with rows whose discount column is always NULL. What does avg(discount) return for that region?

    Checkpoint 3 of 5· Exam question

    A dashboard aggregates a multi-billion-row clickstream table to show the approximate number of unique `device_id` values per day, and query latency for the dashboard tile is a higher priority than exact precision. Why would `approx_count_distinct(device_id)` be preferred over `COUNT(DISTINCT device_id)` for this tile?

    Sources12

    2.median: the middle value, which outliers don't pull

    A mean is affected by extreme values. A summary that holds up better against outliers is the median, available on Databricks SQL and Databricks Runtime 11.3 LTS and above. It is not a separate algorithm: the reference says it 'is a synonym for percentile_cont(0.5) WITHIN GROUP (ORDER BY expr).' Its NULL handling is the same as avg: NULLs are ignored, and an empty or all-NULL group returns NULL. It accepts numeric or interval input, returns an interval for interval input, and returns a DOUBLE otherwise.

    median after removing duplicates; the documented result is 2.5sql
    SELECT median(DISTINCT col) FROM VALUES (1), (2), (2), (3), (4), (NULL) AS tab(col);

    Checkpoint 4 of 5· Check yourself

    Which expression is documented as equivalent to median(price)?

    Sources3

    3.stddev: how spread out the values are

    Count, mean and median describe the size and centre of a group. Standard deviation describes how spread out it is. stddev 'returns the sample standard deviation calculated from the values in the group' and is a synonym for std. It takes numeric input, accepts DISTINCT and FILTER, can be used as a window function, and always returns a DOUBLE. For the values 1, 2, 3, 3 the documented result is 0.9574271077563381. With DISTINCT it runs on 1, 2, 3 only and returns 1.0.

    Sample standard deviation over four valuessql
    SELECT stddev(col) FROM VALUES (1), (2), (3), (3) AS tab(col);

    Because it is a sample statistic, a group with only one row has no spread to measure: 'If any group consists of only one row, the function returns NULL for that group.' In a GROUP BY report, small categories can therefore show a NULL standard deviation next to a perfectly valid count of 1 and a valid mean. That NULL is expected behaviour, not missing data. Putting count, avg, median and stddev in one SELECT gives you a summary-statistics row per group. Databricks applies the same idea in data profiling, which 'uses aggregate statistics and data distributions to track data quality over time.'

    Checkpoint 5 of 5· Check yourself

    A report groups sales by store and computes count(*), avg(amount) and stddev(amount). A new store has exactly one sale. What does its row show?

    Sources45

    Exam traps

    Each one states something that sounds right. Open it to see what is actually true.

    1. 1.avg treats NULL as zero, and returns 0 when there are no values.Why is that wrong?

      NULLs are left out of the average entirely, so the values 1, 2, NULL average to 1.5. A group that is empty or all NULL returns NULL.

      Covered in avg and mean: one function under two names

    2. 2.stddev of a single-row group is 0.Why is that wrong?

      stddev is a sample standard deviation and returns NULL for any group that has only one row.

      Covered in stddev: how spread out the values are

    Practise it for real

    Check the NULL and DISTINCT behaviour of avg, median and stddev yourself by running the documented VALUES queries in the SQL editor.

    1. 1.Run SELECT avg(col) FROM VALUES (1), (2), (NULL) AS tab(col);

      Why: Shows that NULLs are left out of the average rather than counted as zero.

      You should see: 1.5

    2. 2.Run SELECT avg(DISTINCT col) FROM VALUES (1), (1), (2) AS tab(col);

      Why: Shows that DISTINCT removes duplicates before averaging.

      You should see: 1.5

    3. 3.Run SELECT median(col) FROM VALUES (1), (2), (2), (3), (4), (NULL) AS tab(col); then repeat it with median(DISTINCT col).

      Why: Shows how removing a duplicate changes which value is in the middle.

      You should see: 2 without DISTINCT, 2.5 with DISTINCT

    4. 4.Run SELECT stddev(col) FROM VALUES (1), (2), (3), (3) AS tab(col); then repeat it with stddev(DISTINCT col).

      Why: Shows the sample standard deviation with and without duplicates.

      You should see: 0.9574271077563381, then 1.0

    Stuck? Get a nudge

    Before each run, write down your predicted result, then compare it with the output.

    Sources

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

    1. 1.
      “This function is a synonym for mean aggregate function.”
      ↩︎ avg and mean: one function under two names
      “If the result overflows the result type, Databricks raises an ARITHMETIC_OVERFLOW error. To return a NULL instead use try_avg.”
      ↩︎ avg and mean: one function under two names
      “If DISTINCT is specified the average is computed after duplicates have been removed.”
      ↩︎ avg and mean: one function under two names
      “Nulls within the group are ignored. If a group is empty or consists only of nulls, the result is NULL.”
      ↩︎ Exam trap 1
      “Nulls within the group are ignored. If a group is empty or consists only of nulls, the result is NULL.”
      ↩︎ Checkpoint
    2. 3.
      “If DISTINCT is specified, duplicates are removed and the median is computed.”
      ↩︎ median: the middle value, which outliers don't pull
      “This function is a synonym for percentile_cont(0.5) WITHIN GROUP (ORDER BY expr).”
      ↩︎ Checkpoint
    3. 4.
      “Returns the sample standard deviation calculated from the values in the group.”
      ↩︎ stddev: how spread out the values are
      “If any group consists of only one row, the function returns NULL for that group.”
      ↩︎ Exam trap 2
      “If any group consists of only one row, the function returns NULL for that group.”
      ↩︎ Checkpoint
    4. 5.
      “Data profiling uses aggregate statistics and data distributions to track data quality over time.”
      ↩︎ stddev: how spread out the values are

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