Lesson 08 � Beginner

DATA TYPES:
SAHI TYPE, SAHI DATA.

Sahi data type choose karna zaroori hai � galat type se storage waste hoti hai ya data loss hota hai. Har column ka type define karna padta hai. Yeh SQL ki fundamental cheez hai jo har table banate waqt yaad rakhni padti hai.

? 18 min✓ Beginner✓ Prerequisite: Window Functions

WHY: Data types kyun important hain?

Socho ek company ka database hai � employee ka naam, salary, date of join � sab alag-alag types ka data hai. Agar tum salary ko TEXT type mein daal do toh calculations nahi ho payengi. Aur agar naam ko INT mein daal do toh data hi nahi aayega. Sahi data type choose karna = sahi storage + sahi queries.

INT

Poore numbers ke liye. Employee ID, age, quantity � jab decimal ki zaroorat na ho. -2 billion se 2 billion tak ka range hota hai INT mein.

VARCHAR

Variable length text. Naam, email, address � jitna space chahiye utna use hota hai. VARCHAR(50) matlab maximum 50 characters.

DECIMAL

Exact decimal numbers. Price, salary, percentage � jab precision chahiye. DECIMAL(10,2) matlab 10 digits total, 2 decimal places.

DATE/TIME

Dates aur timestamps ke liye. Joining date, order time, expiry � sab ke liye alag types hain. DATE sirf date, TIME sirf time, DATETIME dono.

HOW: Numeric types samjho

Numeric types mein teen categories hain � integers (poore numbers), floating point (approximate decimals), aur exact decimals. Har ka apna use case hai.

sql
-- Numeric types
CREATE TABLE numeric_demo (
 tiny_col TINYINT, -- -128 to 127
 small_col SMALLINT, -- -32768 to 32767
 med_col MEDIUMINT,
 int_col INT, -- -2B to 2B
 big_col BIGINT, -- Very large numbers
 float_col FLOAT, -- Approximate decimal
 double_col DOUBLE, -- More precise
 decimal_col DECIMAL(10,2) -- Exact: 12345678.90
);
Mental model: Integer types alag-alag size ke containers hain � TINYINT chhota box hai (127 tak), BIGINT bahut bada box hai. FLOAT approximation hai (2+2 = 3.9999 kabhi kabhi), DECIMAL exact hai (hamesha 4.00).

String types samjho

Text ke liye alag types hain. CHAR fixed length hai (hamesha pura space lega), VARCHAR variable hai (jitna chahiye utna). TEXT long content ke liye hai.

sql
-- String types
CREATE TABLE string_demo (
 char_col CHAR(10), -- Fixed length
 varchar_col VARCHAR(50), -- Variable length
 text_col TEXT, -- Long text
 blob_col BLOB -- Binary data
);
Pro tip: CHAR(10) mein "Hi" likho toh bhi 10 characters ka space lagega (8 spaces add honge). VARCHAR(10) mein "Hi" likho toh sirf 2 characters ka space lagega. Isliye variable length zyada efficient hai jab data ki length vary kare.

Date aur Time types

Dates ke liye alag types hain � sirf date chahiye toh DATE, sirf time chahiye toh TIME, dono chahiye toh DATETIME. TIMESTAMP timezone handle karta hai automatically.

sql
-- Date types
CREATE TABLE date_demo (
 date_col DATE, -- 2026-01-01
 time_col TIME, -- 14:30:00
 datetime_col DATETIME, -- 2026-01-01 14:30:00
 timestamp_col TIMESTAMP -- Auto timezone
);

Try it: SQL query likho

Neeche ka editor SQL jaisa hai. Yahan apni query likho aur "Run Query" dabao. Screen par sample results dikhenge � yeh browser-based simulation hai, real database nahi.

SQL playgroundData types se tables banao
Run Query dabayein

Quick check

Ek table banao with columns: name (VARCHAR), age (INT), salary (DECIMAL). Table ka naam demo rakho.

CREATE TABLE demo se shuru karo, parentheses mein columns likho � name VARCHAR(100), age INT, salary DECIMAL(10,2).

Common data type mistakes

  • Galat type choose karna: Salary ko VARCHAR mein mat daalo � numbers ko hamesha INT ya DECIMAL mein rakho taaki calculations ho sake.
  • SIZE ka dhyan na rakhna: VARCHAR(255) hamesha mat lagao � actual requirement dekho. Email ke liye VARCHAR(100) kaafi hai.
  • DECIMAL precision bhoolna: DECIMAL(10,2) ka matlab hai 10 digits total, 2 decimal places. Price ke liye yeh sahi hai, percentage ke liye alag chahiye.
  • DATE vs DATETIME confusion: Sirf date chahiye toh DATE use karo, time bhi chahiye toh DATETIME. TIMESTAMP alag hai � yeh timezone handle karta hai.
Data Types complete?

Ab Constraints par chalo � PRIMARY KEY, NOT NULL, UNIQUE jaise rules seekho jo data ki integrity maintain karte hain.