コンテンツにスキップ

集計関数です。

MAX() 関数は、値の集合から最大値を返します。

func.max(<column>)
table.city, func.max(table.temperature).alias('max_temperature')
| city | max_temperature |
|------------|-----------------|
| New York | 32 |
MAX(<expr>)
引数 説明
<expr> 任意の式

最大値。その値の型で返されます。

テーブルの作成とサンプルデータの挿入

CREATE TABLE temperatures (
id INT,
city VARCHAR,
temperature FLOAT
);
INSERT INTO temperatures (id, city, temperature)
VALUES (1, 'New York', 30),
(2, 'New York', 28),
(3, 'New York', 32),
(4, 'Los Angeles', 25),
(5, 'Los Angeles', 27);

クエリのデモ: ニューヨーク市の最高気温を求める

SELECT city, MAX(temperature) AS max_temperature
FROM temperatures
WHERE city = 'New York'
GROUP BY city;

結果

| city | max_temperature |
|------------|-----------------|
| New York | 32 |