← Back to Questions
SQL

What is the difference between CHAR and VARCHAR?

Learn What is the difference between CHAR and VARCHAR? with simple explanations, real-time examples, interview tips and practical use cases.

What is the Difference Between CHAR and VARCHAR in SQL?

CHAR and VARCHAR are SQL data types used to store character or string data.

In simple words:

  • CHAR stores fixed-length strings
  • VARCHAR stores variable-length strings

Simple Understanding

Data Type Storage Type
CHAR Fixed Length
VARCHAR Variable Length

What is CHAR?

CHAR stores:

Fixed-length character data.


Syntax

CHAR(size)

Example

name CHAR(10)

What Happens Internally?

If value inserted is:

Naresh

Actual storage becomes:

Naresh____

(remaining spaces padded automatically)


Important Point

CHAR always uses:

Fixed allocated storage.


Example

CHAR(10)

always reserves:

10 characters

even if actual data is smaller.


What is VARCHAR?

VARCHAR stores:

Variable-length character data.


Syntax

VARCHAR(size)

Example

name VARCHAR(10)

What Happens Internally?

If value inserted is:

Naresh

Only:

6 characters

are stored.


Important Point

VARCHAR uses:

Only required storage space.


CHAR vs VARCHAR Storage Example

Value CHAR(10) VARCHAR(10)
Naresh 10 bytes 6 bytes
Java 10 bytes 4 bytes

Why CHAR Uses More Space

Because:

  • Unused spaces are padded automatically

CHAR Internal Architecture

CHAR(10)
      |
      v
Always Allocate 10 Characters
      |
      v
Unused Space Filled with Spaces

VARCHAR Internal Architecture

VARCHAR(10)
      |
      v
Store Only Actual Data Length
      |
      v
Dynamic Storage Allocation

Main Difference Between CHAR and VARCHAR

Feature CHAR VARCHAR
Storage Type Fixed Length Variable Length
Performance Faster Slightly Slower
Storage Usage More Less
Space Padding Yes No
Best For Fixed-size values Variable-size values

When to Use CHAR

Use CHAR when:

  • Data length is fixed

Examples

  • Country Codes
  • Gender Values
  • PIN Codes
  • Status Flags

Example

gender CHAR(1)

country_code CHAR(2)

Real-Time Example

IN

US

UK

Why CHAR is Good Here

Because:

  • Length always remains fixed

When to Use VARCHAR

Use VARCHAR when:

  • Data length changes frequently

Examples

  • Name
  • Email
  • Address
  • Description

Example

name VARCHAR(100)

email VARCHAR(150)

Why VARCHAR is Better Here

Because:

  • Different users have different name lengths
  • Storage space is saved

Performance Difference

CHAR is usually:

  • Faster than VARCHAR

Reason:

  • Fixed-length storage is easier to access

VARCHAR Performance

VARCHAR requires:

  • Extra length calculation
  • Dynamic storage handling

But in Modern Systems

Difference is usually:

Very small.


Example Table

CREATE TABLE users (

    id INT PRIMARY KEY,

    gender CHAR(1),

    country_code CHAR(2),

    name VARCHAR(100),

    email VARCHAR(150)

);

Explanation

Column Data Type Reason
gender CHAR(1) Fixed Length
country_code CHAR(2) Fixed Length
name VARCHAR(100) Variable Length
email VARCHAR(150) Variable Length

CHAR vs VARCHAR Memory Example

Suppose:

100000 users

Using CHAR(100)

Each row reserves:

100 bytes

even if actual name is small.


Using VARCHAR(100)

Only actual name length stored.

Huge storage savings.


Related Learning Topics


Real-Time Banking Example

Banking systems may use:

account_type CHAR(2)

Examples:

SB

CA

Customer Name

Uses:

VARCHAR(100)

because customer names vary in size.


Real-Time E-Commerce Example

E-commerce systems use:

  • Product Code -> CHAR
  • Product Description -> VARCHAR

Real-Time Microservices Example

Microservices databases commonly use:

  • CHAR for status codes
  • VARCHAR for user data

Example

status CHAR(1)

username VARCHAR(100)

Advantages of CHAR

  • Faster performance
  • Simple storage allocation
  • Best for fixed-size values

Advantages of VARCHAR

  • Saves storage space
  • Flexible
  • Efficient for varying data lengths

Disadvantages of CHAR

  • Wastes storage space
  • Not suitable for variable-length data

Disadvantages of VARCHAR

  • Slightly slower
  • Dynamic storage overhead

Best Practices

  • Use CHAR for fixed-length values
  • Use VARCHAR for variable-length text
  • Avoid large CHAR sizes unnecessarily
  • Optimize column sizes properly

Professional Interview Answer

CHAR and VARCHAR are SQL data types used to store string data. CHAR stores fixed-length data and automatically pads unused spaces, while VARCHAR stores variable-length data and uses only the required storage space. CHAR is generally faster and suitable for fixed-size values such as country codes and status flags, whereas VARCHAR is more storage-efficient and commonly used for names, emails, and addresses. The choice between CHAR and VARCHAR depends on performance requirements and the nature of the stored data.


Why Interviewers Like This Answer

  • Clearly explains storage difference
  • Includes performance comparison
  • Provides real-world examples
  • Shows database optimization understanding
  • Explains practical usage scenarios

Frequently Asked Questions

What is difference between CHAR and VARCHAR?

CHAR stores fixed-length data, while VARCHAR stores variable-length data.

Which is faster CHAR or VARCHAR?

CHAR is usually slightly faster because of fixed-length storage.

Which uses more storage?

CHAR usually uses more storage because unused spaces are padded.

When should CHAR be used?

CHAR should be used for fixed-length values like country codes and status flags.

When should VARCHAR be used?

VARCHAR should be used for variable-length values like names and emails.

Why this SQL question is important?

This interview question helps candidates understand real-time backend development concepts, practical problem solving, coding fundamentals, system design basics and production-ready application behavior.

Practice this question carefully for Java backend roles, Spring Boot developer interviews, microservices interviews, company interviews and full-stack developer preparation.