
- 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
How to count the number of occurrences of a specific value in a column with a single MySQL query?
For this, you can use GROUP BY clause along with IN(). Let us first create a table −
mysql> create table DemoTable -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Name varchar(100) -> ); Query OK, 0 rows affected (0.87 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(Name) values('John'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable(Name) values('Chris'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(Name) values('David'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable(Name) values('Chris'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(Name) values('Chris'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable(Name) values('John'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(Name) values('Carol'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable(Name) values('Sam'); Query OK, 1 row affected (0.19 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+----+-------+ | Id | Name | +----+-------+ | 1 | John | | 2 | Chris | | 3 | David | | 4 | Chris | | 5 | Chris | | 6 | John | | 7 | Carol | | 8 | Sam | +----+-------+ 8 rows in set (0.00 sec)
Now, count the number of occurrences of a specific value in a column with a single query −
mysql> select Name,count(*) AS Occurrences from DemoTable -> where Name in('John','Chris') group by Name;
This will produce the following output −
+-------+-------------+ | Name | Occurrences | +-------+-------------+ | John | 2 | | Chris | 3 | +-------+-------------+ 2 rows in set (0.00 sec)
- Related Articles
- How to get the count of a specific value in a column with MySQL?
- MySQL query to count rows with a specific column?
- Get multiple count in a single MySQL query for specific column values
- MySQL query to count the number of times a specific integer appears in a column for its corresponding value in another column
- Count the occurrences of specific records (duplicate) in one MySQL query
- MySQL query to count occurrences of distinct values and display the result in a new column?
- How to use a single MySQL query to count column values ignoring null?
- MySQL query to count number of duplicate values in a table column
- Finding number of occurrences of a specific string in MySQL?
- Count number of occurrences of records in a MySQL table and display the result in a new column?
- Subtracting a number from a single MySQL column value?
- Count the number of occurrences of a string in a VARCHAR field in MySQL?
- Count and sort rows with a single MySQL query
- A single MySQL query to update only specific records in a range without updating the entire column
- Selecting a value in custom order from another column in a MySQL table with a single query

Advertisements