sql min函数_SQL min()和max()函数

本文介绍了SQL中的MIN()和MAX()函数,用于查找数据列中的最小和最大值。通过示例展示了如何使用这两个函数以及如何结合别名来提高可读性。文章还提供了总结和参考资料链接,帮助读者深入理解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

sql min函数

Hello there! In this tutorial, we are going to discuss the use and working of the SQL min() and max() functions.

你好! 在本教程中,我们将讨论SQL min()和max()函数的使用和工作。

So let us get right into the topic.

因此,让我们直接进入主题。

SQL MIN()函数 (SQL MIN() Function)

The SQL min() function is used to find the minimum/lowest value in the given column. The syntax for using the function is given below.

SQL min()函数用于在给定列中查找最小值/最小值 。 下面给出了使用该函数的语法。


SELECT MIN(column) 
FROM Table
WHERE condition;

Where, column and Table are the column name and table name respectively for which we need to find the minimum value. The WHERE condition is used to narrow down the set of values on the basis of the condition.

其中, columnTable分别是我们需要查找最小值的列名和表名。 的WHERE条件用于缩小的设定值的基础上condition

Let us look at an example to get a better understanding.

让我们看一个例子,以更好地理解。

Example: The table below contains the score of 3 different students in 3 different subjects(Maths, Science, and English) out of 100.

示例 :下表包含3个不同学科在3个不同学科(数学,科学和英语)中的100分(满分100)。

RollNameMathsScienceEnglish
1John958990
2Kate789091
3Alex879289
名称 数学 科学 英语
1个 约翰 95 89 90
2 凯特 78 90 91
3 亚历克斯 87 92 89
Table – Marks
表–标记

Now, let us try to get the minimum marks that a student scored at Maths.

现在,让我们尝试获得学生在数学上得分的最低分数。


SELECT MIN(Maths)
FROM Marks;

Output:

输出:


78

Example – To calculate the overall minimum marks that a student scored out of 300.

示例–计算学生满分300分的总最低分。


SELECT MIN(Maths + Science + English)
FROM Marks;

Output:

输出:


259

We get the desired output as Roll 2 got the lowest total marks(78 + 90 + 91 = 259).

我们得到了期望的输出,因为第2卷获得了最低的总成绩(78 + 90 + 91 = 259 )。

SQL MAX()函数 (SQL MAX() Function)

The SQL max() function on the other hand is used to find the maximum/highest value in the given column. The syntax for using the function is given below.

另一方面,SQL max()函数用于查找给定列中的最大值/最大值 。 下面给出了使用该函数的语法。


SELECT MAX(column) 
FROM Table
WHERE condition;

Similar to the min() function, here too column and Table are the column name and table name respectively for which the maximum value is to be calculated. The WHERE condition is used to narrow down the set of values on the basis of the condition.

min()函数类似,这里的columnTable分别是要为其计算最大值的列名和表名。 的WHERE条件用于缩小的设定值的基础上condition

Example – Considering the same Marks table used for the min() function, let us this time try to calculate the maximum marks that a student scored in English.

示例–考虑与min()函数使用的Marks表相同,让我们这次尝试计算学生在英语中得分的最高分数。


SELECT MAX(English)
FROM Marks;

Output:

输出:


91

Example – To calculate the maximum marks that a student scored out of 300 in all the subjects.

示例–计算学生在所有科目中满分300分的最高分数。


SELECT MAX(Maths + Science + English)
FROM Marks;

Output:

输出:


274

From the table Marks it is clear that Roll 1 has the highest total marks(95 + 90 + 89 = 274). Hence, the output is justified.

从表Marks中可以明显看出, 第一卷的总得分最高(95 + 90 + 89 = 274 )。 因此,输出是合理的。

具有别名SQL MIN()和MAX() (SQL MIN() and MAX() with Alias)

Alias in SQL is used to change the display name of a table or column. This feature is very helpful for increasing readability for the users. This is achieved using the AS keyword.

SQL中的别名用于更改表或列的显示名称。 此功能对于提高用户的可读性很有帮助。 这是使用AS关键字实现的。

Note: These Aliases are temporary names given for user’s convenience.

注意:这些别名是为方便用户而给出的临时名称。

1. MIN()的别名 (1. Alias with MIN())

Let us look at an example.

让我们来看一个例子。


SELECT MIN(Science) AS Min_Sci_Marks
FROM Marks;

Output:

输出:

Min_Sci_Marks
89
Min_Sci_Marks
89

As you can see, this time the column name is changed(temporary).

如您所见,这次列名已更改(临时)。

2.使用MAX()的别名 (2. Alias with MAX())

Similarly for max() function,

对于max()函数,


SELECT MAX(Science) AS Max_Sci_Marks
FROM Marks;

Output:

输出:

Max_Sci_Marks
92
Max_Sci_Marks
92

加起来 (Summing Up)

So that’s it for today. Hope you had a clear understanding of the SQL min() and max() functions. We recommend going through the links given in the references section for more info.

今天就这样。 希望您对SQL min()和max()函数有一个清晰的了解。 我们建议您通过参考部分提供的链接获取更多信息。

You can also go through our SQL Tutorial.

您也可以阅读我们的SQL教程

For any further questions, feel free to use the comments below.

如有其他疑问,请随时使用以下评论。

参考资料 (References)

翻译自: https://www.journaldev.com/40761/sql-min-max-functions

sql min函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值