Data Types in SQL
In SQL, data types define the kind of data a column can hold — like numbers, text, or logical values.
Choosing the right data type for each column is important to store and query your data efficiently, accurately.
Commonly Used SQL Data Types
| Type | Description | Example |
|---|---|---|
| INT | Whole numbers | 25, 100, -7 |
| VARCHAR(n) | Variable-length text | 'Alice', 'HR' |
| BOOLEAN | Logical true/false values | TRUE, FALSE |
| DATE | Calendar date | '2024-01-01' |
| FLOAT | Approximate decimal numbers | 3.14, -0.5 |
Other SQL Data Types
| Type | Description | Example |
|---|---|---|
| BIGINT | Very large integers | 9223372036854775807 |
| SMALLINT | Smaller range integers | -32768 to 32767 |
| DECIMAL(p,s) | Fixed-point numbers | 123.45, -99.99 |
| DOUBLE | Double-precision floating point | 3.14159265359 |
| CHAR(n) | Fixed-length text | 'Y', 'USA' |
| TEXT | Large blocks of text | 'This is a long note' |
| TIME | Time of day | '14:30:00' |
| DATETIME | Date and time | '2024-01-01 14:30:00' |
| TIMESTAMP | Date and time with timezone awareness | '2024-01-01 14:30:00' |
Note: Some databases (like SQLite) are more flexible, while others (like PostgreSQL, MySQL, SQL Server) are more strict with data types.
Note: Exact names and available types may vary slightly depending on the database system (e.g., MySQL, PostgreSQL).
Example: Defining a Table with Data Types
CREATE TABLE employees (
id INT,
name VARCHAR(50),
age INT,
department VARCHAR(50),
gender VARCHAR(10)
);
We’ll dive deeper into how to create tables in upcoming chapters. For now, this example shows how choosing the right data type helps define what kind of values each column can hold.