Learning SQL Data Types & Operators
Today marks Day 17 of my Data Analytics learning journey, and I explored one of the most important topics in SQL β Data Types and Operators. These are the foundation for writing queries correctly and performing meaningful operations on data.
To practice, I also created a Hospital database using different data types and applied SQL operators to perform calculations and comparisons.
πΉ SQL Data Types
In SQL, every column in a table must have a data type. Data types tell the database what kind of values can be stored in a column (numbers, text, dates, etc.).
Here are the main categories of SQL data types:
1. Numeric Data Types
-
INTβ Whole numbers (e.g., 10, -5, 1000) -
DECIMAL(p,s)β Fixed precision decimal values (e.g., 123.45) -
FLOAT/REALβ Approximate decimal values (e.g., 3.14159)
π Used when storing things like age, salary, or percentage.
2. Character/String Data Types
-
CHAR(n)β Fixed-length string (e.g.,'RAMYA 'with length 5) -
VARCHAR(n)β Variable-length string (saves space) -
TEXTβ Large amount of text data
π Best for storing names, emails, addresses, etc.
3. Date and Time Data Types
-
DATEβ Stores year, month, day (e.g.,2025-08-16) -
TIMEβ Stores only time (e.g.,10:30:45) -
DATETIME/TIMESTAMPβ Stores both date and time
π Useful for logging order dates, timestamps, and scheduling events.
4. Other Data Types
-
BOOLEANβ True/False values -
BLOBβ Binary data like images or files
πΉ My Example: Hospital Database
Hereβs a sample hospital table I created using SQL data types:
CREATE TABLE Hospital (
hospital_id INT,
hospital_name VARCHAR(100),
established_date DATE,
total_beds INT,
available_beds INT,
city VARCHAR(50)
);
Now, letβs add some operator-based queries:
β Arithmetic Operator Example
Find occupied beds and bed occupancy percentage:
SELECT
hospital_name,
total_beds,
(total_beds - available_beds) AS occupied_beds,
((total_beds - available_beds) * 100 / total_beds) AS occupancy_percentage
FROM Hospital;
β Comparison Operator Example
Find hospitals in Chennai:
SELECT *
FROM Hospital
WHERE city = 'Chennai';
β Logical Operator Example
Find hospitals with more than 100 beds AND located in Chennai:
SELECT *
FROM Hospital
WHERE total_beds > 100 AND city = 'Chennai';
This way, I not only learned the theory but also applied it with real-life hospital data.
πΉ SQL Operators
Operators are used inside SQL queries to perform operations on data.
1. Arithmetic Operators
Used for mathematical calculations.
-
+(Addition) βSELECT 5+3;β 8 -
-(Subtraction) βSELECT 10-4;β 6 -
*(Multiplication) βSELECT 4*2;β 8 -
/(Division) βSELECT 20/5;β 4 -
%(Modulo) βSELECT 10%3;β 1
2. Comparison Operators
Used for comparing values in conditions.
-
=β Equal to -
<>or!=β Not equal -
>β Greater than -
<β Less than -
>=β Greater than or equal to -
<=β Less than or equal to
π Example:
SELECT * FROM employees WHERE salary > 50000;
3. Logical Operators
Used to combine conditions.
-
ANDβ Both conditions must be true -
ORβ At least one condition must be true -
NOTβ Negates a condition
π Example:
SELECT * FROM students
WHERE grade = 'A' AND age < 20;
4. Special Operators
-
BETWEENβ Check range (age BETWEEN 18 AND 25) -
INβ Match values in a list (city IN ('Chennai','Bangalore')) -
LIKEβ Pattern matching (name LIKE 'R%') -
IS NULLβ Check missing values
πΉ Key Takeaway
β
Data types ensure that each column stores the right kind of information.
β
Operators help us filter, calculate, and manipulate data effectively.
β
Building my hospital database gave me hands-on practice with both concepts.
Top comments (0)