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
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!

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.

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

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

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

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

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

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

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


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

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
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version
Recommended: Win version, supports code prompts!
