ALTER Table — SQL Technical Interview Questions & Answers
ALTER Table
ALTER Table
- ALTER TABLE changes a table that already exists — add a column, rename, change type — without dropping all data. ALTER TABLE students ADD COLUMN email VARCHAR(80); adds email. Existing rows get NULL in that column unless you set a DEFAULT.
- ADD / DROP COLUMN, RENAME COLUMN, MODIFY type, ADD INDEX, ADD FOREIGN KEY — all are ALTER. MySQL 8 uses RENAME COLUMN old TO new. Older 5.7 often used CHANGE.
- Trap: MODIFY VARCHAR(10) when names are already 30 characters — those rows can fail or get cut. Trap: DROP COLUMN on a column the app still writes to.
- On the example next to this theory: ALTER TABLE changes structure without rebuilding the whole table.
Technical Interview Tip: ALTER TABLE students ADD COLUMN email VARCHAR(80);
Code Examples for ALTER Table
ALTER Table — sample query
CREATE TABLE items (id INTEGER PRIMARY KEY, title TEXT);
ALTER TABLE items ADD COLUMN price REAL DEFAULT 0;
INSERT INTO items (id, title, price) VALUES (1, 'Pen', 10);
SELECT * FROM items;ALTER TABLE changes structure without rebuilding the whole table.
How to Explain ALTER Table to an Interviewer
How do you add a column?
ALTER TABLE students ADD COLUMN email VARCHAR(80);