Core Data Types (Numeric, String/Text, Boolean, Date/Time, UUID)
Pick the right column types for numbers, text, booleans, timestamps, and UUIDs.
In plain words
Each column has a type: whole numbers, exact money amounts, text, true/false, or dates. The right type keeps data correct.
Think of it like…
Choosing containers in a kitchen: a measuring jug for exact amounts (numeric), a bucket for rough volumes (float), a labelled jar for text, and a clock that knows its timezone (timestamptz). The wrong container makes the recipe come out wrong.
Where you’ve seen it
An orders table: id bigint, amount numeric(12,2) (exact rupees and paise), status text, is_gift boolean, created_at timestamptz (an exact moment, shown in each user's timezone). Storing amount as double precision would make 0.1 + 0.2 come out as 0.30000000000000004.
Words you’ll hear
- integer / bigint
- 4-byte (up to about 2.1 billion) and 8-byte whole numbers.
- numeric(p, s)
- Exact decimal with p digits, s after the point: use for money.
- double precision
- Fast approximate floating point: use for measurements, not money.
- timestamptz
- PostgreSQL timestamp with time zone: stores an absolute instant.
Visual diagram
Theory & example queries
Numbers: exact vs approximate
Integers are exact and fast. integer stops at 2,147,483,647, which a busy table's id can reach, so ids and large counts should be bigint. numeric(p, s) stores exact decimals and is the right choice for money and anything that must add up to the paisa. Alternatively, store amounts as integer paise.
real and double precision are binary floating point: fast, with a huge range, but they can't represent most decimals exactly, so sums drift (0.1 + 0.2 ≠ 0.3). They suit measurements and scientific values, not prices or balances.
SELECT 0.1::double precision + 0.2::double precision = 0.3::double precision;PostgreSQL is the lesson baseline unless noted otherwise.
Calculate the selected expressions and return the requested output columns.
SELECT 0.1::numeric + 0.2::numeric = 0.3;PostgreSQL is the lesson baseline unless noted otherwise.
Calculate the selected expressions and return the requested output columns.
Expected output2 sample rows
| numeric_type | equal_to_0_3 |
|---|---|
| double precision | false |
| numeric | true |
Note: Representative PostgreSQL results; floating-point behavior follows the chosen binary representation.
returns false, while
returns true.
Key takeaway: bigint for ids, numeric for money, floats only for measurements.
Text, booleans, time and UUIDs
In PostgreSQL, text and varchar(n) perform the same; varchar(n) just adds a length limit, which is a constraint rather than a speed-up. char(n) pads with spaces and is rarely what you want. MySQL's BOOLEAN is really TINYINT(1), and its VARCHAR lengths affect index limits.
For moments in time, use timestamptz in PostgreSQL: it stores an absolute instant (internally UTC) and converts to the session's timezone for display, so users in Mumbai and London see the right local time. Plain timestamp has no timezone, so its meaning depends on whoever wrote it. Use date for calendar days like birthdays. In MySQL, TIMESTAMP is timezone-aware but limited to 2038, so DATETIME is common, with UTC handled by the application. Use the native uuid type (16 bytes) rather than text (36+ bytes) for UUID keys.
CREATE TABLE payments ( id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY, order_id bigint NOT NULL, amount numeric(12, 2) NOT NULL, currency char(3) NOT NULL, captured boolean NOT NULL DEFAULT FALSE, created_at timestamptz NOT NULL DEFAULT now());PostgreSQL is the lesson baseline unless noted otherwise.
Identify the table or database object the statement will modify.
Expected output
Note: Representative PostgreSQL status when the table name is available and identity support is enabled.
Key takeaway: timestamptz for events, date for calendar days, uuid type for UUIDs.
How it works, step by step
Follow one practical SQL workflow from start to finish. Select any step or play through the flow.
Ask what the value means
Is it an identifier, an amount of money, a measurement, a label, a yes/no, a moment, or a calendar day?
money? moment? label?
When to use it & trade-offs
What it is
Every column has a type that decides what it can hold and how it behaves. The core families are integers (smallint, integer, bigint), exact decimals (numeric/decimal), approximate numbers (real, double precision), text (text, varchar(n), char(n)), boolean, dates and times (date, time, timestamp, timestamptz) and UUID. Picking well prevents rounding errors, overflow and timezone bugs.
Why it matters: Wrong types cause some of the most expensive bugs: money stored as floating point that doesn't add up, ids overflowing at 2.1 billion, and timestamps without a timezone that shift by hours. Interviewers check data type choices in every schema design question.
When to reach for it
- Designing new tables
- Reviewing schemas for money, id and timezone bugs
- Choosing types for API and analytics data
When to avoid or be careful
- float or double for prices and balances
- integer ids on tables that could exceed 2 billion rows
- timestamp without time zone for events
Common type decisions
Strengths: numeric is exact; double is fast with a huge range.
Watch out: numeric is slower; double has rounding errors.
Strengths: timestamptz is unambiguous across timezones.
Watch out: Display depends on the session timezone, so set it deliberately.
Strengths: bigint is compact and ordered; uuid needs no coordination.
Watch out: uuid is 16 bytes, and random UUIDs scatter index inserts.
Interview signals & numbers to know
When this topic comes up
- “Totals are off by a paisa”
- “What type would you use for money?”
- “Times are 5.5 hours off”
- Integer overflow on an id column
- Schema design questions
Benchmarks & rules of thumb
- 2,147,483,647
- maximum integer
- 8 bytes
- bigint and timestamptz
- 16 bytes
- uuid
Common mistakes & follow-ups
Common interview mistakes
- Storing prices as double precision and getting totals like 99.99999999.
- Using timestamp without time zone, so events recorded from different servers disagree by hours.
- Storing UUIDs as varchar(36), more than doubling key size and slowing comparisons.
Likely interviewer follow-ups
- Which data type would you use for money, and why?
- What's the difference between timestamp and timestamptz in PostgreSQL?
- When would you choose varchar(n) over text?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Core Data Types (Numeric, String/Text, Boolean, Date/Time, UUID) with hands-on SQL practice and interview Q&A.