Skip to main content

Data Types in SQL

Data types define the kind of data that can be stored in a column, variable, or expression. Choosing the correct data type is critical for data integrity, performance, and storage efficiency.


Why Data Types Matter​

  • Storage: Different data types consume different amounts of storage.
  • Validation: They enforce what values can be stored in a column.
  • Performance: Using the right type improves query speed and indexing.
  • Portability: Being aware of standard vs vendor-specific types helps with migration.

Common SQL Data Types​

Below are the most common categories of data types in SQL. The exact names and limits can vary slightly between RDBMSs.


1. Numeric Data Types​

Used for storing numbers.

TypeDescriptionExample
INTInteger numbers (whole numbers)1, -100
SMALLINTSmaller-range integer32000
BIGINTLarger-range integer9223372036854775807
DECIMAL(p,s) or NUMERIC(p,s)Fixed precision and scale12.34
FLOAT / REALApproximate numeric with floating point3.14159
BITBoolean or bit field0, 1

2. Character and String Data Types​

Used for storing text data.

TypeDescriptionExample
CHAR(n)Fixed-length string'ABC'
VARCHAR(n)Variable-length string'Hello World'
TEXTLarge variable-length textLong articles

3. Date and Time Data Types​

Used for storing dates and times.

TypeDescriptionExample
DATEDate only (year, month, day)'2025-06-26'
TIMETime only (hour, minute, second)'14:30:00'
DATETIMEDate and time'2025-06-26 14:30:00'
TIMESTAMPDate and time, usually includes time zone info'2025-06-26 14:30:00+00'
INTERVALTime span or duration'1 day', '5 hours' (PostgreSQL)

4. Binary Data Types​

Used for storing binary data such as files, images, or blobs.

TypeDescriptionExample
BINARYFixed-length binary dataBinary blobs
VARBINARYVariable-length binary dataFiles
BLOBBinary Large ObjectImages, documents

5. Miscellaneous Data Types​

Some databases provide additional types for specialized use cases.

TypeDescriptionExample
BOOLEANTrue or false valuesTRUE, FALSE
JSONStores JSON data'{"key": "value"}'
UUIDUniversally Unique Identifier'550e8400-e29b-41d4-a716-446655440000'
ARRAYStores arrays of elements'{1,2,3}'
XMLStores XML data<note>Hello</note>

Vendor-Specific Variations​

Keep in mind:

  • MySQL: Uses TINYINT for booleans (0/1).
  • PostgreSQL: Has robust JSON and ARRAY support.
  • SQL Server: Uses UNIQUEIDENTIFIER for UUIDs and NTEXT/NVARCHAR for Unicode.

Always check the exact data type implementations for your RDBMS.


Best Practices for Choosing Data Types​

  1. Use the smallest type that fits your data — it saves storage and speeds up processing.
  2. Be explicit with precision — for monetary values, always use DECIMAL or NUMERIC rather than FLOAT to avoid rounding errors.
  3. Normalize text — use Unicode (NVARCHAR) for multi-language support.
  4. Be consistent — keep types consistent across tables for keys and joins.
  5. Consider indexing — some types, like large text or blobs, aren’t index-friendly.

Summary​

Choosing the right data types is a small decision with a big impact. They affect data accuracy, query performance, storage, and compatibility. Mastering data types is fundamental to writing robust, efficient SQL.

Tip: Always check your database’s documentation for exact type limits and behaviors.