
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
Find Rows with Same Value in a Column in MySQL
First, we will create a table and insert some values into the table. Let us create a table.
mysql> create table RowValueDemo -> ( -> Name varchar(100) -> ); Query OK, 0 rows affected (0.69 sec)
Insert records using the insert command. We have added duplicate values as well for our example.
mysql> insert into RowValueDemo values('John'); Query OK, 1 row affected (0.14 sec) mysql> insert into RowValueDemo values('Bob'); Query OK, 1 row affected (0.14 sec) mysql> insert into RowValueDemo values('Carol'); Query OK, 1 row affected (0.11 sec) mysql> insert into RowValueDemo values('John'); Query OK, 1 row affected (0.24 sec) mysql> insert into RowValueDemo values('John'); Query OK, 1 row affected (0.09 sec) mysql> insert into RowValueDemo values('John'); Query OK, 1 row affected (0.10 sec) mysql> insert into RowValueDemo values('Bob'); Query OK, 1 row affected (0.09 sec) mysql> insert into RowValueDemo values('Bob'); Query OK, 1 row affected (0.18 sec)
Displaying all records with the help of select statement.
mysql> select *from RowValueDemo;
Here is the output.
+-------+ | Name | +-------+ | John | | Bob | | Carol | | John | | John | | John | | Bob | | Bob | +-------+ 8 rows in set (0.00 sec)
Now, we ill get only the rows that have same value for column using the following query −
mysql> select * from RowValueDemo where Name in ( -> select Name from RowValueDemo -> group by Name having count(*) > 1 -> );
The following is the output.
+------+ | Name | +------+ | John | | Bob | | John | | John | | John | | Bob | | Bob | +------+ 7 rows in set (0.06 sec)
Advertisements