Easiest way to get number of rows in a MySQL table?


The easiest way to get number of rows, use aggregate function COUNT(*). Let us first create a table −

mysql> create table DemoTable1575
   -> (
   -> FirstName varchar(20)
   -> );
Query OK, 0 rows affected (1.00 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1575 values('Chris');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable1575 values('Bob');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable1575 values('Adam');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable1575 values('Robert');
Query OK, 1 row affected (0.15 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1575;

This will produce the following output −

+-----------+
| FirstName |
+-----------+
| Chris     |
| Bob       |
| Adam      |
| Robert    |
+-----------+
4 rows in set (0.00 sec)

Here is how to get the number of rows −

mysql> select count(*) from DemoTable1575;

This will produce the following output −

+----------+
| count(*) |
+----------+
|        4 |
+----------+
1 row in set (0.03 sec)

Updated on: 16-Dec-2019

124 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements