How to get number of rows in a table without using count(*) MySQL query?


You can use count(1). Let us first see the syntax −

select count(1) from yourTableName;

Let us first create a table −

mysql> create table DemoTable
   (
   StudentName varchar(100)
   );
Query OK, 0 rows affected (0.84 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable(StudentName) values('John Smith');
Query OK, 1 row affected (0.24 sec)
mysql> insert into DemoTable(StudentName) values('Chris Brown');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable(StudentName) values('David Miller');
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable(StudentName) values('Carol Taylor');
Query OK, 1 row affected (0.15 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

Output

+--------------+
| StudentName  |
+--------------+
| John Smith   |
| Chris Brown  |
| David Miller |
| Carol Taylor |
+--------------+
4 rows in set (0.00 sec)

Following is the query to get number of rows in a table without using count(*) −

mysql> select count(1) from DemoTable;

Output

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

Updated on: 30-Jul-2019

549 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements