AVG (Lakehouse v1)
Aggregate function.
The AVG() function returns the average value of an expression.
Note: NULL values are not counted.
Analyze Syntax
Section titled “Analyze Syntax”func.avg(<column>)Analyze Examples
Section titled “Analyze Examples”func.avg(table.price).alias('avg_price')
| avg_price || --------- || 20.4 |SQL Syntax
Section titled “SQL Syntax”AVG(<expr>)Arguments
Section titled “Arguments”| Arguments | Description |
|---|---|
<expr> | Any numerical expression |
Return Type
Section titled “Return Type”double
SQL Examples
Section titled “SQL Examples”Creating a Table and Inserting Sample Data
Let’s create a table named “sales” and insert some sample data:
CREATE TABLE sales ( id INTEGER, product VARCHAR(50), price FLOAT);
INSERT INTO sales (id, product, price)VALUES (1, 'Product A', 10.5), (2, 'Product B', 20.75), (3, 'Product C', 30.0), (4, 'Product D', 15.25), (5, 'Product E', 25.5);Query: Using AVG() Function
Now, let’s use the AVG() function to find the average price of all products in the “sales” table:
SELECT AVG(price) AS avg_priceFROM sales;The result should look like this:
| avg_price || --------- || 20.4 |