- 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
Display records with more than two occurrences in MySQL?
For this, you can use GROUP BY HAVING clause. Let us first create a table −
mysql> create table DemoTable -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Subject varchar(100) -> ); Query OK, 0 rows affected (0.53 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(Subject) values('MySQL'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(Subject) values('MongoDB'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable(Subject) values('MySQL'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(Subject) values('Java'); Query OK, 1 row affected (0.56 sec) mysql> insert into DemoTable(Subject) values('SQL Server'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(Subject) values('MongoDB'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(Subject) values('MySQL'); Query OK, 1 row affected (0.48 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
+----+------------+ | Id | Subject | +----+------------+ | 1 | MySQL | | 2 | MongoDB | | 3 | MySQL | | 4 | Java | | 5 | SQL Server | | 6 | MongoDB | | 7 | MySQL | +----+------------+ 7 rows in set (0.00 sec)
Following is the query to display distinct records with more than 2 occurrences in MySQL.
mysql> select Subject,count(Subject) freq from DemoTable -> group by Subject -> having count(Subject) > 2;
Output
+---------+------+ | Subject | freq | +---------+------+ | MySQL | 3 | +---------+------+ 1 row in set (0.00 sec)
Advertisements