- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- 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
Delete rows with duplicate and similar content & get row with maximum number with MySQL select statement?
Let us first create a table −
mysql> create table DemoTable1468 -> ( -> Id int, -> Name varchar(20), -> Age int -> ); Query OK, 0 rows affected (1.21 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1468 values(100,'Chris',23); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable1468 values(101,'Bob',25); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1468 values(102,'David',30); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1468 values(100,'Chris',23); Query OK, 1 row affected (0.35 sec) mysql> insert into DemoTable1468 values(100,'Chris',38); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1468 values(101,'Bob',23); Query OK, 1 row affected (0.13 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1468;
This will produce the following output −
+------+-------+------+ | Id | Name | Age | +------+-------+------+ | 100 | Chris | 23 | | 101 | Bob | 25 | | 102 | David | 30 | | 100 | Chris | 23 | | 100 | Chris | 38 | | 101 | Bob | 23 | +------+-------+------+ 6 rows in set (0.00 sec)
Here is the query to delete rows with duplicate and similar content and get row with max number with select statement −
mysql> select Id,Name,Age from DemoTable1468 -> group by Name,Id -> having max(Age);
This will produce the following output −
+------+-------+------+ | Id | Name | Age | +------+-------+------+ | 100 | Chris | 23 | | 101 | Bob | 25 | | 102 | David | 30 | +------+-------+------+ 3 rows in set (0.00 sec)
Advertisements