SQLite MAX()
函数用于获取表达式或给定列的最大值。
语法
SELECT MAX(aggregate_expression) FROM tables [WHERE conditions];
在GROUP BY
子句中使用Max()
函数时的语法:
SELECT expression1, expression2, ... expression_n MAX(aggregate_expression) FROM tables [WHERE conditions] GROUP BY expression1, expression2, ... expression_n;
示例1:
假设有一个名为student
的表,具有以下数据:
从student
表中检索学生的最高费用(fees
):
SELECT MAX(FEES) AS "Highest Fees" FROM STUDENT; -- 最大年龄 SELECT MAX(age) AS "Highest age" FROM STUDENT;
执行上面查询代码,得到以下结果 -
sqlite> SELECT MAX(FEES) AS "Highest Fees" FROM STUDENT; 65000.0 sqlite> sqlite> -- 最大年龄 sqlite> SELECT MAX(age) AS "Highest age" FROM STUDENT; sqlite>
示例2:
使用具有的GROUP BY
子句的MAX()
函数:
从student
表中检索NAME
,ADDRESS
和MAX(FEES)
,并按ADDRESS
的数据排序:
SELECT NAME, ADDRESS, MAX(FEES) AS "Highest FEES" FROM STUDENT WHERE ID <= 8 GROUP BY ADDRESS;
执行上面代码,得到以下结果 -
sqlite> SELECT NAME, ADDRESS, MAX(FEES) AS "Highest FEES" ...> FROM STUDENT ...> WHERE ID <= 8 ...> GROUP BY ADDRESS; Minsu|Beijing|15000.0 Linsu|Guangzhou|65000.0 Sqlsu|Haikou|25000.0 Avgsu|Shanghai|2000.0 Maxsu|Shengzhen|20000.0 sqlite>