
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Check how many rows are in a MySQL database table?
To know how many rows are in a ySQL database table, you need to use aggregate function COUNT(*).
The syntax is as follows
SELECT COUNT(*) FROM yourTableName;
To understand the above syntax, let us create a table. The query to create a table is as follows
mysql> create table CountRowsDemo - > ( - > Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, - > Name varchar(20) - > ); Query OK, 0 rows affected (0.78 sec)
Insert some records in the table using insert command.
The query is as follows
mysql> insert into CountRowsDemo(Name) values(NULL); Query OK, 1 row affected (0.15 sec) mysql> insert into CountRowsDemo(Name) values('Sam'); Query OK, 1 row affected (0.25 sec) mysql> insert into CountRowsDemo(Name) values(NULL); Query OK, 1 row affected (0.11 sec) mysql> insert into CountRowsDemo(Name) values('Mike'); Query OK, 1 row affected (0.21 sec) mysql> insert into CountRowsDemo(Name) values('David'); Query OK, 1 row affected (0.11 sec) mysql> insert into CountRowsDemo(Name) values(NULL); Query OK, 1 row affected (0.11 sec) mysql> insert into CountRowsDemo(Name) values(NULL); Query OK, 1 row affected (0.09 sec) mysql> insert into CountRowsDemo(Name) values('Carol'); Query OK, 1 row affected (0.17 sec)
Display all records from the table using select statement.
The query is as follows
mysql> select *from CountRowsDemo;
The following is the output
+----+-------+ | Id | Name | +----+-------+ | 1 | NULL | | 2 | Sam | | 3 | NULL | | 4 | Mike | | 5 | David | | 6 | NULL | | 7 | NULL | | 8 | Carol | +----+-------+ 8 rows in set (0.00 sec)
Now let us run the following query to count rows from a table
mysql> select count(*) AS TotalRows from CountRowsDemo;
The following is the output
+-----------+ | TotalRows | +-----------+ | 8 | +-----------+ 1 row in set (0.00 sec)
- Related Articles
- How to check an empty table already in a MySQL database?
- How to check table status of the tables in a particular MySQL database?
- How to check if a table already exists in the database with MySQL with INFORMATION_SCHEMA.TABLES.?
- Select two random rows in a MySQL database?
- How to alter the database engine of a MySQL database table?
- How many horizontal rows are there in the modern periodic table and what are they called?
- How to check if a MySQL database exists?
- How to retrieve table names from a database in MySQL?
- Are MySQL database and table names case-sensitive?
- How to replace rows in a MySQL table with conditions?
- MySQL select query to select rows from a table that are not in another table?
- Randomly SELECT distinct rows in a MySQL table?
- How can we create a table from an existing MySQL table in the database?
- Exact count of all rows in MySQL database?
- Count how many rows have the same value in MySQL?

Advertisements