
- 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 query to group by names and display the count in a new column
Use GROUP BY with the COUNT() method. Group the names with GROUP BY and count using the COUNT() method. Let us first create a table −
mysql> create table DemoTable ( Name varchar(30) ); Query OK, 0 rows affected (0.63 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('Chris'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('Robert'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('Mike'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('Robert'); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable values('Mike'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values('David'); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+--------+ | Name | +--------+ | Chris | | Robert | | Mike | | Robert | | Mike | | David | +--------+ 6 rows in set (0.00 sec)
Following is the query to group by names and display the count −
mysql> select Name,count(Name) as NumberOfRowsPerName from DemoTable group by Name;
This will produce the following output −
+--------+---------------------+ | Name | NumberOfRowsPerName | +--------+---------------------+ | Chris | 1 | | Robert | 2 | | Mike | 2 | | David | 1 | +--------+---------------------+ 4 rows in set (0.00 sec)
- Related Articles
- MySQL query to count occurrences of distinct values and display the result in a new column?
- MySQL query to group by column and display the sum of similar values in another column
- MySQL query to group results by date and display the count of duplicate values?
- MySQL query to get the count of values and display the count in a new column ordered in descending order\n
- Using GROUP BY and COUNT in a single MySQL query to group duplicate records and display corresponding max value
- MySQL query to display record with maximum count values in a group with other column values?
- MySQL query to count the duplicate ID values and display the result in a separate column
- GROUP BY and display only non-empty column values in MySQL
- MySQL query to get the length of all columns and display the result in a single new column?
- MySQL query to add days with interval of 45 days and display the output in a new column
- Query to divide the values of two columns and display the result in a new column using MySQL wildcard?
- Group by one column and display corresponding records from another column with a separator in MySQL
- Count number of occurrences of records in a MySQL table and display the result in a new column?
- MySQL query to display the count of distinct records from a column with duplicate records
- How to group by column name and ensure the query retrieves the last update in MySQL?

Advertisements