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
| Type | Use for |
|---|---|
INT / BIGINT | Whole numbers, ids |
DECIMAL(p,s) | Exact money/measures — never FLOAT for money |
VARCHAR(n) / TEXT | Short / long text |
BOOLEAN | True / false |
DATE / TIMESTAMP | Dates and date-times |
JSON / JSONB | Semi-structured data |
Casting & conversion
CAST('2026-01-12' AS DATE)CAST(package AS INT)'12'::INT -- Postgres shorthand for CAST-- A frequent bug: integer divisionSELECT 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:
| Expression | Result |
|---|---|
5 = NULL | unknown (not true — row dropped) |
NULL = NULL | unknown |
NULL AND true | unknown |
NULL OR true | true |
x IS NULL | true / 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
- 01Match the type to the data: INT for ids, DECIMAL for money, VARCHAR/TEXT for text, DATE/TIMESTAMP for time.
- 02Never use FLOAT for money — use DECIMAL (or integer minor units).
- 03Convert with CAST(x AS type) (or ::type in Postgres); watch integer division (3/2 = 1).
- 04NULL means unknown — SQL uses three-valued logic (true/false/unknown).
- 05WHERE keeps only true rows, so unknown (NULL-involving) rows are dropped — test with IS NULL.
