
- 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 rows concatenation to fetch maximum corresponding value from duplicate IDs?
For this, you can use GROUP BY clause. To find maximum value, use MAX() function. Let us first create a table −
mysql> create table DemoTable1804 ( Id int, Marks1 int, Marks2 int, Marks3 int ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1804 values(1,56,89,34); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1804 values(1,98,null,94); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1804 values(2,34,45,78); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1804 values(2,null,67,null); Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1804;
This will produce the following output −
+------+--------+--------+--------+ | Id | Marks1 | Marks2 | Marks3 | +------+--------+--------+--------+ | 1 | 56 | 89 | 34 | | 1 | 98 | NULL | 94 | | 2 | 34 | 45 | 78 | | 2 | NULL| 67 | NULL | +------+--------+--------+--------+ 4 rows in set (0.00 sec)
Here is the query for rows concatenation and finding maximum value −
mysql> select Id,max(Marks1),max(Marks2),max(Marks3) from DemoTable1804 group by Id;
This will produce the following output −
+------+-------------+-------------+-------------+ | Id | max(Marks1) | max(Marks2) | max(Marks3) | +------+-------------+-------------+-------------+ | 1 | 98 | 89 | 94 | | 2 | 34 | 67 | 78 | +------+-------------+-------------+-------------+ 2 rows in set (0.00 sec)
- Related Articles
- MySQL query to fetch the maximum corresponding value from duplicate column values
- Display highest amount from corresponding duplicate ids in MySQL
- Get minimum value from a column (floating values) with corresponding duplicate ids in MySQL
- Find average of corresponding records (Product Price) from duplicate product ids in MYSQL
- Fetch the maximum value from a MySQL column?
- Fetch specific rows from a MySQL table with duplicate column values (names)?
- Fetch maximum ID value from the first table and insert to all the IDs in another table with MySQL INSERT INTO select?
- MySQL query to fetch the maximum cumulative value
- Two ways to fetch maximum value from a MySQL column with numbers
- Update table with duplicate ids in MySQL
- Select minimum row value from a column with corresponding duplicate column values in MySQL
- Fetch random rows from a table with MySQL
- SUM corresponding duplicate records in MySQL
- Finding the average and display the maximum average of duplicate ids?
- Add records from corresponding duplicate values in another column with MySQL

Advertisements