iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

SQL Data Types

Picking the right type makes queries faster, indexes smaller, and bugs less likely. The big families exist in every major engine — only the names differ.

Integers

TypeRangeBytes
TINYINT0–255 / -128–1271
SMALLINT±32k2
INT±2.1B4
BIGINT±9 quintillion8

Decimals & floats

  • DECIMAL(p, s) — exact, base-10. Use for money: DECIMAL(10, 2).
  • FLOAT / DOUBLE — binary IEEE-754. Fast but lossy — never for money.

Strings

TypeUse
CHAR(n)Fixed-length. Padded with spaces.
VARCHAR(n)Variable-length up to n. The default choice.
TEXTEffectively unbounded. Stored separately on some engines.

Dates & times

  • DATE — Y-M-D only.
  • TIME — H:M:S only.
  • DATETIME / TIMESTAMP — both. PG / SQL Server: TIMESTAMPTZ stores zone.

Binary & JSON

  • BLOB / BYTEA — raw bytes.
  • JSON / JSONB — structured data; JSONB in Postgres is indexable.
  • UUID — 16-byte global identifier (PG native; emulate with CHAR(36) elsewhere).
Tip: Pick the smallest type that fits the foreseeable range. Smaller types use less RAM, fit more in cache, and let indexes scan faster.

Example

Example
CREATE TABLE demo (
  id INT,
  name VARCHAR(80),
  price DECIMAL(10,2),
  notes TEXT,
  created_at DATETIME
);
Try it Yourself »

Exercise

Storing money should use…

price (10, 2)

Test yourself

Q1. For money columns, prefer…
Q2. VARCHAR vs TEXT for short fields…
Q3. For UUIDs in Postgres prefer…

Discussion

Loading…