CHAR vs VARCHAR
Understand the difference between CHAR and VARCHAR data types in SQL, storage behavior, padding, and when to use each.
Expected Interview Answer
CHAR is a fixed-length string type that always stores and pads to its declared length, while VARCHAR is a variable-length type that stores only the actual characters used plus a small length overhead.
A CHAR(10) column always occupies 10 bytes of storage per row, padding shorter values with trailing spaces, which makes it fast and predictable for fixed-format data like country codes or fixed-length IDs. A VARCHAR(10) column stores only as many bytes as the actual string needs, plus one or two bytes to record its length, which saves space for variable-length text like names or emails but adds a small overhead when reading the length prefix. The choice comes down to whether the data has a genuinely fixed length or varies significantly in size.
- CHAR gives predictable, fast fixed-width storage
- VARCHAR saves space for variable-length text
- CHAR suits fixed codes like ISO country or currency codes
- VARCHAR suits names, emails, and free text fields
AI Mentor Explanation
CHAR is like a scoreboard slot always reserved for exactly three characters per team code, padding shorter codes like "IND" with nothing needed but padding "SA" out to fill the slot with blanks. VARCHAR is like a commentary caption box that only takes up as much width as the actual team name needs, "IND" using three characters and "Zimbabwe" using eight, with just a small marker recording how long each one is. CHAR wastes a little space on short codes for the sake of fixed, predictable layout; VARCHAR trades that predictability for space efficiency.
Step-by-Step Explanation
Step 1
Assess data length variability
Determine if the values are always the same length or vary significantly.
Step 2
Choose CHAR for fixed-length data
Use CHAR(n) for codes like ISO country or currency codes.
Step 3
Choose VARCHAR for variable data
Use VARCHAR(n) for names, emails, and free text of varying length.
Step 4
Consider storage and performance
CHAR offers predictable row size; VARCHAR saves space at a small read overhead.
What Interviewer Expects
- Clear explanation of fixed vs variable length storage
- Understanding of padding behavior in CHAR
- Awareness of the length-prefix overhead in VARCHAR
- Ability to give appropriate use-case examples for each
Common Mistakes
- Thinking VARCHAR is always strictly better than CHAR
- Forgetting CHAR pads values with trailing spaces
- Not knowing CHAR can be faster for truly fixed-length data
- Confusing CHAR/VARCHAR length limits with byte size directly for multi-byte encodings
Best Answer (HR Friendly)
โCHAR always reserves a fixed amount of storage and pads shorter values with spaces, while VARCHAR only stores as many characters as are actually used, plus a small length marker. I use CHAR for genuinely fixed-length data like country codes, and VARCHAR for anything that varies in length, like names or emails.โ
Code Example
CREATE TABLE Countries (
CountryCode CHAR(3), -- always exactly 3 chars, e.g. 'IND'
CountryName VARCHAR(100) -- stores only as many chars as needed
);Follow-up Questions
- How does CHAR padding affect string comparison results?
- What is the storage overhead of VARCHAR versus CHAR?
- When would fixed-length CHAR actually improve query performance?
- How do CHAR and VARCHAR differ across MySQL and PostgreSQL implementations?
MCQ Practice
1. A CHAR(10) column storing "AB" occupies how much space?
CHAR always reserves its fixed declared length, padding shorter values with trailing spaces.
2. VARCHAR is best suited for which kind of data?
VARCHAR stores only the actual characters used, making it ideal for variable-length text.
3. What extra overhead does VARCHAR typically add compared to CHAR?
VARCHAR stores a small length prefix (typically 1-2 bytes) to record the actual string length.
Flash Cards
CHAR(n) โ Fixed-length storage, always n characters, padded with spaces.
VARCHAR(n) โ Variable-length storage, up to n characters, plus a length prefix.
When to use CHAR โ Fixed-format codes like country or currency codes.
When to use VARCHAR โ Names, emails, and other variable-length free text.