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)。

Roll Name Maths Science English
1 John 95 89 90
2 Kate 78 90 91
3 Alex 87 92 89
名称 数学 科学 英语
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)

  • SQL Functions,SQL函数
  • Alias in SQL,SQL的别名 ,
  • SQL sum(), count() and avg() functions.SQL sum(),count()和avg()函数 。

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

sql min函数

sql min函数_SQL min()和max()函数相关推荐

  1. 习题 9.5 建立一个对象数组,内放5个学生的数据(学号、成绩),设立一个函数max,用指向对象的指针作函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号。

    C++程序设计(第三版) 谭浩强 习题9.5 个人设计 习题 9.5 建立一个对象数组,内放5个学生的数据(学号.成绩),设立一个函数max,用指向对象的指针作函数参数,在max函数中找出5个学生中成 ...

  2. java中rank函数_sql rank()函数

    RANK() OVER([]) partition_by_clause 将from子句生成的结果集划分为应用到RANK函数的分区. Order_by_clause确定将RANK值应用到分区中的行时所使 ...

  3. mysql输出max函数_MySQL中的max()函数使用教程

    MySQL的max()函数是用来找出一个记录集中的最大值记录. 要了解MAX功能考虑的EMPLOYEE_TBL表具有以下记录: mysql> SELECT * FROM employee_tbl ...

  4. python中max函数用法_Python中max函数用法实例分析

    Python中max函数用法实例分析 更新时间:2015年07月17日 15:45:09 作者:优雅先生 这篇文章主要介绍了Python中max函数用法,实例分析了Python中max函数的功能与使用 ...

  5. java中rank函数_SQL中的排名函数(ROW_NUMBER、RANK、DENSE_RANK、NTILE)简介

    排名函数是Sql Server2005新增的功能,下面简单介绍一下他们各自的用法和区别. 在使用排名函数的时候需要注意以下三点: 1.排名函数必须有 OVER 子句. 2.排名函数必须有包含 ORDE ...

  6. python中max函数用法_python的max函数的用法,值得收藏

    本文,用max函数来找出数据中的最大值. 工具/材料 电脑 python 操作方法 01 从一串数字里面,找出最大值. max([5,6,7,8]) 02 从字符串里面找出最大值. max('5678 ...

  7. sql中聚合函数和分组函数_SQL选择计数聚合函数-语法示例解释

    sql中聚合函数和分组函数 The COUNT operator is usually used in combination with a GROUP BY clause. It is one of ...

  8. Java choose函数_SQL SERVER 2012新增函数之逻辑函数CHOOSE详解

    SQL SERVER 2012中新增了CHOOSE,该函数可以从值列表返回指定索引处的项. 例如: select CHOOSE(3,'A','B','C','D') as R1 /* R1 ---- ...

  9. java datediff函数_SQL语句中DateDiff函数说明

    函数简介: 返回 Variant (Long) 的值,表示两个指定日期间的时间间隔数目. 函数语法: DateDiff(interval, date1, date2[, firstdayofweek[ ...

最新文章

  1. 微服务架构的优势与不足(三)
  2. Android5.x新特性之 Toolbar和Theme的使用
  3. 安卓AS_git 进行更新远程分支
  4. linux运行c程序a. out,bash:./a.out:在由ld生成的运行可执行文件上没有这样的文件或目录。...
  5. java基本数据类型线程_Java基本数据类型
  6. 【读书笔记】程序是怎么跑起来的 矢泽久雄
  7. C#知识点总结系列:3、C#中Delegate和Event
  8. 苏宁MOCK测试桩服务建设实践
  9. h5跳转小程序页面url_微信小程序页面跳转方法
  10. 那些基础不好的程序员,后来怎么样了?
  11. TinyXML中文文档,TinyXPath
  12. HFSS天线设计笔记-------主极化与交叉极化
  13. 设为首页 加入收藏代码
  14. LOLBox多玩饭盒Android源码
  15. 使用回溯算法解决排列组合问题
  16. NETDMIS5.0位置度评价案例1
  17. 关于我对网赚的思路,赚钱的方法透露
  18. 大数据使磁带重获新生
  19. 打印魔方阵(C语言)
  20. 【有利可图网】PS实战系列:果汁喷溅的效果,你知道是怎么做的吗?

热门文章

  1. js 跳转html,js页面跳转方式 js页面跳转举例
  2. vue 自定义图片文件上传结合elementui
  3. 码代码微信号今日上线,为互联网同仁提供最前沿咨询
  4. 元旦假期!10分钟用Python批量定制化发送元旦祝福邮件!
  5. 语音翻译工具推荐,实现多国语言互译
  6. 迅雷看看与RealMedia codec
  7. iFunFactory是一家韩国游戏服务器公司
  8. 2021前端学习路径书单—自我成长之路
  9. 新概念二册 Lesson 10 Not for jazz不适于演奏爵士乐 (被动语态+名词所有格+双重所有格)
  10. 任意输入三个数字,按从小到大输出(Python)