SQL Tutorial 0/85 lessons ~6 min read Lesson 9

    SQL Data Types

    Choosing the right data type for each column controls correctness, storage cost and performance.

    Course progress0%
    Focus
    13 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    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

    1. State the business question SQL Data Types answers and the tables involved.
    2. Read the workflow diagram and map each stage to parser, optimizer, or storage.
    3. Write the smallest correct statement (or design change) for a TechLearningPRO Orders/learners dataset.
    4. Predict the failure mode: wrong cardinality, missing index, or unsafe write — then verify.

    Syntax reference

    Visual workflow / architecture:

    bash
    Column Type → Storage / Use
    ─────────────────────────────────
    INT 4 bytes counts
    BIGINT 8 bytes IDs at scale
    NUMERIC(p,s) variable money
    TEXT variable any string
    TIMESTAMPTZ 8 bytes events with TZ
    BOOLEAN 1 byte flags
    JSONB variable flexible docs
    UUID 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.

    sql
    -- TechLearningPRO Orders sketch for: SQL Data Types
    SELECT id, status, amount_cents
    FROM orders
    WHERE status = 'PENDING'
    ORDER BY created_at DESC
    LIMIT 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.

    5 questions
    1BeginnerQuestionQ1. Why never store money in FLOAT?+

    Answer

    Pick the smallest type that covers your range.
    2IntermediateQuestionQ2. Difference between TIMESTAMP and TIMESTAMPTZ.+

    Answer

    Money: NUMERIC. Time: TIMESTAMPTZ.
    3IntermediateQuestionQ3. When to use UUID vs SERIAL/BIGINT?+

    Answer

    JSONB lets you store flexible documents inside relational tables.
    4AdvancedQuestionQ4. What is JSONB and when would you use it?+

    Answer

    JSONB lets you store flexible documents inside relational tables.
    5AdvancedQuestionQ5. VARCHAR vs TEXT.+

    Answer

    JSONB lets you store flexible documents inside relational tables.

    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.

    Ready to mark this lesson complete?Track your journey across the entire course.