Difference Between VARCHAR and CHAR?

VARCHAR and CHAR are both string column types, but they differ in how they store and pad data -- VARCHAR stores variable-length strings using only the space needed, while CHAR always reserves and pads to a fixed length.

Key Points: • VARCHAR(n) uses only as much storage as the actual string requires, plus 1-2 bytes to record the length. • CHAR(n) always consumes n bytes/characters, padding shorter values with trailing spaces. • CHAR can be marginally faster for fixed-length data like country codes because rows are predictable in size. • VARCHAR is generally preferred for names, addresses, or any text with variable length to avoid wasted space. • Trailing spaces in CHAR values are stripped on retrieval in many databases, which can surprise developers comparing string lengths.

Example: A product name column benefits from VARCHAR(50) since names vary in length, while a fixed two-letter country code column is a natural fit for CHAR(2) since every value is exactly the same size.

Code Example:

CREATE TABLE employees (
    name VARCHAR(50),
    code CHAR(10)
);

Interview Tip: A concise interview answer is:

"VARCHAR stores variable-length strings and only uses the space the data actually needs, while CHAR is fixed-length and pads shorter values with spaces up to the declared size. I use VARCHAR for most text like names, and CHAR only for genuinely fixed-width data like country or status codes."