Data Types in SQL with Examples

Data types in SQL define the kind of data that a column, variable, or other database object can store and process. In simple terms, a data type tells the database what type of value can be stored.

SQL databases can store many kinds of information, such as names, numbers, dates, prices, email addresses, and other business data. However, different types of data require appropriate data types.

For example, a person’s name is text, so a character or string data type VARCHAR can be used. Similarly, a person’s age is normally represented as a whole number, so an integer data type INT can be used.

When creating a database table, we assign an appropriate SQL data type to each column. This helps maintain data accuracy, consistency, storage efficiency, and reliable database operations.

Why Does a Database Need Data Types?


Imagine a database column that stores student ages. The database should normally expect numeric values such as 18, 20, or 25. It would not make sense to store a text value such as “Twenty years old” in a column intended for age.

By defining a suitable data type, the database can apply appropriate rules for storing and processing the data.

Data Type vs. Actual Value

It is important to understand the difference between a data type and a value.

  • Data type: Defines the type of values that can be stored.
  • Value: The actual data stored in the database.

For example:

ConceptExample
Data TypeINT
Value25

Practical SQL Example: Defining Column Data Types


The following example creates a table with different SQL data types:

CREATE TABLE students (
    student_id INT,
    student_name VARCHAR(100),
    age INT,
    percentage DECIMAL(5, 2),
    date_of_birth DATE
);

In this example:

  • CREATE TABLE students creates a new table named students.
  • student_id INT defines a column for storing integer values.
  • student_name VARCHAR(100) stores variable-length character text up to a maximum of 100 characters.
  • age INT defines a column for storing a student’s age as a whole number.
  • percentage DECIMAL(5, 2) stores an exact fixed-point decimal value with 5 total digits of precision and 2 decimal places (up to 999.99).
  • date_of_birth DATE stores a calendar date (YYYY-MM-DD) without a time component.

Real-World Analogy for SQL Data Types


Understanding SQL data types becomes easier when we compare a database with a well-organized storage system. Imagine a large warehouse that stores different types of items. The warehouse has separate labeled sections:

  • Numbers section for quantities and counts.
  • Text section for names and descriptions.
  • Date section for important dates.
  • Decimal section for prices and financial values.
  • Yes/No section for true or false information.

Each section accepts only the appropriate type of item. Similarly, an SQL database uses data types to organize information correctly.

Database Storage Analogy

Imagine a student database:

Database ColumnReal-World InformationAppropriate Data Type
student_idStudent identification numberINT
student_nameStudent’s nameVARCHAR
ageStudent’s ageINT
percentageExam percentageDECIMAL
date_of_birthStudent’s birth dateDATE
is_activeActive or inactive statusBOOLEAN

Each column has a specific purpose and therefore requires an appropriate data type.

Why This Analogy Is Useful

Suppose you accidentally place a date in a storage box designed for numbers. The information becomes difficult to organize and use correctly.

The same problem can occur in a database. For example, if you store a product price as plain text instead of a numeric data type, the database may not handle calculations correctly.

If you choose a proper SQL data type, it helps the database:

  • Store data in an appropriate format.
  • Validate the type of data being entered.
  • Perform calculations and comparisons correctly.
  • Organize information consistently.
  • Process data efficiently.

Syntax of SQL Data Types


In SQL, you define a data type when you create a column. The basic syntax is:

column_name DATA_TYPE;

A table usually contains multiple columns, and each column can have its own data type:

CREATE TABLE table_name (
    column_name1 DATA_TYPE,
    column_name2 DATA_TYPE,
    column_name3 DATA_TYPE
);

Here:

  • CREATE TABLE creates a new table.
  • table_name specifies the name of the table.
  • column_name specifies the name of a column.
  • DATA_TYPE specifies the type of data the column can store.

Simple Example

CREATE TABLE students (
    student_id INT,
    student_name VARCHAR(100),
    age INT,
    date_of_birth DATE
);

In this example:

  • student_id INT stores the ID of students as a whole number.
  • student_name VARCHAR(100) stores text with a maximum length of 100 characters.
  • age INT stores the student’s age as an integer value.
  • date_of_birth DATE stores a date.

Types of Data Types in SQL


In relational database systems, SQL data types are classified into several core categories based on the type of the data they store:

  • Numeric Data Types
  • Character and String Data Types
  • Date and Time Data Types
  • Boolean Data Types
  • Binary Data Types
  • Other Database-Specific Data Types

Let’s explore each category in detail.

Comprehensive hierarchy diagram of SQL data types showing Numeric, Character, Date and Time, Boolean, Binary, and Database-Specific categories

1. Numeric Data Types in SQL


Numeric data types in SQL are used to store numerical values in a database. These values can be whole numbers, decimal numbers, exact values, or approximate values. You use numeric data types for information such as age, quantity, product price, salary, etc.

Types of Numeric Data Types

Numeric data types in SQL are commonly divided into two major groups:

1. Exact Numeric SQL Data Types

Exact numeric data types represent numeric values exactly within their defined range or precision. They are useful when accuracy is essential.

Data TypeBrief ExplanationStorage SizeRange / Precision
SMALLINTStores relatively small integer values.2 bytes−32,768 to 32,767
INTEGER / INTStores integer values.4 bytes−2,147,483,648 to 2,147,483,647
BIGINTStores large integer values.8 bytes−9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
DECIMAL(p,s)Stores exact fixed-point numeric values. p specifies total precision and s specifies digits after the decimal point.DBMS-dependentDefined by p and s
NUMERIC(p,s)Stores exact numeric values with specified precision and scale.DBMS-dependentDefined by p and s

Important Note:

  • Storage sizes and exact ranges can vary between DBMSs.
  • The integer ranges shown above are the commonly used ranges for a signed 2-byte, 4-byte, and 8-byte integer type.
  • DECIMAL/NUMERIC precision and storage are DBMS-dependent.

2. Approximate Numeric SQL Data Types

Approximate numeric data types store numbers using floating-point or approximate precision. These types are useful for certain scientific calculations, measurements, and other applications where a small approximation may be acceptable.

Data TypeBrief ExplanationStorage SizeRange / Precision
REALStores approximate single-precision floating-point numbers.DBMS-dependentApproximately 6–7 decimal digits of precision; range is DBMS-dependent
FLOAT(p)Stores approximate floating-point numbers. The precision specification p determines the precision according to the DBMS.DBMS-dependentPrecision and range are DBMS-dependent
DOUBLE PRECISIONStores approximate double-precision floating-point numbers.DBMS-dependentApproximately 15–17 decimal digits of precision; range is DBMS-dependent

Important Note:

  • Approximate numeric types can introduce rounding errors because many decimal fractions cannot be represented exactly in binary floating-point format.
  • The exact storage size, precision, and range of REAL, FLOAT, and DOUBLE PRECISION vary among DBMSs.

2. Character and String Data Types in SQL


Character and string data types are used to store text and character-based information in a database. Some examples of text data include:

  • Names
  • Email addresses
  • Phone numbers
  • Addresses
  • Product names
  • Descriptions
  • Usernames
  • Comments

For example, the value “Rahul Kumar” is text, so it should normally be stored using a character or string data type rather than an integer type.

Main Character and String Data Types

Data TypeBrief DefinitionCommon UseExample
CHAR(n)Stores a fixed-length character string defined by n.Codes, fixed-length identifiers, country codes'IN'
VARCHAR(n)Stores a variable-length character string up to the specified maximum length.Names, emails, usernames, addresses'Rahul Kumar'
TEXTStores variable-length text, generally intended for larger amounts of text.Descriptions, comments, articles, messages'This is a product description.'

3. Date and Time Data Types in SQL


Date and time data types are used to store information about dates, times, or both dates and times in a database.

Data TypeBrief DefinitionCommon UseExample
DATEStores a calendar date without a time component.Birth dates, joining dates, holidays2026-08-31
TIMEStores a time of day without a date component.Opening times, appointment times, daily schedules14:30:00
TIMESTAMPStores both a date and a time.Orders, appointments, application events2026-08-31 14:30:00

4. Boolean Data Types in SQL


Boolean data types in SQL represent logical values such as TRUE and FALSE. They are useful when a database column needs to represent a condition or status with only two possible outcomes.

Common Uses of Boolean Values

Boolean data types are commonly used for:

  • User account active or inactive status
  • Email verified status
  • Completed or incomplete tasks
  • Product availability flags
  • Subscription status
  • Login permissions

Example:

CREATE TABLE students (
    student_id INT,
    student_name VARCHAR(100),
    is_active BOOLEAN
);

In this example, the is_active column can represent whether a student is active.

Insert Student Data:

INSERT INTO students
    (student_id, student_name, is_active)
VALUES
    (101, 'Rahul Kumar', TRUE),
    (102, 'Priya Sharma', FALSE);

Expected Output:

student_idstudent_nameis_active
101Rahul KumarTRUE
102Priya SharmaFALSE

5. Binary Data Types in SQL


Binary data types store data as a sequence of bytes rather than ordinary text or numeric values. They are commonly used for information such as:

  • Images
  • PDF documents
  • Audio files
  • Video files
  • Encrypted data
  • File attachments
  • Other raw binary content

Common Binary Data Types

Data TypeBrief DefinitionCommon UseExample
BINARY(n)Stores fixed-length binary data.Fixed-size binary values, binary hashesBinary bytes
VARBINARY(n)Stores variable-length binary data up to a specified limit.Small files, variable-size binary informationBinary bytes
BLOBStores large binary objects.Images, PDF documents, audio files, video filesImage or PDF data

6. Other Database-Specific SQL Data Types


In addition to common numeric, string, date/time, Boolean, and binary types, many database systems provide specialized data types for particular kinds of information.

These data types are often database-specific, meaning they may not be supported by every SQL database.

Common Database-Specific Data Types

Data TypeBrief DefinitionCommon UseExample
JSONStores structured JSON data in systems that support a JSON type.API responses, application settings, flexible data{"name": "Rahul", "age": 25}
XMLStores XML-formatted data in systems that support XML types.XML documents, enterprise data exchange<student><name>Rahul</name></student>
UUIDStores a universally unique identifier in systems that provide a UUID type.Unique record identifiers, distributed systems550e8400-e29b-41d4-a716-446655440000
ArrayStores multiple values in a single column in database systems that support arrays.Tags, lists, multiple related values{"SQL", "Java", "Python"}
Spatial / GeographicStores location and geometric data.Maps, delivery systems, geographic applicationsPoint, Line, Polygon
ENUMRestricts a value to one of a predefined set of values in systems that support it.Status, category, role'Active'
SETStores zero or more values from a predefined collection in systems that support it.Multiple selectable categoriesDatabase-specific

Important Note:

  • SQL data types are not implemented identically across all database management systems (DBMSs).
  • The available data types, storage requirements, value ranges, precision, and behavior can differ between systems such as MySQL, PostgreSQL, SQL Server, and Oracle.
  • Therefore, the examples and specifications in this article describe commonly used SQL data types and may vary depending on the DBMS.

Basic SQL Data Types Example


Now that you understand the major categories of SQL data types, let’s look at a simple example that uses several common data types in one table.

Example: Creating a Student Table

CREATE TABLE students (
    student_id INT,
    student_name VARCHAR(100),
    age SMALLINT,
    percentage DECIMAL(5, 2),
    date_of_birth DATE,
    is_active BOOLEAN
);

Explanation of Each Column

ColumnData TypePurpose
student_idINTStores the student’s ID as a whole number.
student_nameVARCHAR(100)Stores the student’s name as variable-length text.
ageSMALLINTStores the student’s age as an integer value.
percentageDECIMAL(5, 2)Stores the student’s percentage as an exact decimal value.
date_of_birthDATEStores the student’s date of birth.
is_activeBOOLEANStores whether the student is active or inactive.

Inserting Data into the Table

INSERT INTO students (
    student_id,
    student_name,
    age,
    percentage,
    date_of_birth,
    is_active
)
VALUES (
    101,
    'Saanvi Gupta',
    20,
    89.75,
    '2006-05-15',
    TRUE
);

Retrieving the Data

SELECT * FROM students;

Expected Output:

student_idstudent_nameagepercentagedate_of_birthis_active
101Saanvi Gupta2089.752006-05-15TRUE

 

DEEPAK GUPTA

DEEPAK GUPTA

Deepak Gupta is the Founder of Scientech Easy, a Full Stack Developer, and a passionate coding educator with 8+ years of professional experience in Java, Python, web development, and core computer science subjects. With strong expertise in full-stack development, he provides hands-on training in programming languages and in-demand technologies at the Scientech Easy Institute, Dhanbad.

He regularly publishes in-depth tutorials, practical coding examples, and high-quality learning resources for both beginners and working professionals. Every article is carefully researched, technically reviewed, and regularly updated to ensure accuracy, clarity, and real-world relevance, helping learners build job-ready skills with confidence.