What are generated columns in MySQL?
Why Interviewers Ask This
Advanced questions like this reveal whether a candidate has internalized MySQL / SQL deeply enough to make architectural decisions. Strong answers demonstrate both breadth and depth of experience.
Answer
Generated columns (also called computed or virtual columns, introduced in MySQL 5.7) are columns whose values are derived from an expression based on other columns in the same table, rather than stored directly. Two types: VIRTUAL (default): the value is computed on the fly each time it is read — no storage used (except for index entries); STORED: the computed value is calculated and stored on disk at insert/update time — takes storage but can be faster to read for expensive computations. Syntax: ALTER TABLE orders ADD COLUMN total DECIMAL(10,2) AS (quantity * unit_price) STORED;. You can create indexes on generated columns — extremely useful for computed values used in WHERE clauses: ALTER TABLE users ADD COLUMN email_domain VARCHAR(255) AS (SUBSTRING_INDEX(email, "@", -1)) STORED, ADD INDEX idx_email_domain (email_domain); — enables fast lookup by email domain without modifying the email column. Use cases: extracting JSON fields for indexing: ADD COLUMN city VARCHAR(100) AS (data->>"$.address.city") STORED, ADD INDEX (city);; denormalizing computed values for performance; creating function-based index equivalents. Generated columns cannot reference other generated columns or non-deterministic functions (NOW(), RAND()).
Common Mistake
Rushing to answer is a common mistake. Take two seconds to structure your response: definition → example → trade-off. This structure makes complex MySQL / SQL answers easy to follow.
Previous
What is ProxySQL and how does it help MySQL deployments?
Next
What is the difference between a function and a stored procedure in MySQL?
More MySQL / SQL Questions
View all →- Advanced What is the difference between B-tree and Hash indexes in MySQL?
- Advanced What is MVCC (Multi-Version Concurrency Control) in MySQL?
- Advanced What is the InnoDB Buffer Pool and how does it work?
- Advanced What is deadlock in MySQL and how do you prevent it?
- Advanced What is the query execution plan and how does the MySQL optimizer work?