SQL Data Types: A Beginner’s Guide

SQL Data Types: A Beginner’s Guide
Tutorial Playlist

The SQL data type is the fundamental part of SQL, which is used to design an efficient, scalable, and high-performance database. Correct data types will optimize the storage and also secure the data integrity, which increases the performance of the query. In this article, you will learn about what the data types in SQL are along with their performance, use cases, and functionality.

 

Table of Contents:

What Are SQL Data Types?

There are different types of data types that can be used in a database table. Each data type will define a data type in a column. Data types can be used based on the types of databases the user is using like MySQL, PostgreSQL, SQL Server, and Oracle. Each database has its own set of supported data types, but generally, all the data types will perform similar functionality. 

 

Example:

To mention a person’s name in a column, a user can use VARCHAR or TEXT to mention the type of data. For the salary or any numerical data that has to be used, the user can use INT to mention that. 

Different Data Types in SQL

There are different types of data types in SQL that can be used in a table for easier optimization. 

1. Numeric Data Types in SQL 

Numeric Data Types have TINYINT, SMALLINT, INT, BIGINT, DECIMAL, and FLOAT. 

 

TINYINT Data Type:

The TINYINT is used to store very small integers or numbers in a column that will use only 1 byte of data in storage. The maximum value it can hold is 127, and 255 if it is unsigned. The TINYINT can be used if the user already knows that the range of values in a table is short. Then the user can use TINYINT for those short codes.

 

SMALLINT Data Type:

When the user needs to code a bit more than the range for TINYINT, like more than 127. Then they can use the SMALLINT data type. This uses 2 bytes of data and will store values up to 32,767 or can hold up to 65,535 unsigned characters. 

 

Example: It can be used for fields like age or any stock management where it requires the quantity of product. These fields will not require more characters. 

 

INT Data Type:

INT is the most basic and frequently used data type in most of the applications. It uses storage of 4 bytes and can hold the value up to 2,147,483,647, or if it has unsigned characters, then it can hold more than 4 billion values. 

 

Example: It is mostly used in the field of using IDs, or any salaries, or any numerical field in an application. 

 

BIGINT Data Type:

If the user wants to store very large integer values that are more than what INT can hold, then the user can use the BIGINT data type. It has storage of 8 bytes, and it can hold values up to a maximum of 9.2 quintillion. 

 

Example: It can be used to store or in the fields of transactions and also log entries, where the data will be streamed in high volumes or in large systems to identify unique values. 

 

DECIMAL(p,s) Data Type:

The Decimal data type is used in transactions, financial data or monetary applications. It is used to calculate the numbers correctly to a fixed-point number. p in the decimal is for the total number of digits, and s in the decimal is for the number of digits after the decimal point. 

 

Example: In financial accounting there will be some value, 22.345. These values will be mentioned with the help of decimals in a database and precisely by DECIMAL (5, 3), which will store the value in the database efficiently. 

 

FLOAT Data Type:

The FLOAT data type can be used when the application needs to store values like scientific or decimal values with more floating points. If there is a large decimal value in a record, this FLOAT data type will tolerate some loss of precision. 

 

Example: During scientific value measurements or calculations of large transactional data, this FLOAT data type will give correct values with less loss, like the pi value 3.14159.

Best Practices of Numeric Data Type in SQL

  • If the user wants to include decimal values or any financial statement and wants precise values, then use DECIMAL instead of FLOAT. 
  • The user can use data types like TINYINT or SMALLINT instead of INT to store age or any values within a small range like age or phone number. With this method they can save storage in the database. 

2. String Data Types in SQL

The String data type is used to store the strings in a database. There are many types of String data type like CHAR, VARCHAR, TEXT, NCHAR, and NVARCHAR. 

 

CHAR(n):

The CHAR is a fixed-length character data type that is used when the number of characters in a table is always specified. It can only be used when the user knows the exact amount of characters. Then they can use CHAR and mention the character length. 

 

Example: if the user wants to define a value by mentioning CHAR(3) to store the value “Hello,” then the system will check the number of characters, and then the character length will be limited to  exactly 3. The “Hello” will become “Hel,” as the 5-character word, and the system can only take up to 3 characters. It can be preferred to use for country codes or any abbreviations. 

 

VARCHAR(n) Data Type:

VARCHAR(n) is a variable-length character data type that will store only the characters that the user inputs in the system. Unlike CHAR, this VARCHAR only counts the number of characters, not the spaces in between. It can only store the value up to the maximum of n. This maximizes the storage and is very efficient for storing character strings of different lengths.

 

Example: VARCHAR(10) will store any values of string from 0 to 10 characters. It can be used for storing addresses, emails, names and any description of text string values. 

 

TEXT Data Type:

The TEXT data type is used to store large amounts of text data in a database. It can store more data than the limits of the CHAR and VARCHAR data types. It is designed and structured for large strings, but the usage of operations like indexing or full-text search will be more limited than standard string types. 

 

Example: It can be used when they need to store large paragraphs of text strings, article posts, or comments. 

 

NCHAR(n) Data Type:

To store a fixed length of Unicode characters, NCHAR(n) can be used instead of CHAR(n). The NCHAR(n) can store a wide range of strings, international symbols, and scripts in different languages like Chinese, Japanese, Arabic, Latin, or any special symbols. It can fill any empty spaces between the strings, and it can only be used when the values are the same length and those values support multilingual texts.

 

NVARCHAR(n) Data Type:

NVARCHAR(n) performs the same as VARCHAR(n) but supports UNICODE. It is very suitable for storing variable lengths of multilingual text. It will store spaces compared to NCHAR while storing data. It is very efficient for storing strings in various languages. 

 

Example: It can hold names or passages in different languages across the world or emojis without any data loss. 

Best Practices of String Data Type in SQL

  • If there is no fixed length for the character, use VARCHAR for better performance and efficiency instead of the CHAR data type.
  • If a user wants to store large text values like articles or comments, avoid using the TEXT data type. 

3. Date and Time Data Types in SQL

The Date and Time Data types are used to mention or store data in date or time format. 

 

DATE Data Type:

The DATE data type is used to store values in a date format like YYYY-MM-DD without the use of time characters. As for time to be included, the user needs to add a time component to the query. The DATE data type is used for fields like registration date, date of birth, deadlines, or log information where the date plays a major role.

Example: The values like 2025-08-26  will be defined as 26th August, 2025. 

 

TIME Data Type:

The TIME data type will only store time values in the database without any date. The format for storing the time alone in a database is HH:MM:SS. It is useful when the user wants to store values like event time or to calculate the working hours of a single day. 

Example: If the event time is 13:25:00, then it is considered as 1:25 pm 

 

DATETIME Data Type:

The DATETIME data type is the data type that includes both date and time in a single value. This will let the user get both time and date timestamps for the event. The format to store this is YYYY-MM-DD HH:MM:SS. 

Example: It is used to capture the exact time stamp for events like log in time and date or to schedule a mail or events like 2025-03-25 16:35:09 this will be defined as 25th march, 2025, at 4:35 pm.

 

TIMESTAMP Data Type:

The TIMESTAMP stores the data of time and date in unix timestamp format. Where numeric value will represent the seconds in UTC. IT is usually used in a place where automatic time tracking is needed in different time zones.

Best Practices of Date and Time Data Types in SQL

  • TIMESTAMP can be used in MySQL as CURRENT_TIMESTAMP for auto-updating the time. 
  • The VARCHAR data type can be used to store date/time. Instead, use the DATETIME data type for efficient functions. 

4. Boolean Data Type in SQL

The Boolean data type can be used to store logical values in either true or false. In the system it will be considered as 0 or 1. As 1 is for TRUE  and 0 is for FALSE. This is used in the fields of flags or switches, which indicate whether the user is active or inactive. Boolean is easier to read and understand, but MySQL will treat the boolean as TINYINT(1) or BIT(1), which means the database will consider them as small integers, like digit 1 for true and digit 0 for false, instead of considering them as true or false. 

Best Practices of Boolean Data Type in SQL

  • Avoid using CHAR or VARCHAR to store the boolean values. 

5. Special Data Types in SQL

There are different types of special data types. 

 

BLOB Data Type:

BLOB is abbreviated as Binary Large Object. It is usually used to store images, binary data, audio, video, or any kind of files. This data is not used for plain text. Instead, it will be stored as raw binary format. In applications it can be used to store media files efficiently in the database. The BLOB needs special tools or functions, but they are better with a file storage system with reference to a database. 

 

JSON Data Type:

JSON is a data type that is used to store data in JavaScript Object Notation (JSON) format. It stores and structures the data in key-value pairs. It is very flexible and can use nested data. It can be modified to store data as per user preferences, settings, or lists. It will be helpful when the user needs to deal with APIs or applications that will change data between records. 

Example: {“name”: “Kiran”, “age”: 34} is in a JSON format of an object. 

 

UUID Data Type

UUID stands for Universally Unique Identifier. It will store the value in a unique 128-bit format like “550h2-043300-e29b-41sd6-a716-446632484408940.” It is used as a unique identifier for the records, mainly when there are auto-increment IDs. UUID will give a unique ID to all the records across the database, which is helpful when the user needs to find the records, and it is good for tokens and public identifiers. 

Best Practices for Special Data Types in SQL

  • The user should avoid complex queries on JSON. Use semi-structured data in JSON. 
  • Store the records in file paths in the system instead of using BLOB to reduce the size of the database. 
  • Make sure you validate the JSON format in the application layer before inserting it in the database.
  • Use relational columns for predefined structures and use JSON only if the data is dynamic, which means the data will keep changing.

Features Comparison of SQL Data Types

Category Common Data Types Features
1. Numeric INT, BIGINT, SMALLINT, DECIMAL, FLOAT Store numerical values. Use INT to represent integers and DECIMAL or FLOAT to represent decimal numbers.
2. String VARCHAR(n), CHAR(n), TEXT Store character strings. VARCHAR stores variable-length character strings, CHAR stores fixed-length character strings, and TEXT stores large character strings.
3. Date and Time DATE, TIME, DATETIME, TIMESTAMP Store date or date and time values. Useful for logging and scheduling.
4. Boolean BOOLEAN, BIT The data type will store logical values of TRUE or FALSE. Some databases store those values as 0/1.
5. Special JSON, XML, BLOB, GEOGRAPHY, UUID For semi-structured or binary data, geospatial data, or globally unique identifiers (GUIDs).

 

SQL Datatype Type Conversion and Casting

When the user works with data, they need to change the format or type of data they are using based on the operations they are working on. Understanding data types, type conversions and casting will help the user to work efficiently.

Type Conversion in SQL

Type conversion in SQL is defined as changing a value from one data type to another data type. SQL has 2 type of conversions 

  • Implicit conversion 
  • Explicit conversion

Implicit Conversion in SQL

Implicit conversion in SQL will convert the data types automatically whenever the data in the server is safe to convert. 

 

Example: 

SELECT 100 + '200'; 

Explanation: The implicit conversion converts the ‘200’ into an integer automatically before performing it in addition. But PostgreSQL will not allow implicit conversion in their server. 

Explicit Conversion in SQL

Explicit (casting) conversion in SQL is the process of converting the data type manually in a system.  

 

Examples of usage:

CAST(expression AS target_type)

 

In SQL server only

CONVERT(target_type, expression) 

 

In PostgreSQL

expression::target_type 

 

  1. Using CAST() function in SQL
--To convert string to float
SELECT CAST('123.45' AS FLOAT);
--To convert integer to string   
SELECT CAST(10 AS VARCHAR);
--This will trim time in SQL server
SELECT CAST(GETDATE() AS DATE);      

 

  1. Using CONVERT() function in SQL Server 
SELECT CONVERT(VARCHAR, 334);         
SELECT CONVERT(DATE, GETDATE());    

 

Explanation: The VARCHAR 334 will convert the variable into an integer and result in ‘334’ only. GETDATE will convert the datetime into only the date. 

 

  1. Using :: function in SQL Server
SELECT '2024-12-01'::DATE;
SELECT 123.456::TEXT;
SELECT NOW()::DATE;

Why is casting important in SQL?

  1. To format the data like convert numbers to strings 
  2. To apply date filters, the casting will convert text to dates. 
  3. It will prevent unexpected errors by converting it into the correct data type. 

 

Real-World Examples For Type Casting and Conversion in SQL

Casting order_date from Text to Date – to filter the data from 2023. 

 

Example:

CREATE TABLE orders (
    order_id INT PRIMARY KEY,
    customer_name VARCHAR(100),
    order_date VARCHAR(20)  -- Storing date as text (not ideal)
);
INSERT INTO orders (order_id, customer_name, order_date) VALUES
(1, 'Gaurav', '2022-12-15'),
(2, 'Hari', '2023-01-10'),
(3, 'Kisan', '2023-02-05'),
(4, 'Jahir', '2022-11-30'),
(5, 'Eve', '2023-03-21');
SELECT *
FROM orders
WHERE CAST(order_date AS DATE) >= '2023-01-01';

 

Output:

Data_type_1

Explanation: Here, the CAST() function converted the order_date from VARCHAR to DATE. 

 

Converting DATETIME to DATE – To get only the date. 

 

CREATE TABLE sample_data (
    id INT PRIMARY KEY,
    number_value INT,
    datetime_value DATETIME
);
INSERT INTO sample_data (id, number_value, datetime_value) VALUES
(1, 456, GETDATE()),
(2, 789, '2024-12-31 15:30:00');
-- Convert DATETIME to DATE (removes time part)
SELECT id, CONVERT(DATE, datetime_value) AS date_only
FROM sample_data;

Output:

Data_type_2

Explanation: here, the convert() function converted DATETIME to only date from the given data. 

Common Mistakes of SQL Data Types 

Data Type Mistakes Fix
FLOAT Data Type Using FLOAT for cost (transactions). Rounding off money will give an accurate error Users can fix this with the DECIMAL(p,s) data type.
VARCHAR(255) data type Using the VARCHAR (255) data type. This will create unnecessary storage in the database for smaller records. Use appropriate VARCHAR length based on the record size.
TEXT Data Type Users will sometimes use the TEXT data type for smaller text. Can use VARCHAR for smaller text.
VARCHAR Data Type for dates. Do not use VARCHAR for storing dates in the database. This will make the queries slow. Can use DATE or DATETIME data types for efficient performance.

Conclusion

SQL data types play a major role in defining the database. Using the correct data type for the records will make the query efficient, scalable, and high-performing. The data type will ensure data integrity. By learning these data types like numeric, string, date/time, boolean, Varchar, or special data types like JSON and BLOB, users will be able to design a smart, scalable, and storage-efficient database schema. By avoiding mistakes and using the right data type for the right records, it will make it so users can design a database that is perfect and reliable. 

 

Take your skills to the next level by enrolling in the SQL Training Course today and gaining hands-on experience. Also, prepare for job interviews with SQL interview questions prepared by industry experts.

SQL Data Types: A Beginner’s Guide – FAQs

Our SQL Courses Duration and Fees

Program Name
Start Date
Fees
Cohort Starts on: 15th Apr 2025
₹15,048
Cohort Starts on: 22nd Apr 2025
₹15,048

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.