- 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
Select the maximum for each value in a MySQL table?
For this, use GROUP BY clause along with MAX(). Let us first create a table −
mysql> create table DemoTable -> ( -> CountryName varchar(20), -> Population int -> ); Query OK, 0 rows affected (0.56 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('US',560); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values('UK',10090); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('UK',8794); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('US',1090); Query OK, 1 row affected (0.21 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------------+------------+ | CountryName | Population | +-------------+------------+ | US | 560 | | UK | 10090 | | UK | 8794 | | US | 1090 | +-------------+------------+ 4 rows in set (0.00 sec)
Following is the query to select the maximum for each value in a MySQL table −
mysql> select CountryName,max(Population) from DemoTable group by CountryName;
This will produce the following output −
+-------------+-----------------+ | CountryName | max(Population) | +-------------+-----------------+ | US | 1090 | | UK | 10090 | +-------------+-----------------+ 2 rows in set (0.00 sec)
- Related Articles
- How to select the maximum value of a column in MySQL?
- Conditional select between dates in MySQL for maximum and minimum values of price set in a table?
- Fetch maximum ID value from the first table and insert to all the IDs in another table with MySQL INSERT INTO select?
- Select three random records with a fixed number of characters for each column value in MySQL
- View the auto_increment value for a table in MySQL without using SHOW TABLE?
- Setting similar value for a column in a MySQL table?
- How to SELECT min and max value from the part of a table in MySQL?
- A single MySQL query to select value from first table and insert in the second?
- Select from table where value does not exist with MySQL?
- Update only a single value from a MySQL table where select from same table ordered in descending order?
- Select the minimum value from the maximum values of two tables with a single MySQL query?
- How to find the maximum value for each column of a matrix in R?
- Set value only for NULL values in a MySQL table
- Insert values in a table by MySQL SELECT from another table in MySQL?
- In MySQL how to select the top 2 rows for each group?

Advertisements