
- 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
How to select multiple max values which would be duplicate values as well in MYSQL?
For this, use the join concept. Let us first create a −
mysql> create table DemoTable1389 -> ( -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> StudentMarks int -> ); Query OK, 0 rows affected (2.73 sec)
Insert some records in the table using insert command. Here, we have inserted duplicate values as well −
mysql> insert into DemoTable1389(StudentMarks) values(40); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable1389(StudentMarks) values(40); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1389(StudentMarks) values(68); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable1389(StudentMarks) values(78); Query OK, 1 row affected (0.43 sec) mysql> insert into DemoTable1389(StudentMarks) values(97); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable1389(StudentMarks) values(97); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable1389(StudentMarks) values(97); Query OK, 1 row affected (0.49 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1389;
This will produce the following output −
+-----------+--------------+ | StudentId | StudentMarks | +-----------+--------------+ | 1 | 40 | | 2 | 40 | | 3 | 68 | | 4 | 78 | | 5 | 97 | | 6 | 97 | | 7 | 97 | +-----------+--------------+ 7 rows in set (0.00 sec)
Following is the query to select multiple max values −
mysql> select tbl.StudentId,tbl.StudentMarks from DemoTable1389 tbl -> join ( select max(StudentMarks) as MaxMarks from DemoTable1389) tbl1 -> on tbl1.MaxMarks=tbl.StudentMarks;
This will produce the following output displaying max value, with the duplicates as well −
+-----------+--------------+ | StudentId | StudentMarks | +-----------+--------------+ | 5 | 97 | | 6 | 97 | | 7 | 97 | +-----------+--------------+ 3 rows in set (0.00 sec)
- Related Articles
- Select query to display duplicate values with max date
- MySQL Select Multiple VALUES?
- How to concat Values in MySQL Query and to handle Null values as well?
- MySQL select only duplicate records from database and display the count as well?
- How to get the max of two values MySQL?
- MySQL query to skip the duplicate and select only one from the duplicated values
- How to replace values of select return in MySQL?
- Select minimum row value from a column with corresponding duplicate column values in MySQL
- MySQL query to select the values having multiple occurrence and display their count
- How number values be used as arguments in MySQL STRCMP() function?
- MySQL randomly select 2 values from column values?
- Select multiple values with MongoDB OR operator
- How to order return duplicate column values only once in MySQL?
- How to add duplicate varchar values without displaying error in MySQL?
- MySQL UNIQUE declaration to avoid inserting duplicate values?

Advertisements