advancedTop 30 Scenario-Based Questions

You need to add a NOT NULL column to a 500-million-row production table without downtime. How?

PostgreSQL 11+: ALTER TABLE t ADD COLUMN status VARCHAR(20) DEFAULT 'ACTIVE'. In PostgreSQL 11+, this writes the DEFAULT to catalog metadata only — no row rewrite, O(1). Then: ALTER TABLE t ALTER COLUMN status SET NOT NULL (fast if all rows have the default). For older versions or MySQL: 1) Add column as nullable with default. 2) Backfill in batches (UPDATE ... LIMIT 10000). 3) Add NOT NULL after

Ready to master this question?

Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.

Sign in to generate a response

Next Step

Continue to A query is taking 30 seconds. EXPLAIN ANALYZE shows a Seq Scan on a 100M-row table. How do you fix it?← Back to all SQL questions