MySQL Tutorial
The COUNT() function returns the number of rows in a query.
The COUNT() function is used with MySQL SELECT statement and it is very useful to count the number of rows in a table having enormous data.
SELECT COUNT (expression)
FROM tables
WHERE conditions;
Let's put these statements into real use.
We've a table named Students in our database that contains the following records:
ROLL_NO | NAME | SUBJECT |
---|---|---|
1 | Will | JAVA |
2 | SAM | Python |
3 | Sara | HTML | 4 | Rim | Java |
5 | Micheal | SQL |
6 | Lara | JAVA |
Now lets Count the number of record from Students Table.
SELECT Count(ROLL_NO)
FROM Students;
The above SELECT statement Counts the number of ROLL_NO.
Count(ROLL_NO) |
---|
6 |
We can count number of rows with where condition.
SELECT Count(ROLL_NO)
FROM Students
WHERE SUBJECT='JAVA'
The above SELECT statement COUNT row number of SUBJECT is 'JAVA'.
Count(ROLL_NO) |
---|
3 |