
- 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
Count the occurrences of specific records (duplicate) in one MySQL query
For this, use aggregate function COUNT() and GROUP BY to group those specific records for occurrences. Let us first create a table −
mysql> create table DemoTable ( StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, StudentSubject varchar(40) ); Query OK, 0 rows affected (5.03 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(StudentSubject) values('MySQL'); Query OK, 1 row affected (0.78 sec) mysql> insert into DemoTable(StudentSubject) values('Java'); Query OK, 1 row affected (0.39 sec) mysql> insert into DemoTable(StudentSubject) values('MySQL'); Query OK, 1 row affected (1.12 sec) mysql> insert into DemoTable(StudentSubject) values('MongoDB'); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable(StudentSubject) values('Java'); Query OK, 1 row affected (0.45 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-----------+----------------+ | StudentId | StudentSubject | +-----------+----------------+ | 1 | MySQL | | 2 | Java | | 3 | MySQL | | 4 | MongoDB | | 5 | Java | +-----------+----------------+ 5 rows in set (0.00 sec)
Following is the query to count the occurrences of specific records (duplicate) in one MySQL query −
mysql> select StudentSubject,count(StudentId) from DemoTable group by StudentSubject;
This will produce the following output −
+----------------+------------------+ | StudentSubject | count(StudentId) | +----------------+------------------+ | MySQL | 2 | | Java | 2 | | MongoDB | 1 | +----------------+------------------+ 3 rows in set (0.00 sec)
- Related Articles
- MySQL query to display the count of distinct records from a column with duplicate records
- MySQL query to count records that begin with specific letters
- How to count the number of occurrences of a specific value in a column with a single MySQL query?
- MySQL query to alphabetize records and count the duplicates?
- MySQL query to get the count of distinct records in a column
- How do we count the total duplicate records in a column of MySQL table?
- MySQL query to find duplicate tuples and display the count?
- Display the count of duplicate records from a column in MySQL and order the result
- MySQL query to count number of duplicate values in a table column
- Count number of occurrences of records in a MySQL table and display the result in a new column?
- Using GROUP BY and COUNT in a single MySQL query to group duplicate records and display corresponding max value
- MySQL query to select records beginning from a specific id
- MySQL query to count occurrences of distinct values and display the result in a new column?
- MySQL select only duplicate records from database and display the count as well?
- MySQL query for grouping and summing the values based on specific records

Advertisements