
- 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
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)
- Related Articles
- Find out the records of students with more than a specific score in MySQL?
- Display records from two columns based on comparison in MySQL?
- Find “greatest” between two columns and display with some records already null in MySql
- Count number of occurrences of records in a MySQL table and display the result in a new column?
- Display selected records from a MySQL table with IN() operator
- Display USD currency records with the correct format in MySQL
- Display records ignoring NULL in MySQL
- MySQL query to subtract date records with week day and display the weekday with records
- Display distinct dates in MySQL from a column with date records
- Count the occurrences of specific records (duplicate) in one MySQL query
- Find and display duplicate records in MySQL?
- Display all records except one in MySQL
- Display records by grouping dates in MySQL
- Display records with conditions set using if statement in UPDATE statement with MySQL
- MySql how to display the records with latest ID in a table?

Advertisements