SQL SQRT函数
SQRT是SQL中的数学函数,用于返回给定数字的平方根。假设数字是25,那么此函数将返回5。
SQRT函数语法
SELECT SQRT(Number) AS Alias_Name;
在SQRT语法中,我们需要传递一个要找到平方根的数字。
在结构化查询语言中,我们也可以使用SQRT函数与表格的字段一起使用,如下所示:
SELECT SQRT(column_Name) AS Alias_Name FROM Table_Name;
在这个语法中,我们必须定义要在其上执行SQRT函数的表的名称和列。
SQRT函数的示例
示例1: 此示例返回指定数的平方根:
SELECT SQRT(100) AS Squareroot_of_100;
输出:
Squareroot_of_100
---
10
示例2: 此示例返回指定数字的平方根:
SELECT SQRT(2) AS Squareroot_of_2;
输出:
Squareroot_of_2
---
1.4142135623730
示例 3: 此示例返回8的平方根:
SELECT SQRT(8) AS Squareroot_of_8;
输出:
Squareroot_of_8
---
2.8284
示例4: 此示例返回255的平方根表示:
SELECT SQRT(255) AS Squareroot_of_255;
输出:
Squareroot_of_255
---
15.968719422671311
示例 5: 这个示例返回空值的平方根:
SELECT SQRT(NULL) AS Squareroot_of_NULL;
输出:
Squareroot_of_NULL
---
\-
示例6: 该示例返回从21到30的数字的平方根:
SELECT SQRT(21) AS 21, SQRT(22) AS 22, SQRT(23) AS 23, SQRT(24) AS 24, SQRT(25) AS 25, SQRT(26) AS 26, SQRT(27) AS 27, SQRT(28) AS 28, SQRT(29) AS 29, SQRT(30) AS 30;
输出:
21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 |
---|---|---|---|---|---|---|---|---|---|
45825 | 4.6904 | 4.7958 | 4.8989 | 5 | 5.0990 | 5.1965 | 5.2915 | 5.3851 | 5.4772 |
示例7: 该示例使用SQL表中的SQRT函数。 在此示例中,我们将通过新表执行SQRT函数来创建新表:
以下是在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 | Product_Rating |
---|---|---|---|---|---|---|
104 | P1 | 10.250 | 945 | NULL | 2022-04-30 | NULL |
202 | P4 | 15.500 | 45 | 75 | 2022-01-28 | 5 |
103 | P2 | 18.250 | 25 | NULL | 2022-02-18 | 4 |
111 | P7 | 25.250 | 5 | 15 | 2021-12-25 | 9 |
210 | P6 | 15.500 | 50 | 70 | 2021-10-15 | NULL |
212 | P8 | 19.750 | 110 | 250 | 2022-01-28 | 4 |
112 | P10 | 10.250 | 550 | 835 | 2022-04-11 | NULL |
查询1: 下面的SELECT查询使用了上述Product_Details表的Product_Quantity列的SQRT函数:
SELECT Product_ID, SQRT(Product_ID) AS Squareroot_of_Product_ID FROM Product_Details;
这个查询显示每个产品的产品ID的平方根。
输出结果:
Product_ID | Squareroot_of_Product_ID |
---|---|
104 | 10.1980 |
202 | 14.2126 |
103 | 10.1488 |
111 | 10.53565 |
210 | 14.4913 |
212 | 14.56021 |
112 | 10.58300 |
查询2: 以下SELECT查询使用上述Product_Details表中的Purchasing_Price和Selling_Price列的SQRT函数:
SELECT Purchasing_Price, SQRT(Purchasing_Price) AS SQRTary_of_PurchasingPrice, Selling_Price, SQRT(Selling_Price) AS Squareroot_of_SellingPrice FROM Product_Details;
这个查询显示每个产品的购买和销售价格的平方根。
输出:
Purchasing_Price | Squareroot_of_PurchasingPrice | Selling_Price | Squareroot_of_SellingPrice |
---|---|---|---|
945 | 30.7048 | NULL | - |
45 | 6.70820 | 75 | 8.66025 |
25 | 5 | NULL | - |
5 | 2.23606 | 15 | 3.872983 |
50 | 7.07106 | 70 | 8.36660026 |
110 | 10.48808 | 250 | 15.81138 |
550 | 23.4520 | 835 | 28.8963 |
查询3: 以下SELECT查询使用了上面Product_Details表的Product_Rating列的SQRT函数:
SELECT SQRT(Product_Rating) AS Squareroot_of_productrating FROM Product_Details;
这个查询显示了上表中每个产品评分的平方根。
输出:
Product_Rating | Squareroot_of_productrating |
---|---|
NULL | - |
5 | 2.23606 |
4 | 2 |
9 | 3 |
NULL | - |
4 | 2 |
NULL | - |