MYSQL COUNT function is used to return the total number of rows present in any database table. Count function is mainly used where we want to fetch the total number of records in any of the given table.

Syntax for MYSQL Count() Function is

SELECT COUNT(column_name) FROM table_name WHERE [condition];

Let us take a deeper look at count function with the help of an example

Consider table student_list which contains the list of students

+-----+------------------+--------+-----------+
| sid | name | year | hostel_id |
+-----+------------------+--------+-----------+
|   1 | Ankur Rastogi | First | 4 |
|   2 | Amit Kumar | Second | 8 |
|   3 | Ram Kumar | Second | 8 |
|   4 | Shiv Saxena | First | 6 |
|   5 | Aman Aggarwal | Third | 3 |
|   6 | Nipun Gupta | Fourth | 2 |
|   7 | Arpit Gupta | Third | 1 |
|   8 | Shantanu Rastogi | Second | 3 |
|   9 | Ramesh Kumar | Third | 1 |
|  10 | Neelima Dhingra | Second | NULL |
|  11 | Anu Sibbal | Third | NULL |
+-----+------------------+--------+-----------+

Since there is only one entry for each student in this table so we can find the total number of users in second year with the help of COUNT function. MYSQL query for that will be

SELECT COUNT(*) FROM student_list WHERE year = 'SECOND';
+----------+
| COUNT(*) |
+----------+
|        4 |
+----------+


This article is contributed by Ankur Rastogi on Sat Jun 04 2016 16:51:16 GMT+0530 (IST)

If you want to add more comments to the article or you see any thing incorrect please write a comment below and we will surely get back to you.