What Is the Difference Between CHAR and VARCHAR?
Learn the difference between SQL CHAR and VARCHAR data types, how padding and storage work, and when to choose each type for your schema design.
Expected Interview Answer
CHAR is a fixed-length character type that always reserves and stores the declared number of characters, padding shorter values with spaces, while VARCHAR is a variable-length type that stores only the actual characters used plus a small length prefix, so its storage size shrinks and grows with the data.
Because CHAR(n) always occupies n characters on disk regardless of actual content length, it can waste space on short values but avoids storing a length overhead and can be marginally faster for fixed-width data like country codes or status flags. VARCHAR(n) instead stores a length indicator (typically 1-2 bytes) plus the actual string, so a VARCHAR(255) column holding a 3-character value only consumes space for those 3 characters plus the overhead — no wasted padding. Trailing spaces behave differently too: CHAR values are typically padded and comparisons may ignore trailing spaces, while VARCHAR preserves the exact string including any trailing spaces the application inserted. The practical rule of thumb is CHAR for genuinely fixed-width codes, VARCHAR for everything with naturally variable length like names or emails.
- CHAR is predictable and can be marginally faster for truly fixed-width data
- VARCHAR avoids wasting storage on short values within a wide column
- VARCHAR scales cleanly to naturally variable-length text like names
- Choosing correctly prevents unnecessary bloat across millions of rows
- Awareness of padding behavior avoids subtle string-comparison bugs
AI Mentor Explanation
CHAR is like every player's name plate on the dugout being cut to the exact same fixed length, so a short name like 'Dev' gets padded with blank space to fill the plate just like a longer name 'Bhuvaneshwar' would use the whole plate. VARCHAR is like using a label-maker that prints a plate exactly as long as each name needs, so 'Dev' gets a short plate and 'Bhuvaneshwar' gets a long one, with no.
Step-by-Step Explanation
Step 1
CHAR reserves a fixed width
CHAR(n) always occupies n characters on disk, padding shorter values with trailing spaces.
Step 2
VARCHAR stores only what's needed
VARCHAR(n) stores the actual string length plus a small overhead, growing/shrinking with the data.
Step 3
Padding affects comparisons
Many engines pad CHAR comparisons so trailing spaces are ignored, while VARCHAR preserves exact string content.
Step 4
Pick CHAR for genuinely fixed-width data
Use CHAR for codes like ISO country codes or fixed status flags where every value truly has the same length.
Step 5
Pick VARCHAR for naturally variable text
Use VARCHAR for names, emails, and free text where length legitimately varies row to row.
What Interviewer Expects
- Explains CHAR is fixed-length with padding, VARCHAR is variable-length
- Mentions the small length-prefix overhead VARCHAR stores
- Knows when CHAR is actually the better choice (fixed-width codes)
- Understands trailing-space comparison differences between the types
- Avoids blanket claims that one is always 'better' than the other
Common Mistakes
- Claiming VARCHAR is always superior with no tradeoffs
- Not knowing CHAR pads with spaces and how that affects comparisons
- Using CHAR for naturally variable-length data like names, wasting space
- Forgetting VARCHAR carries a small length-storage overhead
Best Answer (HR Friendly)
“CHAR always reserves the same fixed amount of space for a value, padding shorter entries with blanks, while VARCHAR only stores as much space as the actual text needs — so CHAR suits truly fixed-length codes, and VARCHAR suits naturally variable text like names.”
Code Example
CREATE TABLE countries (
code CHAR(2), -- always 2 chars, e.g. 'US', 'IN'
name VARCHAR(100) -- stores only as many chars as needed
);Follow-up Questions
- How does trailing-space padding affect equality comparisons on CHAR columns?
- What is the storage overhead of a VARCHAR column's length prefix?
- When would fixed-width CHAR actually improve performance?
- How do NCHAR and NVARCHAR differ from CHAR and VARCHAR?
- What happens if you insert a string longer than a CHAR or VARCHAR's declared length?
MCQ Practice
1. What happens when you store a short value in a CHAR(10) column?
CHAR always reserves the declared fixed width, padding shorter values with trailing spaces.
2. What does VARCHAR store in addition to the string itself?
VARCHAR stores the actual characters plus a small length indicator, typically 1-2 bytes.
3. When is CHAR generally the better choice over VARCHAR?
CHAR fits data that truly has a constant length, avoiding any real tradeoff versus VARCHAR there.
Flash Cards
CHAR(n) — Fixed-length; always stores n characters, padding with spaces.
VARCHAR(n) — Variable-length; stores only the actual characters plus a length prefix.
Best use for CHAR — Genuinely fixed-width codes, e.g. 2-letter country codes.
Best use for VARCHAR — Naturally variable-length text like names or emails.