intro
Let's explore different SQL check if table exists methods in MySQL, PostgreSQL, and SQL Server, so you can master the art of table existence verification.
Before running a query on a table, you should first check whether it exists. This is vital in SQL scripts, stored procedures, and custom functions to avoid errors and undesired results or interruptions. Fortunately, there are several ways to check if a table exists using the SQL language, and in this guide we will explore the most practical methods!
Let’s dive in!
Why Check if a Table Exists in SQL?
Checking if a table exists in SQL guarantees smooth database queries. By verifying a table's existence before running a specific operation on it, you can avoid unnecessary failures and avoid data integrity issues.
In particular, below are the main reasons for checking if a table exists:
SQL Check If Table Exists: Possible Approaches
Since each database manages tables differently, there is not a universal SQL check if table exists approach. Thus, let’s explore the common ways to achieve that goal in the most popular databases.
Note: The following queries will be executed in DbVisualizer, a powerful, user-friendly, feature-rich database client that supports over 50 databases and offers advanced management tools.
MySQL
MySQL provides the SHOW TABLES
query, which returns a list of tables in a database. You can use it to check if a table exists with:
1
SHOW TABLES LIKE 'your_table';
If the table does not exist, this query returns an empty result set. Otherwise, if it does exist, it returns a single row with the table name:

Alternatively, you can query INFORMATION_SCHEMA.TABLES
, which contains metadata about database tables:
1
SELECT COUNT(*)
2
FROM INFORMATION_SCHEMA.TABLES
3
WHERE TABLE_SCHEMA = 'your_database' AND TABLE_NAME = 'your_table';
Or equivalently:
1
SELECT EXISTS (
2
SELECT 1 FROM INFORMATION_SCHEMA.TABLES
3
WHERE TABLE_SCHEMA = 'your_database'
4
AND TABLE_NAME = 'your_table'
5
);
The above two queries return 1
if the table exists, 0
otherwise:

SQL Server
In SQL Server, you can check if a table exists using the system catalog views as below:
1
SELECT COUNT(*)
2
FROM INFORMATION_SCHEMA.TABLES
3
WHERE TABLE_SCHEMA = 'dbo' AND TABLE_NAME = 'your_table';
This returns 1
if the table exists or 0
otherwise.
An alternative approach is to query the sys.objects
system catalog:
1
SELECT CASE
2
WHEN EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('dbo.your_table') AND type = 'U')
3
THEN 1 ELSE 0
4
END;
Here, type = 'U'
ensures that the query checks for user-defined tables (as opposed to system tables, views, or other object types). The SQL CASE
statement is used to make the query return 1
if the table exists and 0
otherwise.
Note that OBJECT_ID()
is a special SQL Server function that returns the database object identification number of a schema-scoped object.
The result will be as follows:

PostgreSQL
PostgreSQL offers several ways to check if a table exists. One approach is using the pg_tables
system catalog, which provides access to useful information about each table in the database:
1
SELECT COUNT(*)
2
FROM pg_tables
3
WHERE schemaname = 'public' AND tablename = 'your_table';
The result will be 1
if the table exists or 0
if it does not.
Alternatively, you can use the EXISTS
clause for a more concise check:
1
SELECT EXISTS (
2
SELECT 1 FROM pg_tables
3
WHERE schemaname = 'public'
4
AND tablename = 'your_table'
5
);
In this case, the result will be true
if the table exists; false
will be returned otherwise:

Another option is querying the information_schema.tables
view:
1
SELECT COUNT(*)
2
FROM information_schema.tables
3
WHERE table_schema = 'public'
4
AND table_name = 'your_table';
Or, you can run a query as follows:
1
SELECT EXISTS (
2
SELECT 1 FROM information_schema.tables
3
WHERE table_schema = 'public'
4
AND table_name = 'your_table'
5
);
Using a Visual Database Client
The above methods work well for programmatic SQL table existence checks, making them ideal for stored procedures or SQL scripts. However, a more intuitive approach is to use a visual database client, which provides immediate feedback on whether a table exists.
For example, in DbVisualizer, you can check if a table exists by:

Et voilà! With DbVisualizer, visually checking table existence has never been easier.
Conclusion
In this article, you explored several SQL methods to check if a table exists, understanding how they work and the results they produce. While these queries are effective for programmatic table existence checks, using a visual database client like DbVisualizer makes the task as simple as visually taking a look at the tables in a database. Download it for free today!
FAQ
How to check if temporary table exists in SQL Server?
In SQL Server, you can check if a temporary table exists using the following query:
1
SELECT CASE
2
WHEN OBJECT_ID('tempdb..#your_temp_table')
3
IS NOT NULL THEN 1
4
ELSE 0
5
END;
This query checks the tempdb
system database for the existence of the temporary table #your_temp_table
. It returns 1
if the temporary table exists, or 0
if it does not.
In SQL Server, the double dot (..
) syntax is used to specify the schema and the object in a database. When checking for the existence of a temporary table, the temporary table is stored in the tempdb
system database, and the table itself doesn’t have a schema like regular tables. Therefore, you need to use tempdb..#your_temp_table
to reference the temporary table, where:
What is the most common approach to checking if a table exists in SQL Server?
The most common and efficient approach to check if a table exists in SQL Server is by using the OBJECT_ID()
function. The query checks if the table's object ID is not NULL
, indicating its existence. Here is its syntax:
1
IF OBJECT_ID('dbo.table_name', 'U') IS NOT NULL
2
-- The table exists
In this context, 'U'
can be specified to refer to a user table. If the table exists, OBJECT_ID
returns its object ID; otherwise, it returns NULL
.
How can you list tables in SQL?
There are several approaches to listing tables in SQL. For more information, refer to the following guides:
What is the most common check if table exists SQL procedure?
The most common way to check if a table exists in SQL is by querying system catalog views or tables, which store metadata about database objects. These system catalogs provide a fast and reliable method for checking table existence without requiring full table scans, making it suitable for use in stored procedures and scripts.
Why use a visual database client?
Using a visual database client like DbVisualizer makes checking for table existence easy with visual feedback in its intuitive, modern UI. Thanks to it, even non-technical users can verify if a table exists in a database. Additionally, DbVisualizer offers powerful features like drag-and-drop query creation and query optimization. Try all the features of the Pro version with a 21-day free trial!