Skip to content
Learn Netverks

Lesson

Step 11/36 31% through track

data-types-sql

SQL data types

Last reviewed May 28, 2026 Content v20260528
Track mode
sql_sandbox
Means
SQL sandbox
Reading
~2 min
Level
beginner

This lesson

This lesson teaches SQL data types: the SQL patterns, schema habits, and query reasoning you need before advancing in SQL.

Teams query SQL data types on every SQL codebase—skipping it leaves gaps in debugging and data reviews.

You will apply SQL data types in contexts like: Postgres, MySQL, SQLite, warehouses, and ORMs that still expose SQL.

Copy SQL from each lesson into SQLite (sqlite3), DB Fiddle, or local Postgres—read result grids and row counts. The in-browser SQL lab (sql_sandbox) will run queries when the runner ships; until then, local clients are the practice path.

When you can explain the previous lesson's ideas without copying example queries verbatim.

Columns have types that constrain stored values and affect sorting, storage, and index behavior. ANSI SQL defines common families; each engine adds aliases (e.g., TEXT vs VARCHAR).

Common scalar types

  • INTEGER / BIGINT — whole numbers, often for keys and counts
  • REAL / DOUBLE / DECIMAL — floating or fixed-precision numbers (money often uses DECIMAL)
  • TEXT / VARCHAR — strings of varying length
  • DATE, TIME, TIMESTAMP — calendar and clock values (timezone rules vary by engine)
  • BOOLEAN — TRUE/FALSE (SQLite uses 0/1)

CREATE with types

CREATE TABLE events (
  id INTEGER PRIMARY KEY,
  title TEXT NOT NULL,
  starts_at TEXT,
  is_public INTEGER DEFAULT 1,
  ticket_price REAL
);

Practice: Run in SQLite, DB Fiddle, or Postgres. Create the customers/orders sample from the relational-model lesson if needed.

Type casting

SELECT CAST('42' AS INTEGER) AS n;

SELECT total,
       CAST(total AS INTEGER) AS total_int
FROM orders;

CAST converts values for comparisons and exports—watch precision loss on decimals.

Choosing types

Pick the smallest type that fits domain rules. Store timestamps in UTC at the application or database layer. Avoid storing money as binary floats when DECIMAL is available.

Important interview questions and answers

  1. Q: TEXT vs VARCHAR?
    A: Often interchangeable in practice; VARCHAR may enforce max length depending on engine.
  2. Q: Why DECIMAL for currency?
    A: Avoids floating-point rounding errors in financial calculations.

Self-check

  1. Which type is appropriate for a product price with cents?
  2. What does NOT NULL on a column mean?

Tip: Store money as DECIMAL where available; avoid REAL for currency in Postgres/MySQL.

Interview prep

DECIMAL for money?

Fixed precision avoids float rounding errors.

NOT NULL?

Column must have a value on INSERT.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • DECIMAL vs FLOAT?
  • TIMESTAMP TZ?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump