
You’re on-call for a fintech platform that processes ~20M card transactions/day and stores data in a PostgreSQL data warehouse used for fraud monitoring, chargeback investigations, and regulatory audits. A nightly ETL job loads data into a staging table (stg_authorizations) and then merges it into a curated fact table. Last week, an engineer tried to “clear the staging table” and accidentally broke downstream dashboards for the fraud team. The incident review found confusion between TRUNCATE, DELETE, and DROP, especially around rollback behavior, identity sequences, and dependencies.
Explain the difference between TRUNCATE, DELETE, and DROP in SQL. In your answer, address the following:
TRUNCATE ... CASCADE, trigger firing differences).TRUNCATE, when to use DELETE with a WHERE clause, and when DROP is appropriate.DELETE removes rows from an existing table and can target specific rows using a WHERE clause. It is typically fully logged and may fire DELETE triggers; it generally preserves table structure, indexes, and identity/sequence state (unless explicitly reset).
BEGIN;
DELETE FROM stg_authorizations
WHERE load_date < CURRENT_DATE - INTERVAL '7 days';
ROLLBACK;
TRUNCATE removes all rows from a table efficiently by deallocating data pages/extents rather than deleting rows one-by-one (implementation-dependent). It usually cannot filter with WHERE, may have different trigger semantics than DELETE, and often supports options like restarting identity counters.
BEGIN;
TRUNCATE TABLE stg_authorizations RESTART IDENTITY;
COMMIT;
DROP removes the table definition (metadata) and its data, invalidating dependent objects unless CASCADE is used (DB-specific). It’s appropriate for ephemeral tables or migrations, but dangerous in shared schemas because it breaks downstream queries and permissions.
DROP TABLE IF EXISTS stg_authorizations;
DELETE must respect foreign keys and can be blocked unless you delete child rows first or use ON DELETE CASCADE. TRUNCATE is often more restrictive with FKs (e.g., PostgreSQL requires truncating referencing tables together or using TRUNCATE ... CASCADE), and trigger firing differs by engine. DROP removes the table and therefore breaks dependencies entirely unless handled explicitly.
TRUNCATE TABLE parent_table CASCADE; -- PostgreSQL: also truncates referencing tables
In regulated environments, you choose commands based on audit requirements and blast radius. DELETE provides selective removal and clearer row-level semantics; TRUNCATE is fast for staging resets but should be guarded by least-privilege permissions; DROP should be limited to migration tooling and isolated schemas.