Select Avg in SQL
? SELECT AVG() in SQL
The AVG() function calculates the average (mean) value of a numeric column in a result set.
? Syntax:
SELECT AVG(column_name) FROM table_name WHERE condition;column_name: The numeric column for which you want to find the average.WHERE: Optional condition to filter rows.
? Examples:
Average salary from employees table:
SELECT AVG(salary) AS average_salary FROM employees;Average salary for employees in 'Sales' department:
SELECT AVG(salary) AS avg_sales_salaryFROM employeesWHERE department = 'Sales';Average price of products with price > 100:
SELECT AVG(price) AS avg_priceFROM productsWHERE price > 100;? Notes:
AVG()ignoresNULLvalues.Often used with
GROUP BYto get averages per group:
SELECT department, AVG(salary) AS avg_salaryFROM employeesGROUP BY department;Want examples using AVG() with grouping or joins?