Let us first create a table −
mysql> create table DemoTable( ProductName varchar(100), ProductPrice int ); Query OK, 0 rows affected (0.68 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('Product-1',56);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values('Product-2',78);
Query OK, 1 row affected (0.23 sec)
mysql> insert into DemoTable values('Product-1',88);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values('Product-2',86);
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable values('Product-1',45);
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable values('Product-3',90);
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable values('Product-2',102);
Query OK, 1 row affected (0.22 sec)
mysql> insert into DemoTable values('Product-3',59);
Query OK, 1 row affected (0.13 sec)Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------------+--------------+ | ProductName | ProductPrice | +-------------+--------------+ | Product-1 | 56 | | Product-2 | 78 | | Product-1 | 88 | | Product-2 | 86 | | Product-1 | 45 | | Product-3 | 90 | | Product-2 | 102 | | Product-3 | 59 | +-------------+--------------+ 8 rows in set (0.00 sec)
Following is the query to fetch the maximum corresponding value from duplicate column values. Here, we are finding the maximum ProductPrice for a duplicate column like Product-1, Product-2, etc −
mysql> select ProductName,MAX(ProductPrice) from DemoTable group by ProductName;
This will produce the following output −
+-------------+-------------------+ | ProductName | MAX(ProductPrice) | +-------------+-------------------+ | Product-1 | 88 | | Product-2 | 102 | | Product-3 | 90 | +-------------+-------------------+ 3 rows in set (0.00 sec)