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 in PostgreSql

Avg in PostgreSql

AVG() in PostgreSQL

The AVG() function in PostgreSQL calculates the average (mean) value of a numeric column.

Syntax

SELECT AVG(column_name) FROM table_name WHERE condition;


1. Calculating the Average of a Column

SELECT AVG(salary) FROM employees;

✅ Returns the average salary of all employees.


2. Using AVG() with a WHERE Condition

SELECT AVG(salary) FROM employees WHERE department = 'IT';

✅ Returns the average salary of employees in the IT department.


3. Using AVG() with GROUP BY

SELECT department, AVG(salary) FROM employees GROUP BY department;

✅ Returns the average salary for each department.


4. Using ROUND() to Format the Result

By default, AVG() returns a decimal value. You can round it to a specific number of decimal places:

SELECT ROUND(AVG(salary), 2) FROM employees;

✅ Returns the average salary rounded to 2 decimal places.


5. Handling NULL Values in AVG()

  • AVG() ignores NULL values in calculations.
  • If all values are NULL, it returns NULL.

Example:

SELECT AVG(bonus) FROM employees;

✅ If some employees don’t have a bonus (NULL), they are not included in the calculation.

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