Lesson 11 of 60 – SQL Data Types
18%

SQL Data Types

SQL data types define the kind of value that can be stored in a database column. Choosing the appropriate data type helps store data correctly, use storage efficiently, and perform suitable operations on that data.

Note: SQL data types are not identical across all database systems. MySQL, PostgreSQL, SQL Server, Oracle Database, and other systems may provide different data types or slightly different syntax.

1. What is a Data Type?

A data type specifies what kind of data a column can store.

For example:

  • A student's age can be stored as an integer.
  • A student's name can be stored as text.
  • A student's admission date can be stored as a date.
  • A course fee can be stored as a decimal number.
name VARCHAR(100)
age INT
fee DECIMAL(10,2)
admission_date DATE

2. Why Are Data Types Important?

Data types help a database understand how values should be stored and processed.

Using appropriate data types can help with:

  • Data accuracy
  • Storage efficiency
  • Sorting
  • Searching
  • Calculations
  • Data validation
  • Database performance

3. Main Categories of SQL Data Types

SQL data types can broadly be grouped into categories such as:

  • Numeric data types
  • Character and string data types
  • Date and time data types
  • Boolean or logical data types
  • Binary data types
  • Other database-specific types

The exact list depends on the database management system.

4. INT Data Type

INT is commonly used to store whole numbers without a fractional part.

Examples:

age INT
quantity INT
student_id INT

Example values:

18
25
100
500

5. Integer Types

Some database systems provide multiple integer types with different storage sizes and ranges.

For example, MySQL provides:

  • TINYINT
  • SMALLINT
  • MEDIUMINT
  • INT
  • BIGINT

The appropriate type depends on the range of numbers your application needs to store.

6. BIGINT

BIGINT is used for whole numbers that may exceed the range appropriate for a regular INT column.

It can be useful for very large identifiers or counters.

order_id BIGINT

The exact range depends on the database system and whether the type is signed or unsigned.

7. DECIMAL Data Type

DECIMAL is commonly used for exact numeric values, especially amounts where exact decimal precision matters.

Example:

price DECIMAL(10,2)

Here, 10 represents the precision and 2 represents the scale in systems such as MySQL.

Example values:

250.00
9999.50
12500.75

8. FLOAT Data Type

FLOAT is a floating-point numeric data type supported by many database systems.

It is useful when approximate numeric representation is acceptable.

temperature FLOAT
Important: For monetary values where exact decimal arithmetic is required, DECIMAL is generally preferred over floating-point types.

9. DOUBLE Data Type

DOUBLE is another floating-point numeric type available in several database systems, including MySQL.

It can store approximate numeric values with a larger range or precision than some single-precision floating-point types.

measurement DOUBLE

10. CHAR Data Type

CHAR is used to store fixed-length character strings.

gender CHAR(1)

For example, a column may store values such as:

M
F

CHAR can be useful when values have a consistent length.

11. VARCHAR Data Type

VARCHAR is used to store variable-length character strings.

name VARCHAR(100)
email VARCHAR(150)
city VARCHAR(100)

For example, a name column can contain text values of different lengths.

VARCHAR is commonly used for names, email addresses, cities, titles, and similar text fields.

12. CHAR vs VARCHAR

CHAR VARCHAR
Fixed-length character type Variable-length character type
Useful for values with consistent length Useful for values with varying length
Example: CHAR(1) Example: VARCHAR(100)
Often used for short fixed-format values Commonly used for names and other text

13. TEXT Data Type

TEXT is used in systems such as MySQL for storing variable-length text.

It can be useful for content such as:

  • Descriptions
  • Comments
  • Articles
  • Long messages
description TEXT

Different database systems provide different text types and size limits.

14. DATE Data Type

DATE is used to store calendar dates.

admission_date DATE

Example:

2026-09-20

A DATE value does not represent a time of day.

15. TIME Data Type

TIME is used to represent a time of day or time interval according to the database system.

class_time TIME

Example:

09:30:00

16. DATETIME Data Type

DATETIME stores both date and time in systems that support this type, such as MySQL.

created_at DATETIME

Example:

2026-09-20 10:30:00

It is useful for timestamps such as record creation or update times.

17. TIMESTAMP Data Type

TIMESTAMP is a date-and-time type provided by several database systems. Its exact behavior differs between systems.

In MySQL, TIMESTAMP can be useful for tracking date and time values and can support automatic timestamp behavior in suitable table definitions.

created_at TIMESTAMP

18. YEAR Data Type

Some database systems provide a YEAR data type. MySQL supports YEAR for storing year values.

passing_year YEAR

Example:

2026

19. BOOLEAN / Boolean Values

A Boolean value represents a logical state such as true or false.

Database systems handle Boolean types differently. For example, MySQL supports BOOLEAN as a synonym for TINYINT(1).

is_active BOOLEAN

An application may use such a column to represent whether a record is active.

20. BINARY Data Types

Binary data types are used to store binary values rather than ordinary text.

Depending on the database system, binary types can be used for data such as:

  • Binary identifiers
  • Encoded data
  • Raw binary information

The exact binary data types vary between database systems.

21. BLOB Data Type

BLOB stands for Binary Large Object. In systems such as MySQL, BLOB types can store binary data such as images or other files.

Different BLOB variants support different maximum sizes.

profile_image BLOB
Best Practice: For many web applications, large files are stored in dedicated file or object storage while the database stores a path, URL, or identifier.

22. ENUM Data Type

Some database systems provide an ENUM type. MySQL supports ENUM for columns whose values must come from a predefined list.

Example:

status ENUM('Pending', 'Paid', 'Cancelled')

This can be useful for a small, fixed set of allowed values, although the best design depends on the application and database system.

23. JSON Data Type

Modern relational database systems may provide a JSON data type for storing JSON documents.

For example, MySQL and PostgreSQL provide JSON-related functionality.

preferences JSON

JSON can be useful when data has a flexible or nested structure, although normal relational columns are often preferable for data that needs frequent relational querying.

24. Choosing the Correct Data Type

Choose a data type according to the kind of data the column needs to store.

Data Possible Type
Student ID INT
Name VARCHAR
Description TEXT
Age INT
Fee DECIMAL
Admission Date DATE
Created Date and Time DATETIME / TIMESTAMP
Active Status BOOLEAN or database-specific equivalent

25. Example of a Student Table

Here is an example of a student table using different data types:

CREATE TABLE students (

    id INT PRIMARY KEY,

    name VARCHAR(100),

    age INT,

    fee DECIMAL(10,2),

    admission_date DATE,

    is_active BOOLEAN

);

Each column uses a data type appropriate for the kind of value it is expected to store.

26. DECIMAL Precision and Scale

DECIMAL is often written using precision and scale.

DECIMAL(10,2)

Here:

  • 10 = total number of digits allowed
  • 2 = number of digits after the decimal point

For example, values such as 1250.50 can be represented with this definition.

27. VARCHAR Length

VARCHAR can be defined with a maximum length.

name VARCHAR(100)

This definition specifies a maximum length of 100 characters according to the database system's rules.

The appropriate length should be chosen based on the expected data.

28. Data Type Conversion

Sometimes a value needs to be converted from one data type to another. SQL systems provide conversion or casting functions for this purpose.

For example, in MySQL:

SELECT CAST('100' AS UNSIGNED);

The exact syntax for type conversion differs between database systems.

29. Common Data Type Mistakes

Beginners commonly make mistakes such as:

  • Using text types for values that should be numeric.
  • Using FLOAT for money when exact decimal values are required.
  • Choosing an unnecessarily large data type.
  • Choosing a text length without considering the expected data.
  • Confusing DATE with DATETIME.
  • Assuming the same data types behave identically in every RDBMS.

30. Complete Example

The following table demonstrates several commonly used SQL data types.

CREATE TABLE employees (

    employee_id INT PRIMARY KEY,

    employee_name VARCHAR(100),

    salary DECIMAL(10,2),

    joining_date DATE,

    joining_time TIME,

    created_at DATETIME,

    is_active BOOLEAN

);

The exact behavior of some types, especially BOOLEAN, DATETIME, and auto-generated timestamp features, depends on the database system.

📌 Key Points

  • Data types define what kind of values a column can store.
  • INT is commonly used for whole numbers.
  • DECIMAL is useful for exact numeric values such as prices and fees.
  • FLOAT and DOUBLE are floating-point types for approximate numeric values.
  • CHAR stores fixed-length character data.
  • VARCHAR stores variable-length character data.
  • TEXT is useful for larger text values in supported database systems.
  • DATE stores calendar dates.
  • TIME stores time values.
  • DATETIME and TIMESTAMP can represent date and time.
  • Database systems can provide additional types such as JSON, BLOB, ENUM, and others.
  • Always choose data types according to the actual data and database system being used.

🧠 Quick Quiz

Question: Which SQL data type is commonly preferred for storing exact monetary values such as fees and prices?