Learn SQL
-- tables.data_types

Data Types — Choosing Correctly in SQL

Money is NUMERIC, moments are TIMESTAMPTZ, never FLOAT for cash

--concept

Types fix what a column holds and how it compares, sorts and stores. Money is NUMERIC(p,s) — never FLOAT (0.1+0.2 ≠ 0.3 in binary). Moments are TIMESTAMPTZ (UTC instant, displayed per zone); plain dates are DATE. Identifiers are BIGINT/UUID; free text is TEXT with CHECK limits, not arbitrary VARCHAR(255).

--how it works
1

Money is exact

NUMERIC(p,s) for cash. FLOAT rounds — 0.1+0.2 ≠ 0.3 in binary.

2

Moments are zoned

TIMESTAMPTZ stores UTC instants; display converts per zone. Naive TIMESTAMP breaks across DST.

3

Text + CHECK over magic lengths

TEXT plus CHECK (length ≤ n) enforces real limits; VARCHAR(255) is folklore.

4

Ids are meaningless + stable

BIGINT IDENTITY or UUID. Business values (emails) change — ids must not. JSONB for indexable payloads.

--visualization
amount: NUMERIC(12,2)✓ exact money✗ FLOAT rounds centswrong type corrupts silently
Stage 1 / 1
amount → NUMERIC(12,2): exact money. Avoid: FLOAT rounds cents.
--example
query.sql
1CREATE TABLE orders (
2 id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
3 amount NUMERIC(12,2) NOT NULL CHECK (amount >= 0),
4 created_at TIMESTAMPTZ NOT NULL DEFAULT now()
5);
Result
columnwhy
amountNUMERIC — exact
created_atTIMESTAMPTZ — zoned
idBIGINT — surrogate key

Each column gets the narrowest correct type plus a constraint. FLOAT would round money; naive TIMESTAMP would break across daylight-saving changes.

--variants

Correct order table

Exact money, zoned time, guarded amount.

query.sql
1CREATE TABLE orders (
2 id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
3 amount NUMERIC(12,2) NOT NULL CHECK (amount >= 0),
4 created_at TIMESTAMPTZ NOT NULL DEFAULT now()
5);
Result
columnchoice
amountNUMERIC — exact
created_atTIMESTAMPTZ — zoned
1CHECK rejects negatives at the cheapest layer — the write path.
2DEFAULT now() timestamps without app code.
3FLOAT here would silently mis-sum cents at scale.

Text and id policy

Limits where they matter, stability where it counts.

query.sql
1name TEXT CHECK (length(name) BETWEEN 1 AND 100),
2uid UUID PRIMARY KEY DEFAULT gen_random_uuid()
Result
columnpattern
nameTEXT + CHECK
uidUUID — merge-safe
1CHECK gives a clear error instead of silent truncation.
2UUIDs merge across shards; integer surrogates are narrower/faster locally.
3Natural keys (email) get UNIQUE, never PK.
--key facts
FLOAT trapbinary float rounds money — NUMERIC only
TZ rulestore UTC (TIMESTAMPTZ), convert on display
VARCHAR(255)folklore, not engineering — TEXT + CHECK for real limits
--use cases
Greenfield schema: money, timestamps, ids chosen once, correctly
Bug triage: rounding cents, DST-shifted reports, truncated text
--under the hood
NUMERIC is variable-length decimal — slower than INT but exact; FLOAT is fixed binary — fast but lossy.
TIMESTAMPTZ stores UTC + converts on output; indexes on date_trunc(created_at) serve day-buckets.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.