CHAPTER 05
Beginner
SQL Data Types Explained | INT, VARCHAR, DATE, BOOLEAN
Updated: May 16, 2026
15 min read
# CHAPTER 5
SQL Data Types Explained
1. Introduction
If you tell a human, "My age is 25," the human understands it. If you tell a database, "My age is twenty-five," the database will crash if theage column is mathematically configured to only accept whole numbers. In SQL, you must explicitly declare what kind of data lives in every single column. This strictness is exactly why databases are lightning-fast and structurally indestructible. In this chapter, we will master Data Types, ensuring our databases are optimized for both speed and storage.
2. Learning Objectives
By the end of this chapter, you will be able to:- Define strict data types for table columns.
-
Choose between numerical types (
INT,FLOAT,DECIMAL).
-
Choose between text types (
VARCHAR,TEXT).
-
Handle chronological data (
DATE,DATETIME).
-
Implement logical true/false constraints (
BOOLEAN).
3. Numeric Data Types
Numbers are divided into two categories: Whole numbers and Decimal numbers.-
INT(Integer): Used for whole numbers (e.g.,1,500,-42). Perfect for IDs, quantities, or age.
quantity INT
-
FLOAT/DOUBLE: Used for approximate fractional numbers (e.g.,3.14159). Great for scientific measurements, but terrible for money due to microscopic rounding errors.
-
DECIMAL(M, D): Used for *exact* fractional numbers. Mandatory for financial applications!
price DECIMAL(10, 2) means the number can have 10 digits total, with 2 digits after the decimal point (e.g., 99999999.99).
4. Text (String) Data Types
If you are storing words, sentences, or paragraphs, you use text types.-
VARCHAR(N)(Variable Character): The most common text type. Used for short text like names, emails, or passwords. You must specify the maximum length (N).
email VARCHAR(255) means it can hold up to 255 characters.
-
CHAR(N)(Fixed Character): Used when the text is *always* the exact same length, like a 2-letter state abbreviation (TX,NY). It is slightly faster than VARCHAR.
statecode CHAR(2)
-
TEXT: Used for massive blocks of text with no strict length limit. Perfect for blog post bodies or product descriptions.
blogcontent TEXT
5. Date and Time Data Types
Never store a date as aVARCHAR string like "Jan 5th 2023"! If you do, the database cannot mathematically sort the dates. You must use native chronological types.
-
DATE: Stores just the date (YYYY-MM-DD).
birthdate DATE (e.g., 1990-12-25)
-
DATETIME/TIMESTAMP: Stores both the date and the exact time (YYYY-MM-DD HH:MM:SS). Perfect for tracking when an order was placed.
createdat DATETIME (e.g., 2023-10-31 14:30:00)
6. Logical Data Types
-
BOOLEAN(orTINYINT(1)in MySQL): Used for True/False or Yes/No flags. Behind the scenes, the database actually stores it as a1(True) or a0(False).
is_active BOOLEAN
7. Mini Project: Designing an Optimized Table
Let's design a perfectly optimizedusers table for a web application.
sql
8. Common Mistakes
-
Using VARCHAR for Numbers: If you create a
phonenumber VARCHAR(15)column, that is actually correct! You don't do math on phone numbers, and they often contain dashes (555-1234). But if you create aprice VARCHAR(10)column, you can never ask the database "Find all products under $50", because strings cannot be mathematically compared!
-
Using INT for massive numbers: A standard
INTcaps out around 2.1 Billion. If you are tracking the national debt, you must use aBIGINT.
9. Best Practices
-
Minimize VARCHAR sizes: If you know a ZIP code is never longer than 10 characters, don't use
VARCHAR(255). UseVARCHAR(10). This saves massive amounts of server RAM when querying millions of rows.
10. Exercises
- 1. What data type should you use to store a user's short email address?
-
2.
Why is it architecturally incorrect to store financial currency in a
FLOATcolumn, and what should you use instead?
11. SQL Challenges
Write the SQL DDL command to create amovies table. It should have movieid (Whole Number), title (Short Text up to 150 chars), release_date (Chronological Date), and rating (Exact Decimal with 1 decimal place, e.g., 8.5).
sql
12. MCQ Quiz with Answers
Question 1
In a Relational Database, why is it mandatory to assign a strict Data Type to every column?
Question 2
If you are building a banking application, which data type MUST be used for the accountbalance column to prevent microscopic rounding errors?
13. Interview Questions
-
Q: Compare and contrast
VARCHARandCHAR. When would it be architecturally advantageous to useCHAR(2)instead ofVARCHAR(255)?
-
Q: A junior developer creates a table with
createdat VARCHAR(50)to store timestamps. Explain the severe technical limitations this will cause for the backend reporting team.
14. FAQs
Q: Can a table have multiple columns of the same data type? A: Absolutely! You can havefirstname VARCHAR(50), lastname VARCHAR(50), and city VARCHAR(50) all in the exact same table.
15. Summary
The structure of your database is now perfect. By explicitly definingINT for mathematical IDs, VARCHAR and TEXT for strings, DATE for chronology, and DECIMAL for precision finance, you guarantee that corrupted or invalid data can never enter your system.