ENUM — SQL Technical Interview Questions & Answers
ENUM
ENUM
- ENUM('pass','fail') allows only those strings. Compact. Changing the list later is ALTER TABLE pain. A lookup table (grades) is more flexible. Don’t ENUM a list that product will grow weekly.
- Empty string / invalid values can become '' or error depending on sql_mode. Strict mode is your friend.
- Trap: ENUM as a substitute for a real table of 50 statuses.
- On the example next to this theory: ENUM: create two demo rows, then SELECT qty >= 2 ordered. Say which labels come back.
Technical Interview Tip: status ENUM('pass','fail') — or a status table if it grows.
Code Examples for ENUM
ENUM — sample query
-- ENUM
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;ENUM: create two demo rows, then SELECT qty >= 2 ordered. Say which labels come back.
How to Explain ENUM to an Interviewer
When not ENUM?
List that changes often Use a lookup table