Applies to: ✅ Data 360 SQL ✅ Tableau Hyper API
Computes the average (arithmetic mean) of all input values.
Syntax
avg(<expression>)
Arguments
Required
<expression>: An expression of any numerical type.
Returns
numericwith a scale of 6 for integer-type arguments and numeric arguments with a scale less than 6.double precisionfor floating-point arguments.- Otherwise, returns the same as argument the type.
Returns NULL if no rows are selected.
Considerations
- NULL values are ignored in the calculation.
- Returns NULL when applied to an empty set.
- Use
COALESCEto substitute a default value for NULL results.
Examples
Calculate Average
Get the average price of products.
SELECT avg(price) AS average_price
FROM products;
Average by Group
Calculate the average salary by department.
SELECT department, avg(salary) AS avg_salary
FROM employees
GROUP BY department;
Handle Empty Results
Use COALESCE to handle NULL results.
SELECT coalesce(avg(quantity), 0) AS avg_quantity
FROM orders
WHERE order_date > '2024-01-01';