EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Data Science Data Science Tutorials MySQL Tutorial MySQL GROUP BY Count

MySQL GROUP BY Count

By Aanchal SharmaAanchal Sharma

MySQL GROUP BY Count

Introduction to MySQL GROUP BY Count

MySQL GROUP BY Count is a MySQL query responsible for showing the grouping of rows on the basis of column values along with the aggregate function Count. The GROUP BY clause forms a cluster of rows into a type of summary table rows using the table column value or any expression. We also implement the GROUP BY clause with MySQL aggregate functions for grouping the rows with some calculated value in the column. This may include MAX, MIN, COUNT, SUM, and AVG, where it is used with a SELECT statement that provides info about every group in the result set. Similarly, when we apply the COUNT() function with this GROUP BY clause, it will show the number of counts for each specified grouped row in the table query.

Syntax

The following syntax is the basic structure to query using GROUP BY Count clause in MySQL:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Code:

SELECT Col_1, Col_2,…., Col_n, AggregateFunction(Col_i) FROM TableName
WHERE Cond_Expr GROUP BY Col_1, Col_2,…, Col_n;

Explanation: With the SELECT statement, the GROUP BY clause is an option to provide a grouped result set according to a specified column value. The comma-separated lists: Col_1, Col_2,…, Col_n denote the column values to be fetched in the result set, and one of them will be defined for aggregate function and GROUP BY clause to write the proper query. The MySQL will only evaluate the GROUP BY clause after the SELECT, FROM, and WHERE clauses but should be placed before ORDER BY, HAVING, and LIMIT clauses.

How GROUP BY count works in MySQL?

The GROUP BY clause becomes essential when arranging rows with similar values into subgroups within a collection of result sets. It summarizes the rows by the column data specified in the query. The MySQL GROUP BY clause returns a single row for every arranged group in the output set. This method helps decrease the number of rows in the set of results MySQL provides. Generally, with this GROUP BY clause, we add some aggregate functions, including COUNT, SUM, MIN, MAX, and AVG, which give information about the groups formed using the GROUP BY query in the result rows.

In MySQL, the aggregate function executes the calculation of a set of table rows and outputs a value. Thus, the COUNT() function is used with the MySQL GROUP BY clause to complete the calculation, giving a distinct value for every subgroup formed. If you want to display the result set with unique values to occur with the GROUP BY Count clause, we need to use the DISTINCT operator just after the SELECT clause in the query and before the column names specified. For example,

SELECT DISTINCT ColumnName FROM TableName;

Using the COUNT() function with the GROUP BY clause, then the query will produce the number of values as the count for each subgroup created based on the table column values or expressions.

Examples to Implement MySQL GROUP BY Count

Let us demonstrate some examples using the GROUP BY clause and COUNT() function in MySQL to show the working of GROUP BY Count:

Example #1: Using GROUP BY Count on a single column

Step 1: Suppose we have a Customer table in the database having the fields CustomerID, CustomerName, Credit_Limit, and City, where we have some records as follows:

Code:

SELECT * FROM Customer;

Output:

MySQL GROUP BY count1

Step 2: Now, we will implement the MySQL GROUP BY COUNT to query the data forming groups based on the particular column value, and each group has its count number for the identical values found in the group. The SQL statement to perform this information in the result set rows with the following command.

Code:

SELECT CustomerName, City, COUNT(*) FROM Customer GROUP BY City;

Output:

MySQL GROUP BY count2

Explanation: You can observe that the result rows group together based on the city column, and each group displays the count for that specific value. The rows are then arranged accordingly. Thus, MySQL has returned a number of customers available in the table for each grouped city name. The group operation performed on the Customer table has displayed the reduced form of output rows due to the grouping of column values.

Example #2: Using GROUP BY Count on Multiple columns

Step 1: Let us take another table named Books present in our MySQL database. The table contains fields such as BookID, BookName, Language, and Price. To view the table’s contents, we will execute the below query.

Code:

SELECT * FROM Books;

Output:

MySQL GROUP BY count3

Step 2: Here, we have a language column with values of either ‘English’ or ‘Hindi’; similarly, we can find book names per English medium and Hindi version. Then, we will write a query to perform the grouping of columns in the table and implement the count function. The SQL statement is as follows:

Code:

SELECT BookName, Language, COUNT(*) FROM Books GROUP BY BookName, Language;

Output:

grouping of columns

Explanation: As you can see, the result rows are grouped based on multiple columns, BookName and Language, using the GROUP BY operation, and the next column is for the count that denotes values of books available for each group data in the table.

Example #3: Using GROUP BY Count with ORDER BY clause

For this illustration of an example, we will take the previous table named Books. In this query, along with the GROUP BY COUNT clause, we will also use the ORDER BY clause to evaluate the statement more appropriately. The ORDER BY clause in MySQL is responsible for arranging the column values, irrespective of their data type, either character or numeric, in ascending or descending order, as mentioned in the query part. We will apply it after the GROUP BY clause. The query statement for this is as follows:

Code:

SELECT LANGUAGE, COUNT(*) FROM Books GROUP BY LANGUAGE ORDER BY 2 ASC;

Output:

ORDER BY clause

Explanation: Each group has its respective counts, and the counting for each group is queried in ascending order, as shown above.

Conclusion

We use the COUNT() function in a MySQL query with the GROUP BY clause to illustrate table records based on multiple groupings. This GROUP BY COUNT query provides the count of values for the combination of identical column values preserved as a single group.

Recommended Articles

We hope that this EDUCBA information on “MySQL GROUP BY Count” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. MySQL count()
  2. ROLLUP in MySQL
  3. MySQL Subquery
  4. MySQL avg()
MICROSOFT POWER BI Training
48+ Hours of HD Videos
8 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
CYBER SECURITY & ETHICAL HACKING Certification Course
89+ Hours of HD Videos
13 Courses
3 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
MICROSOFT AZURE
97+ Hours of HD Videos
15 Courses
12 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
KALI LINUX Certification Course
26+ Hours of HD Videos
6 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Popular Course in this category
MYSQL Certification Course
 115+ Hours of HD Videos
18 Courses
3 Mock Tests & Quizzes
  Verifiable Certificate of Completion
  Lifetime Access
4.5
Price

View Course
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more