Basic Data Types

Updated · 15 min read
easyPostgreSQL 18sqldata-typesbasicsnull

Every column has a data type: a rule about what kind of value it can hold. fee_usd holds money amounts, title holds text, and paid_on holds dates. The type isn't just a label. It decides which values the database accepts, how they compare and sort, and what arithmetic you can do with them. This lesson covers the four main kinds of values (plus the special marker NULL), the exact types the Fee Tracker uses, and three ways a type changes your results.

In One Minute

  • The four main kinds of values are numbers, text, dates and times and true/false, plus the special marker NULL for "no value".
  • The Fee Tracker uses five types: integer, numeric(8,2), varchar(n), char(2) and date.
  • A type controls which values can be stored and how they're converted: a February 30 is refused, while 12.345 in a money column is rounded to 12.35.
  • Type changes results: text compares character by character, so '10' comes before '9'; whole numbers divide to whole numbers.
  • A comparison gives true, false, or unknown when NULL is involved; WHERE keeps only the rows where it's true.
  • NULL isn't a type. It's "no value," and any column that allows it can hold it.

Four Kinds of Values, Plus NULL

KindHoldsCommon typesFee Tracker example
NumbersWhole numbers or exact decimalsinteger, numeric(p,s)grade is 12; fee_usd is 450.00
TextLetters, digits, any charactersvarchar(n), char(n), texttitle is Java OOP; state is TX
Dates and timesA calendar date, a time, or bothdate, time, timestamppaid_on is 2025-12-03
True/falsetrue or falsebooleanWhether a payment was late (worked out in a query below; no Fee Tracker column stores one, but boolean is a real PostgreSQL type)
NULLNo value at allNot a type: allowed in any column that doesn't forbid itNavika's email
A column's type is a rule about what it can hold

The Types the Fee Tracker Uses

The database keeps a description of every column, type included. This query asks it about the payments table:

SQL▶ Try it
SELECT column_name, data_type, character_maximum_length, numeric_precision, numeric_scale
FROM information_schema.columns
WHERE table_schema = 'public'
  AND table_name = 'payments'
ORDER BY ordinal_position;
Output
 column_name |     data_type     | character_maximum_length | numeric_precision | numeric_scale
-------------+-------------------+--------------------------+-------------------+---------------
 payment_id  | integer           |                     NULL |                32 |             0
 student_id  | integer           |                     NULL |                32 |             0
 course_id   | integer           |                     NULL |                32 |             0
 amount_usd  | numeric           |                     NULL |                 8 |             2
 due_on      | date              |                     NULL |              NULL |          NULL
 paid_on     | date              |                     NULL |              NULL |          NULL
 method      | character varying |                       10 |              NULL |          NULL
(7 rows)

The same query works for every table. Leave out the table_name filter and it describes all 30 columns of the Fee Tracker; sorting by table keeps each table's columns together:

SQL▶ Try it
SELECT table_name, column_name, data_type, character_maximum_length, numeric_precision, numeric_scale
FROM information_schema.columns
WHERE table_schema = 'public'
ORDER BY table_name, ordinal_position;
Output
 table_name  |   column_name   |     data_type     | character_maximum_length | numeric_precision | numeric_scale
-------------+-----------------+-------------------+--------------------------+-------------------+---------------
 cities      | city            | character varying |                       40 |              NULL |          NULL
 cities      | state           | character         |                        2 |              NULL |          NULL
 cities      | time_zone       | character varying |                       40 |              NULL |          NULL
 courses     | course_id       | integer           |                     NULL |                32 |             0
 courses     | title           | character varying |                       60 |              NULL |          NULL
 courses     | technology      | character varying |                       30 |              NULL |          NULL
 courses     | level           | character varying |                       20 |              NULL |          NULL
 courses     | fee_usd         | numeric           |                     NULL |                 8 |             2
 courses     | prerequisite_id | integer           |                     NULL |                32 |             0
 enrollments | student_id      | integer           |                     NULL |                32 |             0
 enrollments | course_id       | integer           |                     NULL |                32 |             0
 enrollments | enrolled_on     | date              |                     NULL |              NULL |          NULL
 enrollments | status          | character varying |                       10 |              NULL |          NULL
 payments    | payment_id      | integer           |                     NULL |                32 |             0
 payments    | student_id      | integer           |                     NULL |                32 |             0
 payments    | course_id       | integer           |                     NULL |                32 |             0
 payments    | amount_usd      | numeric           |                     NULL |                 8 |             2
 payments    | due_on          | date              |                     NULL |              NULL |          NULL
 payments    | paid_on         | date              |                     NULL |              NULL |          NULL
 payments    | method          | character varying |                       10 |              NULL |          NULL
 students    | student_id      | integer           |                     NULL |                32 |             0
 students    | first_name      | character varying |                       40 |              NULL |          NULL
 students    | last_name       | character varying |                       40 |              NULL |          NULL
 students    | email           | character varying |                       80 |              NULL |          NULL
 students    | phone           | character varying |                       20 |              NULL |          NULL
 students    | city            | character varying |                       40 |              NULL |          NULL
 students    | state           | character         |                        2 |              NULL |          NULL
 students    | grade           | integer           |                     NULL |                32 |             0
 students    | mentor_id       | integer           |                     NULL |                32 |             0
 students    | joined_on       | date              |                     NULL |              NULL |          NULL
(30 rows)

Across all five tables, the Fee Tracker uses just five types:

TypeMeansUsed for
integerA whole number from -2,147,483,648 to 2,147,483,647Ids, grade
numeric(8,2)An exact decimal with up to 8 digits in all, 2 of them after the decimal point, so up to 6 before itMoney: fee_usd, amount_usd
varchar(n)Variable-length text with a maximum of n characters ("character varying")Names, titles, email, method
char(2)Fixed-length text of width 2; a shorter value is padded with spacesstate: CA, TX
dateA calendar date, with no time of dayjoined_on, enrolled_on, due_on, paid_on
What each Fee Tracker type holds, and where its limits are

For exact money calculations, the Fee Tracker uses numeric rather than a floating-point type. numeric keeps decimals exactly: 450.00 is stored as exactly 450.00, so totals come out to the cent. Floating-point types such as real store close approximations instead, which suits measurements more than money. You'll meet the other types, such as boolean, timestamp and text, in SQL Data Types later in the course.

The Type Is a Rule

A column's type controls which values can be stored and how PostgreSQL converts them. Some values are refused outright; others are converted or rounded to fit. There's no February 30, so the database won't store it as a date:

SQL▶ Try it
UPDATE payments
SET paid_on = '2026-02-30'
WHERE payment_id = 1;
Output
ERROR:  date/time field value out of range: "2026-02-30"

And state is two characters wide, so a full state name doesn't fit:

SQL▶ Try it
UPDATE students
SET state = 'Texas'
WHERE student_id = 103;
Output
ERROR:  value too long for type character(2)

Both statements change nothing. You'll learn UPDATE properly in its own lesson later in the course; here it's enough to see that the type protects the data. (Try it starts every run from a fresh copy, so nothing could break anyway.)

Not every awkward value is refused. A numeric(8,2) column keeps two decimal places, so a value with three is rounded to fit, and a value with too many digits before the point is refused. ::numeric(8,2) below means "as a numeric(8,2) value", so you can see the conversion without changing any table (you'll learn this in Type Conversion):

SQL▶ Try it
SELECT 12.345::numeric(8,2) AS rounded, 999999.99::numeric(8,2) AS largest;
Output
 rounded |  largest
---------+-----------
   12.35 | 999999.99
(1 row)
SQL▶ Try it
SELECT 1000000.00::numeric(8,2) AS too_big;
Output
ERROR:  numeric field overflow
DETAIL:  A field with precision 8, scale 2 must round to an absolute value less than 10^6.

char(2) behaves differently again: a value shorter than two characters is accepted and padded with a space. 'T' is stored as T plus one space. PostgreSQL ignores that trailing space when it counts the characters, but the space is still stored: the course's database uses UTF-8, where these plain ASCII characters take one byte each, so the padded value takes 2 bytes:

SQL▶ Try it
SELECT 'T'::char(2) AS state_code, length('T'::char(2)) AS characters, octet_length('T'::char(2)) AS bytes;
Output
 state_code | characters | bytes
------------+------------+-------
 T          |          1 |     2
(1 row)
Refused, rounded, or padded: what the type does to each value

How the Type Changes Your Results

The same characters can mean different things depending on the type.

Text Compares Character by Character

SQL▶ Try it
SELECT '10' < '9' AS as_text, 10 < 9 AS as_numbers;
Output
 as_text | as_numbers
---------+------------
 t       | f
(1 row)

As text, '10' comes before '9': text values are compared character by character, following the database's collation (its rules for sorting text). The course's database uses the simple C collation, which compares the characters' codes, so the first character 1 sorts before 9. Other collations order some text differently, but they put '10' before '9' too, just as "apple" comes before "banana". As numbers, 10 is bigger than 9. (PostgreSQL prints true as t and false as f.)

Whole Numbers Divide to Whole Numbers

SQL▶ Try it
SELECT 7 / 2 AS whole_numbers, 7.0 / 2 AS decimals, -7 / 2 AS negative;
Output
 whole_numbers |      decimals      | negative
---------------+--------------------+----------
             3 | 3.5000000000000000 |       -3
(1 row)

When both numbers are integers, PostgreSQL does integer division: it cuts off the fractional part instead of returning a decimal. 7 divided by 2 is 3, and -7 divided by 2 is -3, not -4, because the fraction is cut off toward zero rather than rounded down. Write one of the numbers with a decimal point and you get 3.5.

Dates Can Be Subtracted

SQL▶ Try it
SELECT payment_id, due_on, paid_on, paid_on - due_on AS days_late
FROM payments
WHERE payment_id IN (3, 7)
ORDER BY payment_id;
Output
 payment_id |   due_on   |  paid_on   | days_late
------------+------------+------------+-----------
          3 | 2025-12-01 | 2025-12-03 |         2
          7 | 2025-12-15 | 2025-12-19 |         4
(2 rows)

Subtracting one date from another gives the number of days between them: payment 3 was 2 days late and payment 7 was 4 days late. That only works because both columns are dates; the same characters stored as text couldn't be subtracted.

True, False and Unknown

A comparison produces a Boolean result: true or false, even though no Fee Tracker column stores one. Here each payment is checked against its due date:

SQL▶ Try it
SELECT payment_id, paid_on > due_on AS late
FROM payments
ORDER BY payment_id;
Output
 payment_id | late
------------+------
          1 | f
          2 | f
          3 | t
          4 | f
          5 | f
          6 | f
          7 | t
(7 rows)

The late column has the type boolean: every value here is t (true) or f (false), because every payment has both dates.

If the comparison can't be decided because a value is NULL, the result is unknown, which PostgreSQL represents as NULL (Try it prints the word). SQL uses this three-valued logic because "is a missing value equal to this one?" has no honest yes-or-no answer. Advitha and Navika have no email, so asking whether their email matches Srihitha's gives unknown:

SQL▶ Try it
SELECT first_name, email = 'srihitha.anamaneni@example.com' AS matches
FROM students
ORDER BY student_id;
Output
 first_name | matches
------------+---------
 Srihitha   | t
 Advitha    | NULL
 Navika     | NULL
(3 rows)

A WHERE clause keeps only the rows where its condition is true. Rows where it's false or unknown are left out. That's why WHERE email = NULL finds nobody, not even the students with no email: the comparison is unknown for every row. The way to find missing values is IS NULL:

SQL▶ Try it
SELECT first_name
FROM students
WHERE email IS NULL
ORDER BY first_name;
Output
 first_name
------------
 Advitha
 Navika
(2 rows)
A comparison with NULL is unknown, and WHERE keeps only true

This lesson covers just enough to explain comparisons: what NULL means, why comparing it gives unknown, and why IS NULL exists. NULL Values goes further: IS NOT NULL, COALESCE, and how NULL behaves in arithmetic, totals and joins.

How a Type Shows Up in a Result

You seeBecause
A value lined up on the rightIn this psql-style output, it's generally a number
A value lined up on the leftIn this psql-style output, it's generally text, a date or true/false. Alignment is how the client displays results; other tools may differ
450.00, always two decimalsThe column is numeric(8,2)
2025-12-03PostgreSQL shows dates as year-month-day by default; its DateStyle setting can change that
t or fIt's a true/false value
NULLThere's no value, whatever the column's type

Common Mistakes

Storing numbers as text. Text compares character by character, so '10' < '9' is true, and you can't add up a text column. Use a number type for anything you'll sort or calculate with.

Expecting 7 / 2 to be 3.5. Two integers divide to an integer. Write 7.0 / 2, or make sure one side is a decimal.

Putting an invalid date in quotes and hoping. '2026-02-30' looks like a date, but the database checks it and refuses it.

Thinking NULL is zero or empty text. NULL means no value. Zero is a number, and '' is text with nothing in it.

Writing = NULL. WHERE email = NULL returns no rows, because any comparison with NULL is unknown. Write WHERE email IS NULL.

Practice

Challenge: For every payment, show the payment_id, the amount_usd, and a true/false column called big that says whether the amount was more than $300.00. Sort by payment_id. Which payments are big?

Show solution
SQL▶ Try it
SELECT payment_id, amount_usd, amount_usd > 300 AS big
FROM payments
ORDER BY payment_id;
Output
 payment_id | amount_usd | big
------------+------------+-----
          1 |     450.00 | t
          2 |     250.00 | f
          3 |     250.00 | f
          4 |     550.00 | t
          5 |     200.00 | f
          6 |     225.00 | f
          7 |     225.00 | f
(7 rows)

Payments 1 ($450.00) and 4 ($550.00) are the only ones over $300.00. The comparison produces a boolean for every row, and the numbers line up on the right because amount_usd is numeric.

One more question: what does email = 'navika@example.com' give for each student?

Srihitha gets f (false), because her email is a different address. Advitha and Navika get NULL (unknown), because they have no email, and comparing a missing value with anything is unknown. So a WHERE with that condition returns no rows at all. To find the students with no email, use WHERE email IS NULL.

Quick check: Data types

4 questions
1Which type does the Fee Tracker use for money?
2What does SELECT 7 / 2; return in PostgreSQL?
3Why does SELECT '10' < '9'; return true?
4Navika's email is NULL. Which rows does WHERE email = NULL return?

Key Takeaways

KindFee Tracker typeExample value
Whole numberinteger101
Moneynumeric(8,2)450.00
Text up to n charactersvarchar(n)'Java OOP'
Fixed width of 2 characterschar(2)'TX'
Datedate'2025-12-03'
True/falseboolean (from a comparison)t

Was this tutorial helpful?

Lesson 9 of 115