SQLite MAX()函數

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的表,具有以下數據:

SQLite

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;
27
sqlite>

示例2:

使用具有的GROUP BY子句的MAX()函數:

student表中檢索NAME,ADDRESSMAX(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>