
- 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
Counting common elements in MySQL?
To count common elements, use COUNT() and GROUP BY. Let us first create a table −
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Number int ); Query OK, 0 rows affected (0.20 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(Number) values(10); Query OK, 1 row affected (0.06 sec) mysql> insert into DemoTable(Number) values(20); Query OK, 1 row affected (0.04 sec) mysql> insert into DemoTable(Number) values(20); Query OK, 1 row affected (0.04 sec) mysql> insert into DemoTable(Number) values(30); Query OK, 1 row affected (0.04 sec) mysql> insert into DemoTable(Number) values(10); Query OK, 1 row affected (0.05 sec) mysql> insert into DemoTable(Number) values(10); Query OK, 1 row affected (0.04 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+----+--------+ | Id | Number | +----+--------+ | 1 | 10 | | 2 | 20 | | 3 | 20 | | 4 | 30 | | 5 | 10 | | 6 | 10 | +----+--------+ 6 rows in set (0.00 sec)
Following is the query to count common elements −
mysql> select Number,count(Number) as CommonElement from DemoTable group by Number;
This will produce the following output −
+--------+---------------+ | Number | CommonElement | +--------+---------------+ | 10 | 3 | | 20 | 2 | | 30 | 1 | +--------+---------------+ 3 rows in set (0.00 sec)
- Related Articles
- Counting with condition in MySQL?
- Counting frequencies of array elements in C++
- Counting elements in two arrays using C++
- Counting unique elements in an array in JavaScript
- Counting same strings in a new MySQL column?
- Efficient algorithm for grouping elements and counting duplicates in JavaScript
- Function for counting frequency of space separated elements in JavaScript
- Counting below / par elements from an array - JavaScript
- JavaScript program for counting frequencies of array elements
- Counting presence of a NOT NULL value in MySQL
- Counting number of positive and negative votes in MySQL?
- Counting different distinct items in a single MySQL query?
- Perform multiple counting without using MySQL COUNT()?
- Counting elements of an array using a recursive function in JS?
- Common MySQL Queries

Advertisements