
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
MySQL query to fetch the maximum corresponding value from duplicate column values
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)
- Related Articles
- MySQL rows concatenation to fetch maximum corresponding value from duplicate IDs?
- Select minimum row value from a column with corresponding duplicate column values in MySQL
- Fetch the maximum value from a MySQL column?
- MySQL query to return the count of only NO values from corresponding column value
- MySQL query to fetch the maximum cumulative value
- Get minimum value from a column (floating values) with corresponding duplicate ids in MySQL
- Add records from corresponding duplicate values in another column with MySQL
- Fetch specific rows from a MySQL table with duplicate column values (names)?
- Two ways to fetch maximum value from a MySQL column with numbers
- MySQL query to display only the column values with corresponding column having whitespace
- MySQL query to count number of duplicate values in a table column
- MySQL query to fetch multiple least values?
- How to concatenate columns based on corresponding duplicate id values in MySQL? Display the duplicate values in the same column separated by slash
- MySQL db query to fetch records from comma separate values on the basis of a specific value
- MySQL query to remove special characters from column values?

Advertisements