- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Using GROUP BY and MAX on multiple columns in MySQL?
To understand the GROUP BY and MAX on multiple columns, let us first create a table. The query to create a table is as follows −
mysql> create table GroupByMaxDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> CategoryId int, -> Value1 int, -> Value2 int -> ); Query OK, 0 rows affected (0.68 sec)
Example
Insert some records in the table using insert command. The query is as follows −
mysql> insert into GroupByMaxDemo(CategoryId, Value1,Value2) values(10,100,50); Query OK, 1 row affected (0.15 sec) mysql> insert into GroupByMaxDemo(CategoryId, Value1,Value2) values(10,100,70); Query OK, 1 row affected (0.21 sec) mysql> insert into GroupByMaxDemo(CategoryId, Value1,Value2) values(10,50,100); Query OK, 1 row affected (0.22 sec) mysql> insert into GroupByMaxDemo(CategoryId, Value1,Value2) values(20,180,150); Query OK, 1 row affected (0.19 sec)
Display all records from the table using a select statement. The query is as follows −
mysql> select *from GroupByMaxDemo;
Output
+----+------------+--------+--------+ | Id | CategoryId | Value1 | Value2 | +----+------------+--------+--------+ | 1 | 10 | 100 | 50 | | 2 | 10 | 100 | 70 | | 3 | 10 | 50 | 100 | | 4 | 20 | 180 | 150 | +----+------------+--------+--------+ 4 rows in set (0.00 sec)
Example
The following is the query to use GROUP BY and MAX on multiple columns −
mysql> select tbl2.CategoryId, tbl2.Value1, max(tbl2.Value2) -> from -> ( -> select CategoryId, max(Value1) as `Value1` -> from GroupByMaxDemo -> group by CategoryId -> ) tbl1 -> inner join GroupByMaxDemo tbl2 on tbl2.CategoryId = tbl1.CategoryId and tbl2.Value1 = tbl1.Value1 -> group by tbl2.CategoryId, tbl2.Value1;
Output
+------------+--------+------------------+ | CategoryId | Value1 | max(tbl2.Value2) | +------------+--------+------------------+ | 10 | 100 | 70 | | 20 | 180 | 150 | +------------+--------+------------------+ 2 rows in set (0.00 sec)
- Related Articles
- MySQL query to GROUP BY multiple columns
- What is the significance of using multiple columns in MySQL GROUP BY clause?
- Using GROUP BY and COUNT in a single MySQL query to group duplicate records and display corresponding max value
- Using group by on two fields and count in MySQL?
- MySQL filtering by multiple columns?
- Can we use MySQL GROUP BY clause with multiple columns like MySQL DISTINCT clause is used?
- How to order MySQL rows by multiple columns?
- Order by multiple columns not working as expected in MySQL?
- How to use MySQL DISTINCT clause on multiple columns?
- MySQL multiple COUNT with multiple columns?
- Find the frequency of exclusive group combinations based on multiple categorical columns in R.
- Sum with MongoDB group by multiple columns to calculate total marks with duplicate ids
- Limit the count using GROUP BY in MySQL
- MySQL GROUP BY date when using datetime?
- Is there a MAX function for rows and not for columns in MySQL?

Advertisements