When you inherit a database or start working on a new project, one of the first things you need to do is understand what tables exist inside it. Whether you are debugging an application, auditing data structures, or building a dynamic reporting tool, knowing how to run a SQL query to get all table names is an essential skill. Instead of manually clicking through a graphical interface, a well-written query lets you retrieve the complete list of tables programmatically, saving time and reducing the risk of overlooking important objects.
Why You Need to List All Table Names in a Database
Databases often grow organically over time. Developers add tables, administrators create temporary working tables, and legacy systems leave behind objects that are no longer documented. Without a clear inventory, it becomes difficult to:
- Understand the overall database schema
- Identify orphaned or unused tables
- Generate automated documentation
- Build tools that dynamically discover database objects
- Perform security audits on sensitive tables
Running a SQL query to get all table names gives you a reliable, repeatable way to inventory your database environment without depending on a specific database management tool.
How to Get All Table Names in MySQL
MySQL offers multiple approaches to retrieve table names, depending on whether you want a quick list or detailed metadata.
Using SHOW TABLES
The simplest method is the SHOW TABLES statement. It returns a single column containing the names of all tables in the currently selected database.
SHOW TABLES;
This command is easy to remember and works well for quick exploration. Even so, it provides limited information. If you need to filter by table type or schema, you will need a more dependable approach And that's really what it comes down to. That's the whole idea..
Querying INFORMATION_SCHEMA.TABLES
For greater control, query the information_schema.tables view. This is part of the SQL standard and works across many database systems, though the exact columns may vary slightly.
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'your_database_name';
You can also restrict the results to base tables only, excluding views:
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'your_database_name'
AND table_type = 'BASE TABLE';
How to Get All Table Names in PostgreSQL
PostgreSQL organizes objects into schemas, so your query must account for the schema context. The most common sources for table metadata are pg_tables and information_schema.tables Easy to understand, harder to ignore. Less friction, more output..
Using pg_tables
The pg_tables catalog view contains information about tables in the current database.
SELECT tablename
FROM pg_tables
WHERE schemaname = 'public';
Replace 'public' with the name of the schema you want to inspect. If you want tables from all schemas, omit the WHERE clause or adjust it accordingly That's the whole idea..
Using information_schema.tables
Like MySQL, PostgreSQL supports the standard information schema:
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'public'
AND table_type = 'BASE TABLE';
Using the psql Meta-Command
If you are working directly in the psql command-line client, you can use the backslash command:
\dt
This lists all tables in the current schema. To see tables across all schemas, use:
\dt *.*
How to Get All Table Names in SQL Server (T-SQL)
SQL Server stores metadata in system catalog views. Because of that, tables* and *information_schema. The two most common options are sys.tables.
Querying sys.tables
The sys.tables view returns a row for each user table in the current database.
SELECT name AS table_name
FROM sys.tables;
To include the schema name, join with sys.schemas:
SELECT s.name AS schema_name, t.name AS table_name
FROM sys.tables t
INNER JOIN sys.schemas s ON t.schema_id = s.schema_id;
Using INFORMATION_SCHEMA.TABLES
SQL Server also supports the standard information schema:
SELECT table_name
FROM information_schema.tables
WHERE table_type = 'BASE TABLE';
This approach is useful if you write portable code that must run on multiple database platforms.
How to Get All Table Names in Oracle
Oracle provides several data dictionary views for inspecting tables. The right one depends on your privileges and whether you want to see tables you own, tables you can access, or all tables in the database.
Querying ALL_TABLES and USER_TABLES
ALL_TABLES shows tables accessible to the current user:
SELECT table_name
FROM all_tables;
USER_TABLES shows only tables owned by the current user:
SELECT table_name
FROM user_tables;
Using DBA_TABLES
If you have DBA privileges, DBA_TABLES reveals every
table in the database:
SELECT table_name
FROM dba_tables;
Access to DBA_TABLES typically requires the DBA role or the SELECT ANY DICTIONARY system privilege.
Filtering by Schema (Owner)
To list tables owned by a specific schema, query the owner column:
SELECT table_name
FROM all_tables
WHERE owner = 'HR';
Replace 'HR' with the target schema name.
Conclusion
Retrieving table names is a routine task, but the method depends on your database platform and the level of detail you need Most people skip this — try not to..
- MySQL: Use
information_schema.tablesor theSHOW TABLESstatement. - PostgreSQL: Query
pg_tablesorinformation_schema.tables, or use the\dtmeta-command inpsql. - SQL Server: Use
sys.tables(often joined withsys.schemas) orinformation_schema.tables. - Oracle: Choose among
user_tables,all_tables, ordba_tablesbased on your privileges and scope.
For portable scripts that run across multiple platforms, information_schema.tables is the most consistent option. For platform-specific administration, native catalog
views provide the most comprehensive metadata, including internal IDs and storage details. By selecting the appropriate view based on your user permissions and the specific database engine, you can efficiently audit your schema and automate database documentation tasks Most people skip this — try not to. Still holds up..