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 |
+----------+



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.



Trending Articles


Incorrect table definition there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause

Infix To Postfix Conversion Using Stack in Javascript

How to solve IE/Edge CORS error ?

Web Server vs Application Server Difference Explained

How to setup angular2 in visual studio ide

Binary Search Tree Insert node Implementation in Javascript

How to use ninject dependency injection in mvc

How to Add Comparison Feature in Replace in C-Sharp