Data Types
Choose the right data type for every column — INT, VARCHAR, DECIMAL, DATE and the other common SQL Server types.
When you create a table you must tell SQL what kind of value each column will hold. Runs are whole numbers, names are text, a debut is a calendar date, a strike rate has decimals. Choosing the right data type keeps your data clean, makes queries faster and stops mistakes like someone typing 'seven hundred' into a runs column.
Definition
A data type is a rule attached to a column that defines which kind of values the column can store (numbers, text, dates…), how much space each value may take, and which operations make sense on it.
- Which kind of values — an
INTcolumn rejects'Virat'; aDATEcolumn rejects'hello'. - How much space —
VARCHAR(50)allows at most 50 characters;DECIMAL(5,2)allows at most 5 digits. - Which operations — you can add two
INTs, find the year of aDATE, or takeLEFT()of aVARCHAR.
Every column must have a data type — you cannot create a column without one in SQL Server.
The 4 types that cover most IPL data
| Data type | Stores | IPL example |
|---|---|---|
INT | Whole numbers (no decimal point), about ±2.1 billion | runs = 750 |
VARCHAR(50) | Text of variable length, up to 50 characters | name = 'Virat Kohli' |
DECIMAL(5,2) | Exact numbers with decimals | strike_rate = 139.50 |
DATE | Calendar dates (year, month, day) | debut = '2008-04-18' |
Understanding the numbers in brackets
VARCHAR(n) — n is the maximum number of characters. VARCHAR(10) fits 'RCB' (3 characters) and only uses space for those 3 characters — that is the "variable" in VARCHAR. In SQL Server, trying to store more than n characters raises the error "String or binary data would be truncated."
DECIMAL(p, s) — two numbers:
| Part | Name | Meaning in DECIMAL(5,2) |
|---|---|---|
p | precision | total number of digits = 5 |
s | scale | digits after the decimal point = 2 |
So DECIMAL(5,2) can store values from -999.99 to 999.99. 139.50 fits (3 digits before, 2 after). 1395.00 does not fit (4 digits before the point).
Other common SQL Server data types
| Category | Type | Use it for |
|---|---|---|
| Whole numbers | TINYINT (0–255), SMALLINT, INT, BIGINT | small counters → huge IDs |
| Exact decimals | DECIMAL(p,s) / NUMERIC(p,s) | money, averages, strike rates |
| Approximate decimals | FLOAT, REAL | scientific values (not money — tiny rounding errors) |
| Fixed-length text | CHAR(n) | codes that are always the same length, e.g. CHAR(3) for 'RCB', 'CSK' |
| Variable-length text | VARCHAR(n), VARCHAR(MAX) | names, cities, long descriptions |
| Unicode text | NVARCHAR(n), NCHAR(n) | text in other scripts, e.g. Hindi 'विराट कोहली' |
| Date & time | DATE, TIME, DATETIME2, DATETIME | debut dates, match start times |
| True / false | BIT (0 or 1) | is_captain, is_overseas |
Tip:
CHAR(10)always uses 10 characters of space and pads short values with spaces.VARCHAR(10)only uses what it needs. For names, always preferVARCHAR.
Example — a table using all four types
sql
CREATE TABLE player_stats (
id INT,
name VARCHAR(50),
debut DATE,
strike_rate DECIMAL(5,2)
);
INSERT INTO player_stats (id, name, debut, strike_rate)
VALUES (1, 'Virat Kohli', '2008-04-18', 139.50);
SELECT * FROM player_stats;Result in SQL Server:
| id | name | debut | strike_rate |
|---|---|---|---|
| 1 | Virat Kohli | 2008-04-18 | 139.50 |
How it works: '2008-04-18' is written as text in the 'YYYY-MM-DD' format and SQL Server converts it into a real DATE. Because strike_rate is DECIMAL(5,2), SQL Server always shows exactly 2 decimal places.
Note (playground): The browser playground runs on SQLite, which is relaxed about types. It shows
139.5instead of139.50, and it does not reject a 60-character name in aVARCHAR(50)column. Real SQL Server is strict — trust the rules on this page.
Choosing the right type — rules of thumb
- Numbers you calculate with → numeric types (
INT,DECIMAL). Never store runs as text —'90' > '100'is TRUE in text comparison! - Numbers you never calculate with (phone numbers, PIN codes) → text (
VARCHAR) — they can have leading zeros and+91. - Dates →
DATE/DATETIME2, neverVARCHAR— otherwise you loseYEAR(),DATEDIFF()and correct sorting. - Money / exact decimals →
DECIMAL, notFLOAT. - Size it sensibly —
VARCHAR(50)for names, notVARCHAR(MAX)for everything.
Common mistakes
- ❌
debut VARCHAR(20)✅debut DATE— text dates sort wrongly and can't use date functions. - ❌
strike_rate INT✅strike_rate DECIMAL(5,2)—INTwould cut 139.50 down to 139. - ❌
DECIMAL(3,2)for a strike rate ✅DECIMAL(5,2)—DECIMAL(3,2)only allows values up to 9.99. - ❌
VARCHARwith no length in SQL Server — insideCREATE TABLEit silently meansVARCHAR(1)! ✅ Always give a length.
SQL Server vs MySQL / PostgreSQL
| Idea | SQL Server | MySQL | PostgreSQL |
|---|---|---|---|
| Unlimited text | VARCHAR(MAX) | TEXT / LONGTEXT | TEXT |
| True/false | BIT | BOOLEAN (alias of TINYINT(1)) | BOOLEAN |
| Date + time | DATETIME2 | DATETIME | TIMESTAMP |
| Unicode text | NVARCHAR | VARCHAR with utf8mb4 | VARCHAR (UTF-8 by default) |
Interview tip
Q: What is the difference between CHAR and VARCHAR?
A: CHAR(n) is fixed-length — it always uses n characters of storage and pads shorter values with spaces. VARCHAR(n) is variable-length — it stores only the characters you actually give (up to n) plus a small length overhead. Use CHAR for values that are always the same length (like 3-letter team codes) and VARCHAR for everything else.
Key points to remember
- A data type decides what kind of values a column can hold, how much space they take and which operations work on them.
- INT stores whole numbers, VARCHAR(n) stores text up to n characters, DECIMAL(p,s) stores exact decimals, DATE stores calendar dates.
- In DECIMAL(p,s), p (precision) = total digits and s (scale) = digits after the decimal point; DECIMAL(5,2) holds up to 999.99.
- CHAR(n) is fixed length and pads with spaces; VARCHAR(n) uses only the space it needs.
- Use NVARCHAR for Unicode text (e.g. Hindi), BIT for true/false, DATETIME2 for date + time in SQL Server.
- Never store numbers or dates as text — you lose correct sorting and the matching functions.
- Use DECIMAL, not FLOAT, for money and exact values.
Try it yourself
A table with INT, VARCHAR, DATE and DECIMAL
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Why the right type matters: DATE functions work
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Text vs numbers: '90' is bigger than '100' as text!
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Practice: design a match_scores table
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Revision Flashcards
Tap a card to flip it and see the answer.
