SQL SELECT MIN
MIN是SQL中的一个聚合函数,它从表中某一列的多个值中返回最小值。
SQL Min()函数的语法
SELECT MIN(Column_Name) FROM Table_Name WHERE [Condition];
在这个语法中,我们还可以在使用WHERE子句时使用MIN函数来选择过滤记录中的最小值。
两个SQL Min函数的示例
示例1:此示例描述了如何在SQL中使用MIN函数。
让我们创建一个简单的表,然后在该表上运行MIN函数。
以下CREATE TABLE语句创建了一个名为Bikes_Details的表,包含五个字段:
CREATE TABLE Bikes_Details
(
Bike_Number INT PRIMARY KEY,
Bike_Model INT,
Bike_Name VARCHAR (50),
Number_of_Bikes INT NOT NULL
Bike_Price INT NOT NULL
) ;
以下INSERT查询将汽车记录插入Bikes_Details表中:
INSERT INTO Bikes_Details (Bike_Number, Bike_Model, Bike_Name, Number_of_Bikes, Bike_Price)
VALUES (2578, 2018, KTM, 3, 1500000),
(9258, 2019, Apache, 2, 3000000),
(8233, 2018, Pulsar, 6, 900000),
(8990, 2018, RX100, 7, 700000),
(9578, 2020, Splender+, 6, 8000000),
(1258, 2021, Bullet, 2, 1500000),
(2564, 2019, Passion, 4, 6000000),
(2155, 2020, Livo, 8, 1800000);
下面的SELECT查询显示了 Bikes_Details 表的数据:
SELECT * FROM Bikes_Details;
Bike_Number | Bike_Model | Bike_Name | Number_of_Bikes | Bike_Price |
---|---|---|---|---|
2578 | 2018 | KTM | 3 | 900000 |
9258 | 2019 | Apache | 2 | 1100000 |
8233 | 2018 | Pulsar | 6 | 900000 |
8990 | 2018 | RX100 | 7 | 700000 |
9578 | 2020 | Splender+ | 6 | 8000000 |
1258 | 2021 | Buller | 2 | 1500000 |
2564 | 2019 | Passion | 4 | 6000000 |
2155 | 2020 | Livo | 8 | 1800000 |
以下查询显示了上述Bikes_Details表中Bike_Number列的最小值:
SELECT MIN(Bike_Number) As "Smallest Bike Number " FROM Bikes_Details;
输出:
示例2:这个例子向你展示如何在WHERE子句中使用MIN函数。
以下查询使用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 |
以下MIN查询显示了College_Student_Details表中Student_Marks列中大于70的最低分数:
SELECT MIN(Student_Marks) As "Lowest Marks Above 70" FROM College_Student_Details WHERE Student_Marks > 70;
输出:
MIN 函数与 SQL GROUP BY 子句
使用 GROUP BY 子句的 MIN 函数显示表中每个组中的最小值。
使用 MIN 函数和 GROUP BY 子句的语法如下所示:
SELECT Column_Name_1, MIN(Column_Name) FROM Table_Name GROUP BY Column_Name_1;
MIN函数与GROUP BY子句的示例
让我们使用上述College_Student_Details表来理解MIN函数与GROUP BY子句的概念。
以下查询显示了上述College_Student_Details表中每门课程中学生的最低分数:
SELECT Student_Course, MIN (Student_Marks) FROM College_Student_Details GROUP BY Student_Course;
输出:
Student_Course | MIN (Student_Marks) |
---|---|
B.tech | 78 |
MCA | 92 |
BBA | 88 |
MBA | 65 |
BCA | 97 |