all topics

Data Types

int / integer

Stores integer values, whole numbers, no decimal point.

sql
age INT

decimal

Stores floating-point values, and takes two numbers in parentheses to control exactly how: DECIMAL(total digits, digits after the decimal point).

sql
salary DECIMAL(10, 2)

DECIMAL(10, 2) means: 10 digits total, and 2 of those are reserved for after the decimal point, leaving 8 for before it. So this column can hold anything up to 99999999.99.

char

Stores character values, at a fixed length. CHAR(20) always takes up 20 characters of storage, a shorter value gets padded with spaces to fill it out.

sql
grade CHAR(2)

varchar

Also stores character values, but at a variable length instead, only the actual characters used take up space, no padding. CHAR for values that are always roughly the same length, VARCHAR when the length genuinely varies.

sql
ename VARCHAR(20)

char vs varchar, in memory

A common board question, and easiest to see with the same value stored both ways.

sql
name CHAR(10)
name = 'Ram'

CHAR(10) reserves all 10 characters of storage the moment the column's defined, no matter what actually gets put in it. Storing 'Ram' fills the first 3 slots and pads the remaining 7 with blank space, R a m _ _ _ _ _ _ _, all 10 slots occupied regardless.

sql
name VARCHAR(10)
name = 'Ram'

VARCHAR(10) only reserves what's actually used, plus a little overhead to track the length. Storing 'Ram' here takes up space for R a m, and the remaining 7 characters of "room" are left unused, free to be released rather than sitting padded and wasted.

That difference is the whole trade-off:

  • CHAR occupies all the memory assigned to it, every time, NOT memory efficient.
  • VARCHAR leaves unused memory space free, memory efficient.
  • CHAR has faster processing, fixed-size slots are simpler for the engine to read and jump between.
  • VARCHAR's processing is slower than CHAR, the trade for that memory efficiency, the engine has to check the actual stored length every time instead of just reading a fixed-width block.
  • CHAR allows a length of 0 to 255, 256 possible sizes.
  • VARCHAR allows a length of 0 to 65535, 65536 possible sizes, a much wider range, useful when a column's length genuinely can't be predicted ahead of time.

date

Stores a date, in the fixed format YYYY-MM-DD.

sql
dob DATE
-- e.g. 2026-03-20