Avg SQL

Galaxy Glossary

How do you calculate the average of a column in a SQL table?

The AVG() function in SQL calculates the average value of a numeric column in a table. It's a crucial aggregate function for summarizing data.
Sign up for the latest in SQL knowledge from the Galaxy Team!
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Description

The AVG() function is a powerful tool in SQL for summarizing data. It allows you to quickly determine the average value of a numeric column within a table or a subset of rows. This is essential for understanding central tendencies in your data. For instance, in a sales database, you might want to find the average sales amount per month or the average customer order value. The AVG() function simplifies this process, providing a concise summary statistic. It's important to note that AVG() ignores NULL values in the specified column. If all values in the column are NULL, the result will be NULL. This behavior is crucial to understand when dealing with potentially incomplete datasets. Finally, AVG() is often used in conjunction with other aggregate functions like COUNT() or GROUP BY to provide a more comprehensive analysis of your data.

Why Avg SQL is important

The AVG() function is crucial for data analysis and reporting. It helps quickly understand central tendencies, allowing for better decision-making based on data insights. It's a fundamental building block for more complex queries and reports.

Example Usage


-- Sample table: Sales
CREATE TABLE Sales (
    OrderID INT PRIMARY KEY,
    OrderDate DATE,
    TotalAmount DECIMAL(10, 2)
);

-- Insert some sample data
INSERT INTO Sales (OrderID, OrderDate, TotalAmount)
VALUES
(1, '2023-10-26', 150.50),
(2, '2023-10-27', 200.00),
(3, '2023-10-27', 100.00),
(4, '2023-10-28', 125.75),
(5, '2023-10-28', NULL);

-- Calculate the average total amount
SELECT AVG(TotalAmount) AS AverageTotalAmount
FROM Sales;

Common Mistakes

Want to learn about other SQL terms?