
- 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
MySQL Aggregate Function to find the number of occurrences?
To count the number of occurrences from a table, you can use aggregate function COUNT() with GROUP BY. The syntax is as follows −
SELECT yourColumnName,COUNT(*) as anyVariableName from yourTableName GROUP BY yourColumnName;
To understand the above syntax, let us create a table. The query to create a table is as follows −
mysql> create table CountOccurrences -> ( -> CarId int not null auto_increment, -> CarName varchar(30), -> PRIMARY KEY(CarId) -> ); Query OK, 0 rows affected (0.68 sec)
Insert some records in the table using insert command. The query is as follows −
mysql> insert into CountOccurrences(CarName) values('Aston Martin'); Query OK, 1 row affected (0.10 sec) mysql> insert into CountOccurrences(CarName) values('BMW'); Query OK, 1 row affected (0.20 sec) mysql> insert into CountOccurrences(CarName) values('Aston Martin'); Query OK, 1 row affected (0.15 sec) mysql> insert into CountOccurrences(CarName) values('Honda'); Query OK, 1 row affected (0.20 sec) mysql> insert into CountOccurrences(CarName) values('BMW'); Query OK, 1 row affected (0.11 sec) mysql> insert into CountOccurrences(CarName) values('Audi'); Query OK, 1 row affected (0.18 sec) mysql> insert into CountOccurrences(CarName) values('Aston Martin'); Query OK, 1 row affected (0.11 sec) mysql> insert into CountOccurrences(CarName) values('Bugatti'); Query OK, 1 row affected (0.22 sec) mysql> insert into CountOccurrences(CarName) values('BMW'); Query OK, 1 row affected (0.27 sec) mysql> insert into CountOccurrences(CarName) values('Honda'); Query OK, 1 row affected (0.13 sec) mysql> insert into CountOccurrences(CarName) values('Audi'); Query OK, 1 row affected (0.13 sec) mysql> insert into CountOccurrences(CarName) values('BMW'); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement. The query is as follows −
mysql> select *from CountOccurrences;
The following is the output −
+-------+--------------+ | CarId | CarName | +-------+--------------+ | 1 | Aston Martin | | 2 | BMW | | 3 | Aston Martin | | 4 | Honda | | 5 | BMW | | 6 | Audi | | 7 | Aston Martin | | 8 | Bugatti | | 9 | BMW | | 10 | Honda | | 11 | Audi | | 12 | BMW | +-------+--------------+ 12 rows in set (0.00 sec)
The following is the query to count the number of occurrences of each value in a column using count() with GROUP BY −
mysql> select CarName,count(*) as TotalCount from CountOccurrences -> group by CarName;
The following is the output −
+--------------+------------+ | CarName | TotalCount | +--------------+------------+ | Aston Martin | 3 | | BMW | 4 | | Honda | 2 | | Audi | 2 | | Bugatti | 1 | +--------------+------------+ 5 rows in set (0.00 sec)
- Related Articles
- MySQL query to find the number of occurrences from two columns?
- Find the average of column values in MySQL using aggregate function
- Get the maximum value of a column with MySQL Aggregate function
- Call aggregate function in sort order with MySQL
- How MySQL aggregate functions can be combined with MySQL IF() function?
- How to add column values in MySQL without using aggregate function?
- Select aggregate function and all other columns in MySQL
- Finding number of occurrences of a specific string in MySQL?
- MongoDB Aggregate to limit the number of records
- What happens if the argument ‘count’ in MySQL SUBSTRING_INDEX() function has the value greater than the total number of occurrences of delimiter?
- C++ Program to Find the Number of occurrences of a given Number using Binary Search approach
- MongoDB query to implement aggregate function
- Count the number of occurrences of a string in a VARCHAR field in MySQL?
- How to use Oracle aggregate function XMLAGG ?
- Resolve ERROR 1111 (HY000): Invalid use of group function in MySQL? How to correctly use aggregate function with where clause?

Advertisements