SQL Data Types

Updated · 10 min read
mediumPostgreSQL 18sqldata-typesnumerictimestamptime-zoneinterview

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, INTEGER and BIGINT, from small to huge. INTEGER is the usual choice.
  • Money and other exact decimals: NUMERIC(p, s). Never REAL, FLOAT or DOUBLE PRECISION, which are approximate.
  • Text: VARCHAR(n) with a limit, TEXT without one; CHAR(n) pads with spaces and is rarely what you want.
  • Dates and times: DATE, TIME, TIMESTAMP, and TIMESTAMPTZ for moments people in different time zones share.
  • BOOLEAN holds true, false or NULL.

Whole Numbers: Pick the Size

TypeRangeSizeUse for
SMALLINT-32,768 to 32,7672 bytesSmall counts, where space matters
INTEGERabout -2.1 billion to 2.1 billion4 bytesMost ids and counts
BIGINTabout ±9.2 quintillion8 bytesIds in very large tables, byte counts

A value outside the range is refused, not wrapped around:

SQL▶ Try it
SELECT 40000::smallint;
Output
ERROR:  smallint out of range

The 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:

SQL▶ Try it
SELECT 12.345::numeric(8,2) AS rounded_up,
       12.344::numeric(8,2) AS rounded_down,
       123456.78::numeric(8,2) AS fits;
Output
 rounded_up | rounded_down |   fits
------------+--------------+-----------
      12.35 |        12.34 | 123456.78
(1 row)
SQL▶ Try it
SELECT 1234567.89::numeric(8,2);
Output
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.

Three whole-number sizes with their ranges, and the anatomy of NUMERIC(8,2)

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:

SQL▶ Try it
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);
Output
    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:

SQL▶ Try it
SELECT 19.99::real * 3 AS real_total,
       19.99::numeric * 3 AS numeric_total;
Output
    real_total     | numeric_total
-------------------+---------------
 59.96999931335449 |         59.97
(1 row)
Ten 0.1 values add to 0.9999999999999999 in DOUBLE PRECISION and exactly 1.0 in NUMERIC

Text: VARCHAR, TEXT and CHAR

TypeHoldsNotes
VARCHAR(n)Text up to n charactersLonger text is refused; the usual choice when a limit makes sense
TEXTText of any lengthNot standard SQL, but PostgreSQL, MySQL and SQLite have it
CHAR(n)Exactly n characters, padded with spacesOnly 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:

SQL▶ Try it
SELECT 'abc'::char(5) = 'abc  '::char(5) AS char_equal,
       'abc'::varchar = 'abc  '::varchar AS varchar_equal;
Output
 char_equal | varchar_equal
------------+---------------
 t          | f
(1 row)

Dates, Times and Time Zones

TypeHoldsExample
DATEA calendar day2026-01-05
TIMEA time of day18:00:00
TIMESTAMPA date and time, with no time zone2026-01-05 18:00:00
TIMESTAMPTZA moment in time, shown in your session's time zone2026-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:

SQL▶ Try it
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;
Output
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.

Logged at 6:00 PM in Dallas: TIMESTAMPTZ shows 4:00 PM in San Francisco; TIMESTAMP still says 18:00

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):

SQL▶ Try it
SELECT 'TRUE'::boolean AS a, 'no'::boolean AS b, '1'::boolean AS c, 'off'::boolean AS d;
Output
 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

PostgreSQLMySQLSQL ServerOracle
INTEGERINTINTINTEGER (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 languageVARCHAR2(80)
TEXTTEXTVARCHAR(MAX)CLOB
TIMESTAMPTZTIMESTAMP (converts to UTC)DATETIMEOFFSETTIMESTAMP WITH TIME ZONE
BOOLEANBOOLEAN (stored as 0 and 1)BITBOOLEAN (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
SQL▶ Try it
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;
Output
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.

Quick check: SQL data types

3 questions
1Which type should hold a course fee such as 450.00?
2What's the largest value NUMERIC(8,2) can hold?
3Navika logs a time at 6:00 PM in Dallas into a TIMESTAMPTZ column. What does Srihitha see in San Francisco?

Key Takeaways

You're storingUse
An id or a countINTEGER (BIGINT for billions)
MoneyNUMERIC(8,2)
A name or emailVARCHAR(n)
Notes of any lengthTEXT
A calendar dayDATE
When something happenedTIMESTAMPTZ
Yes or noBOOLEAN

Was this tutorial helpful?

Lesson 29 of 115