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)