
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- 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
- MongoDB Aggregate to limit the number of records
- C++ Program to Find the Number of occurrences of a given Number using Binary Search approach
- Finding number of occurrences of a specific string in MySQL?
- Call aggregate function in sort order with MySQL
- Count the number of occurrences of a string in a VARCHAR field in MySQL?
- MongoDB query to implement aggregate function
- Select aggregate function and all other columns in MySQL
- How MySQL aggregate functions can be combined with MySQL IF() function?
- Unique Number of Occurrences in Python
- How to add column values in MySQL without using aggregate function?
- Python Program to Find Number of Occurrences of All Elements in a Linked List
- What happens if the argument ‘count’ in MySQL SUBSTRING_INDEX() function has the value greater than the total number of occurrences of delimiter?
Advertisements