DEV Community

Cover image for Day 17 of My Data Analytics Journey !
Ramya .C
Ramya .C

Posted on

Day 17 of My Data Analytics Journey !

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)
);
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

βœ… Comparison Operator Example

Find hospitals in Chennai:

SELECT * 
FROM Hospital 
WHERE city = 'Chennai';
Enter fullscreen mode Exit fullscreen mode

βœ… Logical Operator Example

Find hospitals with more than 100 beds AND located in Chennai:

SELECT * 
FROM Hospital 
WHERE total_beds > 100 AND city = 'Chennai';
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

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)