Skip to content
Laviyx Learn

Try

SQL course menu

SQL Data Types Explained: Numbers, Text, Dates and More

A beginner-friendly guide to SQL data types: INTEGER, DECIMAL vs FLOAT, VARCHAR vs CHAR vs TEXT, DATE and TIMESTAMP, BOOLEAN, and how types differ between MySQL, PostgreSQL, SQL Server and SQLite.

Beginner6 min readUpdated
On this page

Every column in a table has a data type. The type decides what values the column can hold, how much space they take, how they sort, and which operations make sense. Picking good types is one of the easiest ways to keep data correct.

Why types matter#

  • Correctness: a DATE column cannot hold 'next Tuesday', and a number column cannot hold 'N/A'.
  • Behaviour: numbers sort as numbers (9 before 100); text sorts character by character ('100' before '9').
  • Performance and storage: smaller, precise types make tables and indexes smaller and faster.

Whole numbers#

TypeTypical rangeUse for
SMALLINTabout ±32 thousandsmall counts, ratings
INTEGER / INTabout ±2.1 billionids, quantities, most counts
BIGINTabout ±9.2 quintillionvery large ids, big counters, money stored in paise/cents

Integer division drops the remainder in many databases:

SQLRun
SELECT 7 / 2 AS int_div,
       7 / 2.0 AS dec_div;
Output
int_div | dec_div
--------+--------
3       | 3.5

PostgreSQL, SQL Server and SQLite return 3 for 7 / 2; MySQL and Oracle return 3.5. Write 7 / 2.0 (or cast) when you need decimals.

Exact decimals: DECIMAL / NUMERIC#

DECIMAL(p, s) stores numbers exactly. p (precision) is the total number of digits; s (scale) is how many of them come after the decimal point.

DefinitionLargest value
DECIMAL(8, 2)999999.99
DECIMAL(5, 0)99999
DECIMAL(10, 4)999999.9999

NUMERIC and DECIMAL are the same thing. Use them for money and anything else where every digit must be right.

Approximate decimals: FLOAT / REAL / DOUBLE#

Floating-point types are fast and handle very large and very small numbers, but they store a close approximation in binary. Store 0.1 and 0.2 in FLOAT columns, add them, and the result is 0.30000000000000004, not 0.3.

Use FLOAT / DOUBLE PRECISION for scientific measurements and statistics, never for money.

Text#

TypeMeaningUse for
CHAR(n)exactly n characters, padded with spacesfixed-length codes such as country codes 'IN'
VARCHAR(n)up to n charactersnames, emails, titles
TEXTlong textdescriptions, articles, comments

VARCHAR(n) is the everyday choice. Its limit protects you from accidentally storing a whole essay in a name column. SQL Server uses NVARCHAR for full Unicode text (Hindi, Tamil, emoji); in PostgreSQL and MySQL with utf8mb4, ordinary VARCHAR already handles Unicode.

Dates and times#

TypeExample valueUse for
DATE2026-10-23birthdays, order dates
TIME14:30:00opening hours
TIMESTAMP / DATETIME2026-10-23 14:30:00when something happened
TIMESTAMP WITH TIME ZONE2026-10-23 14:30:00+05:30events across time zones

Always write dates in ISO format, 'YYYY-MM-DD'. It is unambiguous and sorts correctly even as text. You will learn to work with dates in Date Functions.

True or false: BOOLEAN#

PostgreSQL has a real BOOLEAN type with values TRUE, FALSE, and NULL. MySQL accepts BOOLEAN but stores it as TINYINT(1) (0 or 1). SQL Server uses BIT. SQLite stores 0 and 1 in an integer.

Other useful types#

  • UUID: globally unique identifiers such as a3f1c2e4-... (native in PostgreSQL; UNIQUEIDENTIFIER in SQL Server).
  • JSON / JSONB: structured documents inside a column (PostgreSQL, MySQL).
  • BLOB / BYTEA: binary data such as small files. Large files usually belong in file storage, with only the path in the database.

Converting between types: CAST#

CAST(value AS type) converts a value:

SQLRun
SELECT CAST('42' AS INTEGER) + 8 AS total;
Output
total
-----
50

Conversion fails (or, in some databases, silently produces 0 or NULL) when the value does not fit, such as CAST('abc' AS INTEGER). PostgreSQL also has the shorthand '42'::integer.

The same idea, different names#

ConceptPostgreSQLMySQLSQL ServerSQLite
Whole numberINTEGERINTINTINTEGER
Exact decimalNUMERIC(p,s)DECIMAL(p,s)DECIMAL(p,s)NUMERIC
Short textVARCHAR(n)VARCHAR(n)NVARCHAR(n)TEXT
Date and timeTIMESTAMPDATETIMEDATETIME2TEXT (ISO string)
True/falseBOOLEANTINYINT(1)BITINTEGER

Common mistakes#

Practice#

Practice exercise

Choose a data type for each column of an invoices table:

  1. invoice_id: auto-generated identifier
  2. customer_email: up to 255 characters
  3. amount: money with paise, up to 9,999,999.99
  4. issued_at: exact moment the invoice was created
  5. is_paid: yes or no
  6. pin_code: Indian PIN code such as 560001
Show hint

Think about whether you ever do maths with the value, whether it must be exact, and whether leading zeros could matter.

Show solution
  1. INTEGER (or BIGINT) as an auto-generated primary key.
  2. VARCHAR(255).
  3. DECIMAL(9, 2): 7 digits before the point and 2 after.
  4. TIMESTAMP (or TIMESTAMP WITH TIME ZONE if users are in several time zones).
  5. BOOLEAN (BIT in SQL Server, TINYINT(1) in MySQL).
  6. CHAR(6) or VARCHAR(6): a code, not a quantity, so store it as text.

Key takeaway#

Finished this lesson?