SQL GREATEST函数
GREATEST是一种SQL数值函数,它在结构化查询语言中显示指定输入的最大值。
GREATEST函数的语法
SELECT GREATEST(Number1, Number2, Number3, Number4, ......, NumberN) AS Alias_Name;
在最大语法中,我们必须传递那些我们想要找到最大值的数字。
在结构化查询语言中,我们也可以使用GREATEST函数来操作表格的列,示例如下所示:
SELECT GREATEST(Integer_column_Name1, Integer_column_Name1, Integer_column_Name1, Integer_column_Name1, ......., Integer_column_NameN,) AS Alias_Name FROM Table_Name;
在这个语法中,我们需要定义我们想要执行GREATEST函数的表的名称和列。
GREATEST函数的例子
例子1: 这个例子返回指定数字中最大的那个:
SELECT GREATEST(1, 2, 5, 8) AS greatest_of_numbers;
输出:
greatest_of_numbers
---
8
示例 2: 这个示例返回指定数字中的最大值:
SELECT GREATEST(22, 88, 98, 108, 55, 22, 45, 22) AS greatest_of_numbers;
输出:
greatest_of_numbers
---
108
示例3: 该示例从给定的输入中返回最大值:
SELECT GREATEST(3, 12, 34, 8, 25) AS greatest_of_numbers;
输出:
greatest_of_numbers
---
34
示例 4: 返回255的最大表示的示例:
SELECT GREATEST(3.8, 3.45, 3.52, 3.82, 3.08, 3.62) AS greatest_of_numbers;
输出:
greatest_of_numbers
---
3.82
示例5: 这个示例返回最大值:
SELECT GREATEST(NULL) AS greatest_of_NULL;
输出:
greatest_of_numbers
---
Shows Error
示例6: 这个示例使用了SQL表的GREATEST函数。在这个示例中,我们将通过这个新的表来对表的列执行GREATEST函数:
下面是在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和Product_ID列的GREATEST函数:
SELECT Product_ID, Product_Quantity, GREATEST(Product_ID, Product_Quantity) AS greatest_of_Product_ID_Quantity FROM Product_Details;
该查询显示了每个产品的产品编号和数量的最大值。
输出:
Product_ID | Product_Quantity | greatest_of_Product_ID_quantity |
---|---|---|
104 | 10.250 | 150 |
202 | 15.500 | 312 |
103 | 18.250 | 147 |
111 | 25.250 | 157 |
210 | 15.500 | 322 |
212 | 19.750 | 324 |
112 | 10.250 | 160 |
查询2: 以下SELECT查询使用了GREATEST函数,使用了上面Product_Details表中的Selling_Price和Purchasing_Price列:
SELECT Selling_Price, Purchasing_Price GREATEST(Selling_Price, Purchasing_Price) AS greatest_of_Selling_Purchasing_Price FROM Product_Details;
这个查询显示每个产品的购买价和销售价中的最大值。
输出:
Selling_Price | Purchasing_Price | greatest_of_Selling_PurchasingPrice |
---|---|---|
NULL | 945 | - |
75 | 45 | 75 |
NULL | 25 | - |
15 | 5 | 15 |
70 | 50 | 70 |
250 | 110 | 250 |
835 | 550 | 835 |
查询3: 以下SELECT查询使用了上述Product_Details表中的Purchasing_Price和Product_ID列的GREATEST函数:
SELECT Product_ID, Purchasing_Price, GREATEST(Product_ID, Purchasing_Price) AS greatest_of_Product_ID_and_Purcasing FROM Product_Details;
这个查询显示了每个产品的产品ID和购买价格的最大值。
输出:
Product_ID | Purchasing_Price | greatest_of_Product_ID_and_Purchasing |
---|---|---|
104 | 945 | 945 |
202 | 45 | 202 |
103 | 25 | 103 |
111 | 5 | 111 |
210 | 50 | 210 |
212 | 110 | 212 |
112 | 550 | 550 |