PostgreSQL AVG

The AVG() function returns the average of numeric values in a column, commonly used to analyze data such as average salaries, prices, or ratings.

Key Topics

1. Basic Syntax

Compute the average for a column:

SELECT AVG(salary) AS average_salary
FROM employees;

2. Using GROUP BY

Calculate averages per group:

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

This shows the average salary in each department.

Best Practices

  • Ensure the column is an appropriate numeric type.
  • Combine AVG() with GROUP BY for category-based averages.

Key Takeaways

  • AVG() gives the mean of numeric values.
  • Often used for analytics and performance metrics within tables.