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

MySQLMySQLi Database

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)
raja
Published on 19-Feb-2018 11:38:24
Advertisements