SQL Data Types
Choosing the right data type for each column controls correctness, storage cost and performance.
Introduction
Choosing the right data type for each column controls correctness, storage cost and performance. Get this wrong and you'll fight bugs for years.
Beginner analogy: data types are containers — you wouldn't store soup in a paper bag. Numbers go in numeric columns, dates in date columns, text in text columns.
Learning objective: Apply SQL Data Types correctly in a TechLearningPRO production database — with clear query intent, safe defaults, and failure modes you can explain in review.
Treat every example as production-shaped SQL: explicit column lists, deterministic predicates, and indexes that match the access path. Prefer PostgreSQL dialect notes when dialects diverge; call out MySQL differences when they matter.
Purpose of this lesson
Apply SQL Data Types correctly in a TechLearningPRO production database — with clear query intent, safe defaults, and failure modes you can explain in review.
Understanding the topic
Core concepts to understand:
- Numeric: INT, BIGINT, NUMERIC(10,2) for money, REAL/DOUBLE for science.
- Text: TEXT (Postgres prefers it), VARCHAR(n) (MySQL), CHAR(n).
- Dates: DATE, TIME, TIMESTAMP, TIMESTAMPTZ (with timezone).
- Boolean: BOOLEAN (true/false).
- JSON: JSONB (Postgres) — schemaless data inside a column.
- UUID — globally unique identifiers, common for distributed apps.
Step-by-step explanation
- State the business question SQL Data Types answers and the tables involved.
- Read the workflow diagram and map each stage to parser, optimizer, or storage.
- Write the smallest correct statement (or design change) for a TechLearningPRO Orders/learners dataset.
- Predict the failure mode: wrong cardinality, missing index, or unsafe write — then verify.
Syntax reference
Visual workflow / architecture:
Column Type → Storage / Use─────────────────────────────────INT 4 bytes countsBIGINT 8 bytes IDs at scaleNUMERIC(p,s) variable moneyTEXT variable any stringTIMESTAMPTZ 8 bytes events with TZBOOLEAN 1 byte flagsJSONB variable flexible docsUUID 16 bytes distributed IDs
Informative example
Shape to practice: rewrite the diagram as an explicit SQL statement against orders(id, learner_id, status, amount_cents, created_at) — select only the columns you need.
-- TechLearningPRO Orders sketch for: SQL Data TypesSELECT id, status, amount_centsFROM ordersWHERE status = 'PENDING'ORDER BY created_at DESCLIMIT 50;
Real-world use
Stripe stores money as NUMERIC (never float). Every Postgres-backed SaaS uses TIMESTAMPTZ to avoid timezone bugs. JSONB powers half of modern Postgres apps for semi-structured data.
Security implications
SQL is a trust boundary. Application code must never concatenate untrusted input into statements.
- Use bind parameters / prepared statements — never string-built SQL from request bodies.
- Least-privilege DB roles: app users should not own DDL in production.
- Avoid returning raw DB errors to clients; log server-side with a correlation id.
Best practices
- Money → NUMERIC, never FLOAT.
- Times → TIMESTAMPTZ, store UTC.
- Use UUIDs for distributed/multi-region systems.
Common mistakes
- Storing money in FLOAT/DOUBLE — leads to rounding errors.
- TIMESTAMP without timezone — silent bugs across regions.
- Overusing TEXT for everything (fine in Postgres, wasteful in MySQL).
Advanced interview questions
Interview Prep
Practice concise answers, then expand each card for the explanation.
1BeginnerQuestionQ1. Why never store money in FLOAT?+
Answer
2IntermediateQuestionQ2. Difference between TIMESTAMP and TIMESTAMPTZ.+
Answer
3IntermediateQuestionQ3. When to use UUID vs SERIAL/BIGINT?+
Answer
4AdvancedQuestionQ4. What is JSONB and when would you use it?+
Answer
5AdvancedQuestionQ5. VARCHAR vs TEXT.+
Answer
Hands-on exercise
Hands-on + interview drill:
- Implement the smallest correct example for SQL Data Types on an Orders-like schema.
- Q1. Why never store money in FLOAT?
- Q2. Difference between TIMESTAMP and TIMESTAMPTZ.
- Q3. When to use UUID vs SERIAL/BIGINT?
- Q4. What is JSONB and when would you use it?
- Q5. VARCHAR vs TEXT.
Summary
SQL Data Types is a production SQL skill: state the question, write the precise statement, and prove it with an execution plan or constraint — not with lucky result sets.