sql to list all tables in a database is a fundamental skill for developers, data analysts, and database administrators. Whether you're working with MySQL, PostgreSQL, SQL Server, or Oracle, knowing how to retrieve a complete list of tables within a given database is essential for schema exploration, debugging, documentation, and performance tuning. This article provides a comprehensive, practical guide to achieving this across the most widely used relational database systems, along with best practices and troubleshooting tips.
Introduction
Every relational database management system (RDBMS) stores metadata about its structure, including tables, views, indexes, and users. When the need arises to list all tables, the approach varies slightly depending on the database engine. The SQL standard provides a common pathway through information schema views, but proprietary extensions and syntax differences mean that a one-size-fits-all query rarely works across platforms. This guide walks you through the most reliable methods for each major system, ensuring you can adapt quickly regardless of your environment.
Steps to List All Tables in SQL
The most portable and standards-compliant way to list tables is by querying the information_schema.tables view. This virtual schema is supported by MySQL, PostgreSQL, SQL Server, and Oracle (with minor variations), making it the go-to starting point for most users.
MySQL and MariaDB
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'your_database_name'
AND table_type = 'BASE TABLE';
This query filters out views by specifying table_type = 'BASE TABLE', returning only actual tables. Replace 'your_database_name' with the name of the database you're inspecting. If you omit the schema filter, the query returns tables from all databases you have access to, which can be useful for multi-tenant inspections And it works..
PostgreSQL PostgreSQL also supports the information_schema, but it often requires connecting to the specific database first. Once connected:
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'public'
AND table_type = 'BASE TABLE';
Note that PostgreSQL defaults to the public schema for new tables, but you can adjust the table_schema filter to inspect other
schemas such as information_schema or custom application schemas. For example:
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'your_schema_name'
AND
table_type = 'BASE TABLE';
PostgreSQL users can also use the dedicated catalog views or the psql meta-command \dt, which lists tables in the current search path quickly without writing SQL.
SQL Server
In SQL Server, information_schema.tables works as expected, but you may want to include the schema name for clarity:
SELECT table_schema, table_name
FROM information_schema.tables
WHERE table_type = 'BASE TABLE'
ORDER BY table_schema, table_name;
SQL Server users can also query sys.tables directly, which returns database-scoped table metadata:
SELECT name AS table_name
FROM sys.tables
ORDER BY name;
Alternatively, the stored procedure sp_tables provides a familiar, report-friendly listing:
EXEC sp_tables;
Oracle
Oracle's support for information_schema is limited compared to other engines, so it's more reliable to query the data dictionary views. The user_tables view lists tables owned by the current user:
SELECT table_name
FROM user_tables
ORDER BY table_name;
To see all tables accessible to your account, use all_tables, and for administrative access, dba_tables (requires elevated privileges):
SELECT owner, table_name
FROM all_tables
ORDER BY owner, table_name;
Filtering and Refining Your Results
Real-world databases often contain thousands of objects, so filtering is essential. To search for tables by name pattern, use the LIKE operator:
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'public'
AND table_name LIKE 'user%'
AND table_type = 'BASE TABLE';
To include row counts and size estimates for performance analysis, join against additional metadata. In MySQL, for example:
SELECT table_name, table_rows,
ROUND(data_length / 1024 / 1024, 2) AS data_mb
FROM information_schema.tables
WHERE table_schema = 'your_database_name'
ORDER BY data_length DESC;
Best Practices
A few habits will make your schema exploration safer and more efficient:
- Always filter by
table_schemaor database name to avoid scanning metadata you don't need. - Explicitly filter
table_type = 'BASE TABLE'when you only want real tables, since views and system objects can otherwise clutter results. - Order your results (
ORDER BY table_name) so output is predictable and easy to scan or diff. - Use the built-in meta-commands where available (
\dtin psql,sp_tablesin SQL Server) for quick, ad-hoc inspection. - Confirm your current database or schema context before running a query, especially in PostgreSQL where the connection target matters.
Troubleshooting
If a query returns no rows, first verify you're connected to the correct database and that the schema name matches exactly (identifiers are case-sensitive in some engines). Permission issues commonly cause empty or partial results; the dba_tables view in Oracle, for instance, requires administrative rights. On systems with many schemas, remember that omitting the schema filter can return an overwhelming list, so narrow your scope early. Finally, be aware that some managed or serverless databases hide system schemas entirely, so rely on the documented dictionary views for that platform.
Conclusion
Listing all tables in a database is a foundational task that, once understood, transfers easily across platforms. Although MySQL, PostgreSQL, SQL Server, and Oracle each have their own quirks, the underlying principle is consistent: every engine exposes its structure through metadata, and you simply need to query the right source. Start with information_schema.tables for portability, fall back to proprietary catalog views like sys.tables or user_tables when needed, and refine your results with filters and ordering. With these techniques in hand, you can confidently explore, document, and troubleshoot the structure of virtually any relational database you encounter It's one of those things that adds up..