Get minimum value from a column (floating values) with corresponding duplicate ids in MySQL

MySQLMySQLi Database

To get minimum value from a column with corresponding duplicate ids, use GROUP BY and MIN() −

select min(yourColumnName) from yourTableName group by yourColumnName;

To understand the above syntax, let us create a table −

mysql> create table DemoTable2005
(
   Id int,
   Price float
);
Query OK, 0 rows affected (0.71 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable2005 values(1,56.88);
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable2005 values(1,120.56);
Query OK, 1 row affected (0.23 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable2005;

This will produce the following output −

+------+--------+
| Id   | Price  |
+------+--------+
|    1 |  56.88 |
|    1 | 120.56 |
+------+--------+
2 rows in set (0.00 sec)

Here is the query to get minimum value from a column wherein the corresponding column has duplicate ids −

mysql> select min(Price) from DemoTable2005 group by Id;

This will produce the following output −

+------------+
| min(Price) |
+------------+
|      56.88 |
+------------+
1 row in set (0.00 sec)
raja
Published on 02-Jan-2020 10:19:41
Advertisements