
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 Multiple Max Values Including Duplicates 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)
Advertisements