SQL AVG Function - Complete Beginner Tutorial
Quick Answer
The SQL AVG function calculates the average value of a numeric column in a database table. It is commonly used in SELECT statements to find the mean of values, ignoring NULLs by default.
Learning Objectives
- Understand the purpose and usage of the SQL AVG function.
- Write SQL queries using AVG to calculate average values.
- Learn how AVG handles NULL values and grouping.
Introduction
The AVG function in SQL is an aggregate function used to calculate the average value of a numeric column.
It is essential for summarizing data and gaining insights such as average sales, scores, or measurements.
An average is a single value that represents a set of values.
What is the SQL AVG Function?
AVG stands for average and is used to compute the mean value of a numeric column in a table.
It is an aggregate function, meaning it operates on multiple rows and returns a single value.
- Syntax: SELECT AVG(column_name) FROM table_name;
- Only numeric columns can be used with AVG.
- NULL values are ignored in the calculation.
Using AVG with GROUP BY
AVG is often combined with the GROUP BY clause to calculate averages for each group of rows.
This is useful for finding averages per category, department, or any grouping.
- Syntax: SELECT group_column, AVG(numeric_column) FROM table_name GROUP BY group_column;
- Each group returns its own average value.
Handling NULL Values in AVG
AVG automatically ignores NULL values in the column when calculating the average.
This ensures that missing or undefined values do not affect the result.
- If all values are NULL, AVG returns NULL.
- You can use COALESCE to replace NULLs if needed before averaging.
Examples of Using AVG
Let's look at practical examples to understand how AVG works.
Basic AVG Example
Calculate the average salary from an employees table.
AVG with GROUP BY Example
Calculate the average salary per department.
Practical Example
This query calculates the average salary from the employees table.
This query calculates the average salary for each department.
Examples
SELECT AVG(salary) AS average_salary FROM employees;This query calculates the average salary from the employees table.
SELECT department, AVG(salary) AS average_salary FROM employees GROUP BY department;This query calculates the average salary for each department.
Best Practices
- Always use meaningful aliases for AVG results for clarity.
- Use GROUP BY with AVG to get averages per category or group.
- Be aware that AVG ignores NULLs, so check your data for missing values.
Common Mistakes
- Trying to use AVG on non-numeric columns.
- Forgetting to use GROUP BY when averaging per group.
- Assuming AVG includes NULL values in the calculation.
Hands-on Exercise
Calculate Average Age
Write a query to find the average age of users in a users table.
Expected output: A single value representing the average age.
Hint: Use the AVG function on the age column.
Average Sales per Region
Write a query to calculate the average sales amount per region from a sales table.
Expected output: A list of regions with their average sales.
Hint: Use GROUP BY region with AVG on the sales_amount column.
Interview Questions
What does the SQL AVG function do?
InterviewThe AVG function calculates the average value of a numeric column, ignoring NULL values.
How does AVG handle NULL values?
InterviewAVG ignores NULL values and calculates the average only on non-NULL values.
Can you use AVG with GROUP BY? Explain.
InterviewYes, AVG can be used with GROUP BY to calculate averages for each group of rows.
MCQ Quiz
1. What is the best first step when learning AVG Function?
A. Understand the purpose and basic idea
B. Skip directly to advanced implementation
C. Ignore examples and practice
D. Memorize terms without context
Correct answer: A
Starting with the purpose and basic idea makes later examples and practice easier to understand.
2. Which activity helps reinforce AVG Function?
A. Reading once without practice
B. Building or writing a small practical example
C. Avoiding review questions
D. Skipping the summary
Correct answer: B
A small practical example helps connect the topic to real usage.
3. Which statement is most accurate about this topic?
A. The SQL AVG function calculates the average value of a numeric column in a database table.
B. AVG Function never needs examples
C. AVG Function is unrelated to practical work
D. AVG Function should be learned without checking results
Correct answer: A
The correct option is based on the available topic explanation.
Key Takeaways
- AVG calculates the mean of numeric values in a column.
- NULL values are ignored in AVG calculations.
- AVG can be combined with GROUP BY to find averages per group.
- The SQL AVG function calculates the average value of a numeric column in a database table.
- It is commonly used in SELECT statements to find the mean of values, ignoring NULLs by default.
Summary
The SQL AVG function is a powerful tool to calculate average values in your data.
It ignores NULLs and works well with GROUP BY to provide grouped averages.
Understanding AVG helps you summarize and analyze numeric data effectively.
Frequently Asked Questions
Can AVG be used on columns with NULL values?
Yes, AVG ignores NULL values and calculates the average based on non-NULL values.
What data types can AVG be used with?
AVG works with numeric data types such as INT, FLOAT, DECIMAL, etc.
How do I calculate average per category in SQL?
Use AVG combined with GROUP BY on the category column to calculate averages per category.





