# SQL Data Types

> Choose the right column type for whole numbers, decimals and text, and see what happens when a value does not fit.

Subject: SQL | Lesson 2 of 44 | 5 min read
Interactive lesson: https://codewithanimation.in/sql/sql-data-types
Previous: [Relational Model & Tables](https://codewithanimation.in/sql/relational-model-and-tables.md) | Next: [NULL & Three-Valued Logic](https://codewithanimation.in/sql/null-and-three-valued-logic.md)

## A type says what a column can hold

Every column has a data type. The type limits which values the column accepts and decides what you can do with them. Numbers can be added. Text can be joined and sorted by its characters.

Common types are `integer`, `numeric`, `text`, `date`, `time` and `timestamp`. Whole numbers come in three sizes: `smallint`, `integer` and `bigint`. Bigger types take more space and hold bigger values.

For exact decimals, use `numeric(precision, scale)`. Precision is the total number of digits. Scale is how many of them come after the decimal point.

## The type protects your data

A column typed `integer` refuses a name. A column typed `date` refuses a nonsense date. The database stops bad values at the door, so you do not clean them up later.

The type also decides behaviour. The text `'10'` and the number 10 sort differently, as you will see below.

## Fit the value to the type

`integer` holds -2147483648 to 2147483647. `smallint` holds -32768 to 32767. A value outside the range is refused with an error, never cut down to fit.

`numeric(3,1)` keeps 3 digits in total and 1 after the point. If a value has extra decimals, PostgreSQL rounds them first. Then it checks that the number is small enough:

```sql
CREATE TABLE items (price numeric(3,1));
INSERT INTO items VALUES (12.345);  -- stored as 12.3
INSERT INTO items VALUES (100.0);   -- error: numeric field overflow
```

The second insert fails because `numeric(3,1)` leaves only 2 digits before the point, so the biggest value is below 100.

## Common mistake: Storing numbers as text

If you keep numbers in a `text` column, they sort one character at a time. The values 10, 2 and 9 come back as 10, 2, 9, because the character `1` comes before `2`.

Do not use `real` or `double precision` for money. Floating-point values are approximations, so equality checks can surprise you. Use `numeric` for exact amounts.

## Tip: Pick the smallest type you are sure of

`integer` is the usual choice for whole numbers. Move to `bigint` only when `integer` might not be large enough. Use `numeric` whenever you need exact decimals.

## Try it: Store a value, then sort some numbers

Pick a column type and a value to see what PostgreSQL stores or why it refuses. Then switch to Sort numbers and compare an integer column with a text column.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/sql/sql-data-types

## Check yourself

### Question 1

You insert 40000 into a `smallint` column. What happens?

- A. PostgreSQL refuses it with an out of range error
- B. It stores 32767, the largest value that fits
- C. It stores 40000 anyway

Correct answer: A

A whole-number type has a fixed range. A value outside it causes an error.

- Why not B: Values outside the range are refused, never cut down.
- Why not C: `smallint` only holds -32768 to 32767.

### Question 2

A column is `numeric(3,1)`. What is stored when you insert 12.345?

- A. 12.3
- B. 12.345
- C. Nothing: the insert fails

Correct answer: A

Extra decimals are rounded to the scale. Then PostgreSQL checks that the digits before the point fit.

- Why not B: The scale is 1, so only one digit after the point is kept.
- Why not C: The size check passes because 12.3 has two digits before the point.

### Question 3

The numbers 2, 9 and 10 are stored in a `text` column. What order does `ORDER BY` return?

- A. 10, 2, 9
- B. 2, 9, 10
- C. 9, 2, 10

Correct answer: A

Text compares character by character, so `10` comes first because its first character is `1`.

- Why not B: That is the order for a number column. Text is compared one character at a time.
- Why not C: Text sorts by characters, and 9 comes after 1 and 2.

## Remember this

- A data type limits which values a column accepts and what you can do with them.
- smallint, integer and bigint have fixed ranges, and a value outside the range is refused.
- numeric(precision, scale) rounds extra decimals first, then refuses a value with too many digits before the point.
- Numbers kept as text sort one character at a time, so use a number type for numbers.
