Skip to main content
Albero Academy

Data Types & NULL

Choose the right column type, convert between types with CAST, and understand NULL's three-valued logic properly.

Chapter 9SQL13 min readBeginner friendly
  • #SQL
  • #Data Types
  • #NULL

Every column has a type, and choosing well keeps data correct and queries fast. This chapter surveys the common types, how to convert between them, and finishes the NULL story with the "three-valued logic" that explains its odd behaviour.

Common types

TypeUse for
INT / BIGINTWhole numbers, ids
DECIMAL(p,s)Exact money/measures — never FLOAT for money
VARCHAR(n) / TEXTShort / long text
BOOLEANTrue / false
DATE / TIMESTAMPDates and date-times
JSON / JSONBSemi-structured data

Casting & conversion

changing types
CAST('2026-01-12' AS DATE)
CAST(package AS INT)
'12'::INT -- Postgres shorthand for CAST
-- A frequent bug: integer division
SELECT 3 / 2; -- 1 (integer division!)
SELECT 3.0 / 2; -- 1.5 (force a decimal)
SELECT wins * 1.0 / games; -- multiply by 1.0 to get a ratio

Three-valued logic

Because NULL means "unknown", SQL logic has three truth values: true, false, and unknown. A comparison involving NULL is unknown, and WHERE only keeps rows that are true — so unknown rows are dropped:

ExpressionResult
5 = NULLunknown (not true — row dropped)
NULL = NULLunknown
NULL AND trueunknown
NULL OR truetrue
x IS NULLtrue / false (the correct test)

Practice

Reproduce the integer-division bug (3 / 2) and fix it three ways. Cast a text date to a real DATE. Then predict and verify the result of a filter like WHERE bonus <> 0 on a table where some bonus values are NULL.

Key takeaways

  1. 01Match the type to the data: INT for ids, DECIMAL for money, VARCHAR/TEXT for text, DATE/TIMESTAMP for time.
  2. 02Never use FLOAT for money — use DECIMAL (or integer minor units).
  3. 03Convert with CAST(x AS type) (or ::type in Postgres); watch integer division (3/2 = 1).
  4. 04NULL means unknown — SQL uses three-valued logic (true/false/unknown).
  5. 05WHERE keeps only true rows, so unknown (NULL-involving) rows are dropped — test with IS NULL.