SQL Data Types
Choosing a column's type decides what it can hold, how big it can grow, and whether arithmetic on it is exact. Basic Data Types introduced the five types the Fee Tracker uses. Now that you're creating your own tables, you need the full picture: which whole-number size to pick, why money must never be stored in a floating-point type, how the text types differ, what a time zone does to a timestamp, and how BOOLEAN works. Each section shows real results, including the surprises.
In One Minute
- Whole numbers:
SMALLINT,INTEGERandBIGINT, from small to huge.INTEGERis the usual choice. - Money and other exact decimals:
NUMERIC(p, s). NeverREAL,FLOATorDOUBLE PRECISION, which are approximate. - Text:
VARCHAR(n)with a limit,TEXTwithout one;CHAR(n)pads with spaces and is rarely what you want. - Dates and times:
DATE,TIME,TIMESTAMP, andTIMESTAMPTZfor moments people in different time zones share. BOOLEANholdstrue,falseorNULL.
Whole Numbers: Pick the Size
| Type | Range | Size | Use for |
|---|---|---|---|
SMALLINT | -32,768 to 32,767 | 2 bytes | Small counts, where space matters |
INTEGER | about -2.1 billion to 2.1 billion | 4 bytes | Most ids and counts |
BIGINT | about ±9.2 quintillion | 8 bytes | Ids in very large tables, byte counts |
A value outside the range is refused, not wrapped around:
SELECT 40000::smallint;ERROR: smallint out of rangeThe same happens when arithmetic goes past the top: 2147483647 + 1 on integers fails with integer out of range, while the same sum as a BIGINT gives 2147483648. INTEGER is right for the Fee Tracker's ids; a table expecting billions of rows, such as page views, would use BIGINT.
Exact Decimals with NUMERIC
NUMERIC(p, s) stores decimals exactly: p digits in total, s of them after the decimal point. The Fee Tracker's amount_usd is NUMERIC(8,2): up to 8 digits, 2 after the point, so the largest value is 999,999.99. Extra decimal places are rounded, and too many digits before the point is an error:
SELECT 12.345::numeric(8,2) AS rounded_up,
12.344::numeric(8,2) AS rounded_down,
123456.78::numeric(8,2) AS fits; rounded_up | rounded_down | fits
------------+--------------+-----------
12.35 | 12.34 | 123456.78
(1 row)SELECT 1234567.89::numeric(8,2);ERROR: numeric field overflow
DETAIL: A field with precision 8, scale 2 must round to an absolute value less than 10^6.The detail line explains the limit: with 2 of the 8 digits after the point, the value must be below 10^6, a million. DECIMAL is another name for NUMERIC; they're the same type.
Why Money Is Never FLOAT
REAL and DOUBLE PRECISION (FLOAT) store numbers in binary, so most decimals are stored only approximately. Ten dimes make exactly a dollar. Add ten 0.1 values in each type:
SELECT sum(x) AS float_total
FROM (VALUES (0.1::double precision), (0.1), (0.1), (0.1), (0.1),
(0.1), (0.1), (0.1), (0.1), (0.1)) AS dimes(x);
SELECT sum(x) AS numeric_total
FROM (VALUES (0.1::numeric), (0.1), (0.1), (0.1), (0.1),
(0.1), (0.1), (0.1), (0.1), (0.1)) AS dimes(x); float_total
--------------------
0.9999999999999999
(1 row)
numeric_total
---------------
1.0
(1 row)The floating-point total is a hair short of 1, and a check like total = 1.00 would be false. With REAL, the error shows sooner: three $19.99 items come to 59.97 in NUMERIC, but not in REAL:
SELECT 19.99::real * 3 AS real_total,
19.99::numeric * 3 AS numeric_total; real_total | numeric_total
-------------------+---------------
59.96999931335449 | 59.97
(1 row)Text: VARCHAR, TEXT and CHAR
| Type | Holds | Notes |
|---|---|---|
VARCHAR(n) | Text up to n characters | Longer text is refused; the usual choice when a limit makes sense |
TEXT | Text of any length | Not standard SQL, but PostgreSQL, MySQL and SQLite have it |
CHAR(n) | Exactly n characters, padded with spaces | Only for fixed codes, such as state (CA, TX) |
In PostgreSQL, VARCHAR(n) and TEXT are stored the same way and are equally fast; the limit is just a rule. So use VARCHAR(n) when a limit protects your data (a two-letter state, an 80-character email) and TEXT for notes and descriptions.
CHAR(n) has a quirk worth knowing: trailing spaces don't count when comparing CHAR values, but they do for VARCHAR and TEXT:
SELECT 'abc'::char(5) = 'abc '::char(5) AS char_equal,
'abc'::varchar = 'abc '::varchar AS varchar_equal; char_equal | varchar_equal
------------+---------------
t | f
(1 row)Dates, Times and Time Zones
| Type | Holds | Example |
|---|---|---|
DATE | A calendar day | 2026-01-05 |
TIME | A time of day | 18:00:00 |
TIMESTAMP | A date and time, with no time zone | 2026-01-05 18:00:00 |
TIMESTAMPTZ | A moment in time, shown in your session's time zone | 2026-01-05 18:00:00-06 |
The difference matters as soon as people in two time zones use the same data. Navika finishes a study session at 6:00 PM in Dallas (Central time) and logs it. Srihitha, in San Francisco (Pacific time, two hours behind), reads the log:
CREATE TABLE session_log (
logged_at TIMESTAMP,
logged_at_tz TIMESTAMPTZ
);
SET TIME ZONE 'America/Chicago';
INSERT INTO session_log VALUES ('2026-01-05 18:00', '2026-01-05 18:00');
SET TIME ZONE 'America/Los_Angeles';
SELECT logged_at, logged_at_tz FROM session_log;CREATE TABLE
SET
INSERT 0 1
SET
logged_at | logged_at_tz
---------------------+------------------------
2026-01-05 18:00:00 | 2026-01-05 16:00:00-08
(1 row)TIMESTAMPTZ stored the moment, so Srihitha sees it correctly as 4:00 PM her time (-08 means eight hours behind UTC). Plain TIMESTAMP just stored the digits 18:00, so it looks to Srihitha as if Navika studied until 6:00 PM in San Francisco, two hours later than she really did.
SET TIME ZONE here changes the time zone for this session only. Try it starts each run in UTC, which is why the example sets it first.
BOOLEAN
BOOLEAN holds true, false or NULL. PostgreSQL accepts several spellings on input, such as 'yes', 'no', '1' and 'off', and psql shows the values as t or f (most client libraries turn them into true and false):
SELECT 'TRUE'::boolean AS a, 'no'::boolean AS b, '1'::boolean AS c, 'off'::boolean AS d; a | b | c | d
---+---+---+---
t | f | t | f
(1 row)Anything else, such as 'maybe', is an error. A NULL boolean means unknown, with the three-valued logic from NULL Values: NOT NULL is still NULL.
The Same Types in Other Databases
| PostgreSQL | MySQL | SQL Server | Oracle |
|---|---|---|---|
INTEGER | INT | INT | INTEGER (stored as NUMBER(38)) |
NUMERIC(8,2) | DECIMAL(8,2) | DECIMAL(8,2) | NUMBER(8,2) |
VARCHAR(80) | VARCHAR(80) | VARCHAR(80), or NVARCHAR(80) for any language | VARCHAR2(80) |
TEXT | TEXT | VARCHAR(MAX) | CLOB |
TIMESTAMPTZ | TIMESTAMP (converts to UTC) | DATETIMEOFFSET | TIMESTAMP WITH TIME ZONE |
BOOLEAN | BOOLEAN (stored as 0 and 1) | BIT | BOOLEAN (23ai and later) |
Tabs showing each database's version of every example are coming to this course.
Common Mistakes
Money in FLOAT or REAL. Totals drift by fractions of a cent. Use NUMERIC.
NUMERIC without enough digits. NUMERIC(5,2) tops out at 999.99; a $1,200 course fee won't fit.
TIMESTAMP for moments shared across time zones. It stores digits, not a moment. Use TIMESTAMPTZ.
CHAR(n) for ordinary text. It pads with spaces. Use VARCHAR(n) or TEXT.
Dates like 01/05/2026. Is that January 5 or May 1? It depends on settings. Write '2026-01-05'.
Interview Corner
Why shouldn't you store money in a FLOAT column?
Floating-point types store most decimals approximately in binary, so sums drift: ten 0.1 values add to 0.9999999999999999. NUMERIC (or DECIMAL) stores decimals exactly.
What's the difference between TIMESTAMP and TIMESTAMPTZ in PostgreSQL?
TIMESTAMP stores a date and time with no time zone, just the digits. TIMESTAMPTZ stores an exact moment and shows it in each session's time zone, so 6:00 PM in Dallas reads as 4:00 PM in San Francisco.
What's the difference between CHAR and VARCHAR?
CHAR(n) always holds exactly n characters, padding with spaces; VARCHAR(n) holds up to n characters with no padding.
Practice
Challenge: Create a table course_offers with an offer_id (INTEGER), a title of up to 60 characters, a price_usd that holds up to $9,999.99 exactly, a starts_at moment that works across time zones, and an is_online flag. Insert one offer, "SQL Basics", at $300.00, starting 2026-02-02 at 5:00 PM Dallas time ('2026-02-02 17:00-06'), online. Show it in San Francisco time.
Show solution
CREATE TABLE course_offers (
offer_id INTEGER,
title VARCHAR(60),
price_usd NUMERIC(6,2),
starts_at TIMESTAMPTZ,
is_online BOOLEAN
);
INSERT INTO course_offers
VALUES (1, 'SQL Basics', 300.00, '2026-02-02 17:00-06', true);
SET TIME ZONE 'America/Los_Angeles';
SELECT * FROM course_offers;CREATE TABLE
INSERT 0 1
SET
offer_id | title | price_usd | starts_at | is_online
----------+------------+-----------+------------------------+-----------
1 | SQL Basics | 300.00 | 2026-02-02 15:00:00-08 | t
(1 row)NUMERIC(6,2) holds up to 9,999.99. The start time, 5:00 PM in Dallas, shows as 3:00 PM in San Francisco.
Key Takeaways
| You're storing | Use |
|---|---|
| An id or a count | INTEGER (BIGINT for billions) |
| Money | NUMERIC(8,2) |
| A name or email | VARCHAR(n) |
| Notes of any length | TEXT |
| A calendar day | DATE |
| When something happened | TIMESTAMPTZ |
| Yes or no | BOOLEAN |
Was this tutorial helpful?