SQL DROP TABLE IF EXISTS

SQL DROP TABLE IF EXISTS statement is used to drop or delete a table from a database, if the table exists. If the table does not exist, then the statement responds with a warning.

The table can be referenced by just the table name, or using schema name in which it is present, or also using the database in which the schema or table is present.

Syntax

The syntax of a SQL DROP TABLE IF EXISTS statement is

DROP TABLE IF EXISTS table_name;
DROP TABLE IF EXISTS schema_name.table_name;
DROP TABLE IF EXISTS database_name.table_name;
DROP TABLE IF EXISTS database_name.schema_name.table_name;

where

ArgumentDescription
table_nameName of the table which has to be dropped.
schema_nameName of the schema in which the table exists.
database_nameName of the database in which the specified schema or table exists.
ADVERTISEMENT

DROP TABLE IF EXISTS

SQL DROP TABLE IF EXISTS Query to drop the table table_name is

DROP TABLE IF EXISTS table_name;

Examples

DROP TABLE IF EXISTS baseball_players;
DROP TABLE IF EXISTS employees;

DROP TABLE IF EXISTS from Specified Schema

SQL DROP TABLE IF EXISTS Query to drop the table table_name from the schema schema_name is

DROP TABLE IF EXISTS schema_name.table_name;

Examples

DROP TABLE IF EXISTS athletes.baseball_players;
DROP TABLE IF EXISTS shipping.employees;

DROP TABLE IF EXISTS from Specified Database

SQL DROP TABLE IF EXISTS Query to drop the table table_name from the schema schema_name is

DROP TABLE IF EXISTS database_name.table_name;

Examples

DROP TABLE IF EXISTS school.baseball_players;
DROP TABLE IF EXISTS organization.employees;

DROP TABLE IF EXISTS from Specified Schema in Specific Database

SQL DROP TABLE IF EXISTS Query to drop the table table_name from the schema schema_name present in the database database_name is

DROP TABLE IF EXISTS database_name.schema_name.table_name;

Examples

DROP TABLE IF EXISTS school.athletes.baseball_players;
DROP TABLE IF EXISTS organization.shipping.employees;

Examples

Positive Scenario

In this example, we try to delete a table students from the database school. The database and the table in it are present. We execute the DELETE TABLE IF EXISTS query/statement and observe the result.

SQL Statement

DROP TABLE IF EXISTS school.students;

Response

0 row(s) affected

Negative Scenario

In this example, we try to delete a table nothing from the database school. The specified table is not present in the database. We execute the DELETE TABLE IF EXISTS query/statement and observe the result.

Since, the table is not present, the query responds with a warning.

SQL Statement

DROP TABLE IF EXISTS school.nothing;

Response

0 row(s) affected, 1 warning(s): 1051 Unknown table 'school.students'