Maximum of Column per Group in MySQL

MySQLMySQLi Database

Let us understand how to find the maximum of a column per group in MySQL −

SELECT colName1, MAX(colName2)
FROM tableName
GROUP BY colName1
ORDER BY colName1;

We will now see a live example. Let’s say we have a table PRODUCT −

<PRODUCT>

+---------+--------+
| Article | Price  |
+---------+--------+
| 1       | 255.50 |
| 1       | 256.05 |
| 2       | 90.50  |
| 3       | 120.50 |
| 3       | 123.10 |
| 3       | 122.10 |
+---------+--------+

Following is the query to get the maximum of column per group −

Query

SELECT Article, MAX(Price) AS MaxPrice
FROM Product
GROUP BY Article
ORDER BY Article;

Output

+--------------+--------------+
| Article      | MaxPrice     |
+--------------+--------------+
| 0001         | 256.05       |
| 0002         | 90.50        |
| 0003         | 123.10       |
+--------------+--------------+
raja
Published on 09-Mar-2021 13:05:28
Advertisements