How can we know the repetition of a value in column with the help of group function COUNT(*) and GROUP BY clause?


We can use COUNT(*) and GROUP BY clause to find out the repetition of a value in the column. Following is the example, using COUNT(*) and GROUP BY clause on ‘Name’ column of table ‘Student’, to demonstrate it −

mysql> select count(*),name from student group by name;

+----------+---------+
| count(*) | name    |
+----------+---------+
| 1        | Aarav   |
| 2        | Gaurav  |
| 1        | Harshit |
+----------+---------+

3 rows in set (0.00 sec)

The result set of above query shows that which value is repeated and for how much times in the column. In the above example, ‘Gaurav’ is repeated two times in the column ‘name’. The same can be verified from the ‘SELECT *’ query below −

mysql> Select * from Student;

+------+---------+---------+-----------+
| Id   | Name    | Address | Subject   |
+------+---------+---------+-----------+
| 1    | Gaurav  | Delhi   | Computers |
| 2    | Aarav   | Mumbai  | History   |
| 15   | Harshit | Delhi   | Commerce  |
| 20   | Gaurav  | Jaipur  | Computers |
+------+---------+---------+-----------+

4 rows in set (0.00 sec)

Updated on: 30-Jan-2020

67 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements