Query to count the number of tables I have in MySQL
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
If you want to count how many tables exist in a MySQL database, the standard solution is to query information_schema.tables. That system schema contains metadata about databases, tables, views, and other objects managed by the server.
The important detail is that information_schema.tables contains entries for every schema on the server, so your query must filter by the database name you care about.
Count Base Tables in One Database
The most common query is:
This counts only normal tables in the named schema.
Why include table_type = 'BASE TABLE'? Because information_schema.tables can also include views. If you want a pure table count, exclude them explicitly.
Count Tables in the Current Database
If you are already connected to the database and do not want to hard-code its name, use DATABASE().
That makes the query more reusable in scripts and admin sessions.
Include Views If That Matches Your Goal
Sometimes the real question is “how many table-like objects do I have?” In that case, remove the table_type filter.
That will count both base tables and views.
If you want to see the breakdown instead of a single number:
This is often the most informative version because it shows exactly what MySQL is counting.
List the Tables Too
A count is useful, but during troubleshooting you often want the actual names as well.
That lets you verify the count and spot unexpected objects.
Why Not Use SHOW TABLES?
SHOW TABLES is fine for interactive use, but it is less convenient when you want a count directly in SQL logic.
This prints a result set of names, which your client can count, but information_schema is better when you want filtering, grouping, joins, or scripting.
Performance and Permissions
For normal database sizes, this metadata query is lightweight. The more common issue is permissions. If a user sees fewer schemas or objects than an admin user, the count may differ because metadata visibility is permission-sensitive.
That is not a bug in the query. It reflects the database account’s visibility into server objects.
This is worth remembering in shared environments. A deployment script run by an application account may report a smaller table count than the same query run by a DBA account, even though both are connected to the same server.
Common Pitfalls
- Forgetting to filter by
table_schema, which counts objects from other databases too. - Counting views unintentionally because
information_schema.tablesincludes more than just base tables. - Hard-coding a schema name when
DATABASE()would make the query reusable. - Comparing counts from different users without considering metadata permissions.
- Using
SHOW TABLESwhen you actually need a SQL expression that can be filtered or grouped.
Summary
- Use
information_schema.tablesto count MySQL tables reliably. - Filter by
table_schemaso you only count objects in the target database. - Add
table_type = 'BASE TABLE'when you want tables but not views. - Use
DATABASE()if you want the current schema dynamically. - Querying metadata is usually better than
SHOW TABLESwhen you need a count in SQL.
Related reading
- Querying a Global Secondary Index in dynamodb Local
- Querying CompositeType columns in Cassandra using Hector
- Querying DynamoDB by date
- Querying DynamoDB without Primary Key
- Querying DynamoDB without PrimaryKey with Lambda
- Querying for greatest value of Range key on AWS DynamoDb
- Questions for reading data from JDBC source in DataStream Flink
- Questions while I'm making distributed key-value store

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.