SQL AVG函数

SQL AVG函数

AVG是SQL中的一个聚合函数,它返回表的整数列的平均值。

AVG函数的语法

在结构化查询语言中,我们使用AVG函数与表的列一起使用,如下所示:

SELECT AVG(column_Name) AS Alias_Name FROM Table_Name;

在这个语法中,我们必须定义我们想要执行AVG函数的表的名称和列。

AVG函数的例子

在这里,我们将通过新建表来执行AVG函数,该表包含表的列:

以下显示了在SQL中创建新表的语法:

CREATE TABLE Name_of_New_Table
(
First_Column_of_table Data Type (character_size of First Column),  
Second_Column_of_table Data Type (character_size of the Second column ),  
Third_Column_of_table Data Type (character_size of the Third column),  
.......,  
Last_Column_of_table Data Type (character_size of the Last column)
);  

以下CREATE语句用于创建 Product_Details 表,用于存储产品的价格和数量:

CREATE TABLE Product_Details
(
Product_ID INT NOT NULL,
Product_Name Varchar(50),
Product_Quantity INT,
Purchasing_Price INT,
Selling_Price INT,
Release_Date Date, 
Product_Rating INT
);

以下多个INSERT查询将产品记录以及它们的销售价格和购买价格插入Product_Details表中:

INSERT INTO Product_Details (Product_ID, Product_Name, Product_ Quantity Purchasing_Price, Selling_Price, Release_Date, Product_Rating) VALUES (104, P1, 10.250, 945, NULL, 2022-04-30, NULL);

INSERT INTO Product_Details (Product_ID, Product_Name, Product_ Quantity Purchasing_Price, Selling_Price, Release_Date, Product_Rating) VALUES (202, P4, 15.500, 45, 75, 2022-01-28, 5);

INSERT INTO Product_Details (Product_ID, Product_Name, Product_ Quantity Purchasing_Price, Selling_Price, Release_Date, Product_Rating) VALUES (103, P2, 18.250, 25, NULL, 2022-02-18, 4);

INSERT INTO Product_Details (Product_ID, Product_Name, Product_ Quantity Purchasing_Price, Selling_Price, Release_Date, Product_Rating) VALUES (111, P7, 25.250, 5, 15, 2021-12-25, 9);

INSERT INTO Product_Details (Product_ID, Product_Name, Product_ Quantity Purchasing_Price, Selling_Price, Release_Date, Product_Rating) VALUES (210, P6, 15.500, 50, 70, 2021-10-15, NULL);


INSERT INTO Product_Details (Product_ID, Product_Name, Product_ Quantity Purchasing_Price, Selling_Price, Release_Date, Product_Rating) VALUES (212, P8, 19.750, 110, 250, 2022-01-28, 4);

INSERT INTO Product_Details (Product_ID, Product_Name, Product_ Quantity Purchasing_Price, Selling_Price, Release_Date, Product_Rating) VALUES (112, P10, 10.250, 550, 835, 2022-04-11, NULL);

下列SELECT语句显示了上述 Product_Details 表中插入的记录:

SELECT * FROM Product_Details;
Product_ID Product_Name Product_Quantity Purchasing_Price Selling_Price Release_Date Category Product_Rating
104 P1 10.250 945 NULL 2022-04-30 Cloths NULL
202 P4 15.500 45 75 2022-01-28 Electrical 5
103 P2 18.250 25 NULL 2022-02-18 Toys 4
111 P7 25.250 5 15 2021-12-25 Cloths 9
210 P6 15.500 50 70 2021-10-15 Electrical NULL
212 P8 19.750 110 250 2022-01-28 Cloths 4
112 P10 10.250 550 835 2022-04-11 Toys NULL

查询1: 以下SELECT查询使用了以上Product_Details表中的Product_Quantity列的AVG函数:

SELECT AVG(Product_Quantity) AS Average_ofproductquantity FROM Product_Details;

输出:

Average_ofproductquantity   
---  
16.39

查询 2: 以下SELECT查询使用了上述Product_Details表的Selling_Price列的AVG函数:

SELECT AVG(Selling_Price) AS Average_ofsellingpriceproducts FROM Product_Details;

这个查询返回销售价格产品的平均值。

输出:

Average_ofsellingpriceproducts   
---  
249 

查询3: 以下SELECT查询使用上述Product_Details表中的Product_Rating列进行AVG函数的计算:

SELECT AVG(Product_Rating) AS Average_of_productrating FROM Product_Details;

这个查询返回Product_rating列数值的平均值。

输出:

Average_ofofproductrating   
---  
5.5  

AVG函数与WHERE子句

我们还可以使用WHERE子句与AVG函数一起使用,它会对过滤后的行的值进行求和。

使用带有WHERE子句的AVG函数的语法如下所示:

SELECT AVG(column_Name) AS Alias_Name FROM Table_Name WHERE Condition;

查询 1: 以下 SELECT 查询在上述 Product_Details 表中使用了带有 WHERE 子句的 AVG 函数:

SELECT AVG(Product_Quantity) AS Average_ofproduct>200 FROM Product_Details WHERE Product_ID > 200;

这个查询找到了上述表中产品id大于200的那些产品的数量的平均值。

输出:

Average_ofproduct>200   
---  
16.91  

查询2: 下面的SELECT查询在上述Product_Details表中使用了AVG函数和WHERE子句:

SELECT AVG(Purchasing_Price) AS Average_of_purchasingprice FROM Product_Details WHERE Release_Date = 2022-01-28;

这个查询找到了2022-01-28发布的产品的平均购买价格。

输出:

Average_of_purchasingprice   
---  
162.5 

带有DISTINCT子句的AVG函数

我们还可以在AVG函数中使用DISTINCT子句,该子句会添加表中列的不同值。

使用AVG函数和DISTINCT子句的语法如下所示:

SELECT AVG(DISTINCT (column_Name)) AS Alias_Name FROM Table_Name WHERE Condition;

查询1: 下面的SELECT查询在上面的Product_Details表中使用了AVG函数和DISTINCT子句:

SELECT AVG(DISTINCT(Product_Quantity)) AS Average_ofdistinctproduct FROM Product_Details;

这个查询添加了Product_quantity列的不同值的数量,然后找出了平均值。

输出:

Average_ofdistinctproduct   
---  
17.8   

查询2: 以下SELECT查询使用了上述Product_Details表中的AVG函数和DISTINCT子句:

SELECT AVG(DISTINCT(Product_Rating)) AS Average_ofdistinctproductrating FROM Product_Details;

输出:

Average_ofdistinctproductrating   
---  
6 

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程