What is DROP TABLE?
The DROP TABLE statement is used to permanently remove an
existing table from a database. When a table is dropped, the table
structure and all data stored inside it are deleted.
Unlike DELETE, which removes rows while keeping the table,
DROP TABLE removes the entire table itself.
Basic DROP TABLE Syntax
The basic syntax is:
DROP TABLE table_name;
The statement above removes the specified table and all of its stored records.
Dropping a Simple Table
The following example creates a table, adds some records, displays the records, and then removes the table. It is fully compatible with SQLite.
DROP TABLE IF EXISTS workshops;
CREATE TABLE workshops (
workshop_id INTEGER PRIMARY KEY,
workshop_name TEXT,
seats INTEGER
);
INSERT INTO workshops VALUES
(1, 'Robotics Basics', 25),
(2, '3D Printing', 18),
(3, 'CAD Design', 30);
SELECT *
FROM workshops
ORDER BY workshop_id;
DROP TABLE workshops;
DROP TABLE IF EXISTS
The IF EXISTS option prevents an error when the specified
table does not exist. This is especially useful when running setup or
reset scripts multiple times.
DROP TABLE IF EXISTS temporary_notes;
CREATE TABLE temporary_notes (
note_id INTEGER PRIMARY KEY,
note_text TEXT
);
INSERT INTO temporary_notes (note_text) VALUES
('Review SQL joins'),
('Practice GROUP BY'),
('Learn database indexes');
SELECT *
FROM temporary_notes
ORDER BY note_id;
DROP TABLE IF EXISTS temporary_notes;
DROP TABLE Removes the Entire Table
Once a table is dropped, both its structure and its records are removed. The following example demonstrates this behavior.
DROP TABLE IF EXISTS books;
CREATE TABLE books (
book_id INTEGER PRIMARY KEY,
title TEXT,
author TEXT
);
INSERT INTO books VALUES
(1, 'Database Essentials', 'Maya Rao'),
(2, 'SQL in Practice', 'Arjun Mehta');
SELECT *
FROM books;
DROP TABLE books;
SELECT name
FROM sqlite_master
WHERE type = 'table'
AND name = 'books';
The final query returns no rows because the books table no
longer exists.
Dropping Multiple Tables
Multiple tables can be removed by using a separate
DROP TABLE statement for each table.
DROP TABLE IF EXISTS courses;
DROP TABLE IF EXISTS instructors;
CREATE TABLE courses (
course_id INTEGER PRIMARY KEY,
course_name TEXT
);
CREATE TABLE instructors (
instructor_id INTEGER PRIMARY KEY,
instructor_name TEXT
);
INSERT INTO courses VALUES
(1, 'Data Science'),
(2, 'Mechanical Design');
INSERT INTO instructors VALUES
(1, 'Neha'),
(2, 'Vikram');
SELECT *
FROM courses
ORDER BY course_id;
SELECT *
FROM instructors
ORDER BY instructor_id;
DROP TABLE courses;
DROP TABLE instructors;
DROP TABLE with a Related Foreign Key
When tables are related through foreign keys, the order in which tables are removed can matter. In SQLite, the dependent table should generally be dropped before the table it references.
PRAGMA foreign_keys = ON;
DROP TABLE IF EXISTS order_items;
DROP TABLE IF EXISTS products;
CREATE TABLE products (
product_id INTEGER PRIMARY KEY,
product_name TEXT NOT NULL
);
CREATE TABLE order_items (
item_id INTEGER PRIMARY KEY,
product_id INTEGER NOT NULL,
quantity INTEGER,
FOREIGN KEY (product_id)
REFERENCES products(product_id)
);
INSERT INTO products VALUES
(1, 'Mechanical Keyboard'),
(2, 'USB-C Adapter');
INSERT INTO order_items VALUES
(101, 1, 2),
(102, 2, 3);
SELECT
order_items.item_id,
products.product_name,
order_items.quantity
FROM order_items
JOIN products
ON order_items.product_id = products.product_id
ORDER BY order_items.item_id;
DROP TABLE order_items;
DROP TABLE products;
Checking Whether a Table Exists
SQLite stores information about database objects in
sqlite_master. You can use it to check whether a table
exists before dropping it.
DROP TABLE IF EXISTS inventory;
CREATE TABLE inventory (
item_id INTEGER PRIMARY KEY,
item_name TEXT,
quantity INTEGER
);
INSERT INTO inventory VALUES
(1, 'Steel Bolts', 120),
(2, 'Copper Wire', 75),
(3, 'Aluminum Sheets', 40);
SELECT name
FROM sqlite_master
WHERE type = 'table'
AND name = 'inventory';
DROP TABLE inventory;
DROP TABLE vs DELETE vs TRUNCATE
These commands have different purposes. DROP TABLE removes
the entire table, while DELETE removes rows but keeps the
table structure.
| Command | What It Does | Table Remains? |
|---|---|---|
| DROP TABLE | Removes the table and its data | No |
| DELETE | Removes selected rows | Yes |
| TRUNCATE | Removes all rows in systems that support it | Yes |
SQLite note: SQLite supports
DROP TABLE and DELETE, but it does not provide
a standalone TRUNCATE TABLE statement.
Common DROP TABLE Options
| Syntax | Purpose | SQLite |
|---|---|---|
| DROP TABLE table_name; | Drops an existing table | Supported |
| DROP TABLE IF EXISTS table_name; | Drops table only if it exists | Supported |
Important Points
DROP TABLEremoves an entire table from the database.- Both the table structure and its stored data are removed.
DROP TABLE IF EXISTSavoids an error when the table is missing.- Dropping a table is different from deleting individual rows.
- Related tables with foreign keys may need to be dropped in the correct order.
- Important data should be backed up before using destructive commands.
Best Practices
- Verify the table name before executing
DROP TABLE. - Use
IF EXISTSwhen appropriate. - Back up important data before dropping production tables.
- Use
DELETEinstead when you only need to remove rows. - Check foreign-key relationships before dropping related tables.
- Avoid destructive commands on important databases without testing them first.
The DROP TABLE statement permanently removes a table and its data from a database. Use it carefully, especially when working with production data or tables that are referenced by other database objects.
🧪 Test Your SQL Code
Edit the SQL code on the left and click “Run Code” to see the result on the right.
Click “Run Code” to see the result here.