SQL SELECT MAX
MAX是结构化查询语言中的聚合函数,它从表中的多个列值中返回最大值。
SQL Max()函数的语法
SELECT MAX(Column_Name) FROM Table_Name WHERE [Condition];
我们还可以使用WHERE条件从特定记录中选择最大值。
SQL MAX函数的两个示例
在本文中,您将学习如何通过以下两个示例在SQL中使用MAX函数。
示例1:此示例描述了如何在SQL中使用MAX函数。
让我们创建一个简单的表,在该表上运行MAX函数。
以下CREATE TABLE语句创建具有五个字段的Cars_Details表:
CREATE TABLE Cars_Details
(
Car_Number INT PRIMARY KEY,
Car_Model INT,
Car_Name VARCHAR (50),
Car_Price INT NOT NULL,
Car_AmountINT NOT NULL
) ;
以下INSERT查询将汽车的记录插入到 Cars_Details 表中:
INSERT INTO Cars_Details (Car_Number, Car_Model, Car_Name, Car_Amount, Car_Price)
VALUES (2578, 2018, Creta, 3, 1500000),
(9258, 2019, Audi, 2, 3000000),
(8233, 2018, Venue, 6, 900000),
(8990, 2018, Nexon, 7, 700000),
(9578, 2020, Mercedes, 6, 8000000),
(1258, 2021, Thar, 2, 1500000),
(2564, 2019, Jaguar, 4, 6000000),
(2155, 2020, Scorpio, 8, 1800000);
以下的SELECT查询显示了 Cars_Details 表的数据:
SELECT * FROM Cars_Details;
Car_Number | Car_Name | Car_Amount | Car_Price |
---|---|---|---|
2578 | Creta | 3 | 900000 |
9258 | Audi | 2 | 1100000 |
8233 | Venue | 6 | 900000 |
8990 | Nexon | 7 | 700000 |
9578 | Mercedes | 6 | 8000000 |
1258 | Thar | 2 | 1500000 |
2564 | Jaguar | 4 | 6000000 |
2155 | Scorpio | 8 | 1800000 |
以下查询显示了上述Car_Details表中最大的Car_Number:
SELECT MAX(Car_Number) As "Largest Car Number "FROM Cars_Details;
输出:
示例2:本示例描述如何在SELECT语句中使用WHERE子句与MAX函数:
下面的查询使用CREATE TABLE语句创建了 College_Student_Details 表:
CREATE TABLE College_Student_Details
(
Student_ID INT NOT NULL,
Student_Name varchar(100),
Student_Course varchar(50),
Student_Age INT,
Student_Marks INT
);
下面的SQL查询使用INSERT INTO语句将学生的记录插入到上述表中:
INSERT INTO College_Student_Details VALUES (101, Anuj, B.tech, 20, 88);
INSERT INTO College_Student_Details VALUES (102, Raman, MCA, 24, 98);
INSERT INTO College_Student_Details VALUES (104, Shyam, BBA, 19, 92);
INSERT INTO College_Student_Details VALUES (107, Vikash, B.tech, 20, 78);
INSERT INTO College_Student_Details VALUES (111, Monu, MBA, 21, 65);
INSERT INTO College_Student_Details VALUES (114, Jones, B.tech, 18, 93);
INSERT INTO College_Student_Details VALUES (121, Parul, BCA, 20, 97);
INSERT INTO College_Student_Details VALUES (123, Divya, B.tech, 21, 89);
INSERT INTO College_Student_Details VALUES (128, Hemant, MBA, 23, 90);
INSERT INTO College_Student_Details VALUES (130, Nidhi, BBA, 20, 88);
INSERT INTO College_Student_Details VALUES (132, Priya, MBA, 22, 99);
INSERT INTO College_Student_Details VALUES (138, Mohit, MCA, 21, 92);
让我们使用以下SELECT语句查看上表的记录:
SELECT * FROM College_Student_Details;
Student_ID | Student_Name | Student_Course | Student_Age | Student_Marks |
---|---|---|---|---|
101 | Anuj | B.tech | 20 | 88 |
102 | Raman | MCA | 24 | 98 |
104 | Shyam | BBA | 19 | 92 |
107 | Vikash | B.tech | 20 | 78 |
111 | Monu | MBA | 21 | 65 |
114 | Jones | B.tech | 18 | 93 |
121 | Parul | BCA | 20 | 97 |
123 | Divya | B.tech | 21 | 89 |
128 | Hemant | MBA | 23 | 90 |
130 | Nidhi | BBA | 20 | 88 |
132 | Priya | MBA | 22 | 99 |
138 | Mohit | MCA | 21 | 92 |
以下MAX查询显示了上述Student_Details表中80以下的最高分数:
SELECT MAX(Student_Marks) As "Highest Marks under 80" FROM College_Student_Details WHERE Student_Marks < 80;
输出:
MAX函数与SQL的GROUP BY子句
使用GROUP BY子句的MAX函数显示表中每个组的最高值。
使用MAX函数与GROUP BY子句的语法如下所示:
SELECT Column_Name_1, MAX(Column_Name) FROM Table_Name GROUP BY Column_Name_1;
使用GROUP BY子句的MAX函数示例
让我们以上面的College_Student_Details表为例来理解使用GROUP BY子句的MAX函数的概念。
以下查询显示了上述College_Student_Details表中每门课程中学生的最高分数:
SELECT Student_Course, MAX (Student_Marks) FROM College_Student_Details GROUP BY Student_Course;
输出:
Student_Course | AVG (Student_Marks) |
---|---|
B.tech | 93 |
MCA | 98 |
BBA | 92 |
MBA | 99 |
BCA | 97 |