SQL Cookbook Lesson

How to drop a table in SQL

Learn how to drop a table in SQL with examples and explanations from SQLPad.

Problem

You have a table in your SQL database that you no longer need and you want to drop it permanently.

Sample Data

For this scenario, we don't need sample data because our aim is to drop a table from the database.

MySQL Solution

DROP TABLE IF EXISTS TableName;

PostgreSQL Solution

DROP TABLE IF EXISTS TableName;

Explanation

MySQL Explanation

In MySQL, you can use the DROP TABLE command to delete a table from the database. The IF EXISTS clause is optional and it is used to prevent an error from occurring if the table does not exist.

Replace 'TableName' with the name of the table you want to drop.

Please note that this operation is permanent and you won't be able to retrieve the data after deletion.

PostgreSQL Explanation

In PostgreSQL, the command to drop a table is identical to the command in MySQL.

The DROP TABLE command is used to remove a table definition and all associated data, indexes, rules, triggers, and constraints for that table.

Again, you need to replace 'TableName' with the actual name of the table you want to remove, and be aware that this operation cannot be undone. Be sure to back up any important data before executing this command.

Keep Practicing

Related Cookbook Lessons

View all lessons

Interview Prep

Begin Your SQL, Python, and R Journey

Master 230 interview-style coding questions and build the data skills needed for analyst, scientist, and engineering roles.