search
HomeDatabaseSQLUsage of avg function in sql

The AVG function in SQL is used to calculate the average of numeric values. Its syntax is AVG(column_name). AVG functions ignore NULL values. You can use the AS keyword to specify aliases for results. It only works for numeric type data, returning NULL if the target column has no value or contains only NULL values, and throwing an error if it contains non-numeric values.

Usage of avg function in sql

Usage of AVG function in SQL

The AVG function in SQL is used to calculate the value of a set of numeric values average value.

Syntax:

<code>AVG(column_name)</code>

Where:

  • column_name is the target column to calculate the average.

Usage:

<code>-- 示例表包含 'sales' 列,存储销售数据
CREATE TABLE sales (
    product_id INT,
    sales INT
);

-- 计算 'sales' 列的平均值
SELECT AVG(sales) FROM sales;</code>

Return result:

<code>NULL</code>

Because there is no data in the table yet, the result is NULL.

Handling NULL values:

AVG functions ignore NULL values. For example:

<code>-- 在 'sales' 列中插入一些数据,包括 NULL 值
INSERT INTO sales (product_id, sales) VALUES
(1, 10),
(2, 20),
(3, NULL);

-- 计算 'sales' 列的平均值
SELECT AVG(sales) FROM sales;</code>

Returns:

<code>15.0</code>

The AVG function ignores NULL values, so the average is (10 20) / 2 = 15.

Using aliases:

You can use the AS keyword to specify an alias for the result of an AVG function. For example:

<code>-- 计算 'sales' 列的平均值并将其别名为 'average_sales'
SELECT AVG(sales) AS average_sales FROM sales;</code>

Return result:

<code>| average_sales |
|---|---|
| 15.0 |</code>

Note:

  • AVG function only applies to numeric types The data.
  • If the target column has no values ​​or contains only NULL values, the AVG function returns NULL.
  • The AVG function throws an error if the target column contains non-numeric values.

The above is the detailed content of Usage of avg function in sql. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What are the different types of data partitioning in SQL (horizontal, vertical)?What are the different types of data partitioning in SQL (horizontal, vertical)?Mar 13, 2025 pm 02:01 PM

The article discusses horizontal and vertical data partitioning in SQL, focusing on their impact on performance and scalability. It compares benefits and considerations for choosing between them.

How to handle foreign key constraints in SQL delete rowsHow to handle foreign key constraints in SQL delete rowsMar 04, 2025 pm 05:52 PM

This article addresses deleting rows with foreign key constraints in relational databases. It details methods for handling constraint violations, including cascading deletes, restricting deletes, and setting nulls. The article emphasizes best pract

How do I use aggregate functions in SQL to summarize data (SUM, AVG, COUNT, MIN, MAX)?How do I use aggregate functions in SQL to summarize data (SUM, AVG, COUNT, MIN, MAX)?Mar 13, 2025 pm 01:50 PM

The article explains how to use SQL aggregate functions (SUM, AVG, COUNT, MIN, MAX) to summarize data, detailing their uses and differences, and how to combine them in queries.Character count: 159

What are the security risks of using dynamic SQL and how can I mitigate them?What are the security risks of using dynamic SQL and how can I mitigate them?Mar 13, 2025 pm 01:59 PM

The article discusses security risks of dynamic SQL, focusing on SQL injection, and provides mitigation strategies like using parameterized queries and input validation.

What are the ACID properties of transactions in SQL?What are the ACID properties of transactions in SQL?Mar 13, 2025 pm 01:54 PM

The article discusses the ACID properties (Atomicity, Consistency, Isolation, Durability) in SQL transactions, crucial for maintaining data integrity and reliability.

What are the different transaction isolation levels in SQL (READ UNCOMMITTED, READ COMMITTED, REPEATABLE READ, SERIALIZABLE)?What are the different transaction isolation levels in SQL (READ UNCOMMITTED, READ COMMITTED, REPEATABLE READ, SERIALIZABLE)?Mar 13, 2025 pm 01:56 PM

The article discusses SQL transaction isolation levels: READ UNCOMMITTED, READ COMMITTED, REPEATABLE READ, and SERIALIZABLE. It examines their impact on data consistency and performance, noting that higher isolation ensures greater consistency but ma

What is the difference between SQL delete rows and truncateWhat is the difference between SQL delete rows and truncateMar 04, 2025 pm 05:49 PM

This article compares SQL's DELETE and TRUNCATE commands. DELETE removes rows individually, allowing conditional removal and transaction rollback. TRUNCATE is faster, removing all rows at once, but lacks rollback capability. Performance and data re

How to test SQL delete rowsHow to test SQL delete rowsMar 04, 2025 pm 05:53 PM

This article details effective testing strategies for SQL DELETE operations. It emphasizes verifying correct row deletion via pre- and post-deletion data comparisons, row counts, and negative testing. Best practices, including backups, transaction

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!