DROP Table — SQL Technical Interview Questions & Answers

DROP Table

DROP Table

  • DROP TABLE students; removes the table and its data. DROP TABLE IF EXISTS students; is the script form. Child tables with FOREIGN KEY to students must be dropped first or FKs removed.
  • Trap: DROP TABLE when you meant DELETE FROM. Trap: dropping in the wrong database.
  • On the example next to this theory: DROP Table: create two demo rows, then SELECT qty >= 2 ordered. Say which labels come back.

Technical Interview Tip: DROP TABLE IF EXISTS students; — structure and data both go.

Code Examples for DROP Table

DROP Table — sample query

-- DROP Table
CREATE TABLE demo (
  id INTEGER PRIMARY KEY,
  label TEXT NOT NULL,
  qty INTEGER DEFAULT 0
);
INSERT INTO demo (id, label, qty) VALUES
  (1, 'alpha', 2),
  (2, 'beta', 5);
SELECT label, qty FROM demo WHERE qty >= 2 ORDER BY qty DESC;

DROP Table: create two demo rows, then SELECT qty >= 2 ordered. Say which labels come back.

How to Explain DROP Table to an Interviewer

DROP TABLE vs DELETE?

DROP removes the table DELETE removes rows, table stays