Data type in SQL
🧠 What are Data Types in SQL?
In SQL, data types define what kind of data can be stored in each column of a table—such as numbers, text, dates, or true/false values.
When you create a table, you must choose the correct data type for each column.
🔢 1. Numeric Data Types
Used to store numbers, like marks, price, quantity, etc.
| Data Type | Description | Example |
INT | Integer (whole numbers) | 100, -50 |
SMALLINT | Smaller whole numbers | 300 |
BIGINT | Very large whole numbers | 99999999999 |
DECIMAL(p,s) | Exact decimal number, p = total digits, s = after decimal | DECIMAL(5,2) → 123.45 |
NUMERIC(p,s) | Same as DECIMAL | NUMERIC(6,2) → 99.99 |
FLOAT | Approximate decimal number | 3.14 |
REAL | Less precision than FLOAT | 1.23 |
📝 2. Character/String Data Types
Used to store text like names, addresses, etc.
| Data Type | Description | Example |
CHAR(n) | Fixed length string, always uses n spaces | CHAR(5) → 'Hi ' |
VARCHAR(n) | Variable length string (up to n characters) | VARCHAR(10) → 'Hello' |
TEXT | Large amount of text | 'This is a comment' |
📅 3. Date and Time Data Types
Used to store date and/or time.
| Data Type | Description | Example |
DATE | Only date | '2025-08-03' |
TIME | Only time | '14:30:00' |
DATETIME | Both date and time | '2025-08-03 14:30:00' |
TIMESTAMP | Like DATETIME, used in logs | '2025-08-03 14:30:00' |
✅ 4. Boolean Data Type
Stores true/false (yes/no) values.
| Data Type | Description | Example |
BOOLEAN | TRUE (1) or FALSE (0) | TRUE |
🧪 5. Other Data Types (Special Use)
| Data Type | Description | Example |
BLOB | Binary Large Object (files, images) | (used to store files) |
UUID | Unique identifier | '550e8400-e29b...' |
JSON | JSON formatted data | '{"name": "Amit"}' |
🧾 Example Table With Data Types
Here’s an example of a table that uses different types of data:
CREATE TABLE Students (
StudentID INT PRIMARY KEY, -- number (whole)
Name VARCHAR(50), -- text
DOB DATE, -- date
Score DECIMAL(5,2), -- number with decimal
IsActive BOOLEAN -- true/false
);
This table will store:
StudentID: Unique number for each studentName: Up to 50 charactersDOB: Date of birthScore: Like marks (e.g., 88.75)IsActive: Whether the student is active or not (TRUE/FALSE)
📌 Quick Summary Table
| Category | Data Types |
| Numeric | INT, DECIMAL, FLOAT, BIGINT, SMALLINT |
| Text | CHAR, VARCHAR, TEXT |
| Date/Time | DATE, TIME, DATETIME, TIMESTAMP |
| Boolean | BOOLEAN |
| Others | JSON, UUID, BLOB |
✅ INSERT Commands (Add Rows)
Here are a few examples inserting different students:
INSERT INTO Students (StudentID, Name, DOB, Score, IsActive)
VALUES (1, 'Amit Singh', '2002-05-10', 89.75, TRUE);
INSERT INTO Students (StudentID, Name, DOB, Score, IsActive)
VALUES (2, 'Priya Sharma', '2001-11-22', 92.50, TRUE);
INSERT INTO Students (StudentID, Name, DOB, Score, IsActive)
VALUES (3, 'Rahul Mehta', '2003-03-15', 76.00, FALSE);
INSERT INTO Students (StudentID, Name, DOB, Score, IsActive)
VALUES (4, 'Anjali Verma', '2002-08-01', 84.25, TRUE);