Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Avg Function In Sql in SQL

Avg Function In Sql in SQL

? AVG() Function in SQL

The AVG() function calculates the average (arithmetic mean) of the values in a numeric column.


? Syntax

SELECT AVG(column_name)FROM table_nameWHERE condition;
  • Works on numeric columns (INT, FLOAT, DECIMAL, etc.).

  • Ignores NULL values automatically.

  • Returns the average as a float or appropriate numeric type.


? Example Table: sales

idproductamount
1Laptop1000.00
2Keyboard150.00
3Mouse50.00
4Laptop1200.00
5MouseNULL

? Example Queries

1. Average of entire column

SELECT AVG(amount) AS avg_amountFROM sales;

Result: (1000 + 150 + 50 + 1200) / 4 = 600 (ignores NULL)


2. Average by product

SELECT product, AVG(amount) AS avg_amountFROM salesGROUP BY product;
productavg_amount
Laptop1100.00
Keyboard150.00
Mouse50.00

3. Average with condition

SELECT AVG(amount) AS avg_amount_highFROM salesWHERE amount > 100;

Calculates average for amounts greater than 100.


?? Notes

  • AVG() ignores NULL values.

  • Use with GROUP BY to get averages per group.

  • Can be combined with other aggregate functions like SUM(), COUNT(), etc.


Let me know if you want examples combining AVG() with HAVING or window functions!

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql