SQL

πŸ—“οΈ Day 1: Introduction to SQL and Databases

πŸ—“οΈ Day 1: Introduction to SQL and Databases

Welcome to Day 1 of your SQL learning journey!
Today, we’ll explore the basics β€” what data is, what a database does, and how SQL helps us manage data efficiently.


πŸ“Š What is Data?

Data is simply a collection of information.
Example: Names, phone numbers, employee records, sales reports β€” all are data.


πŸ’¬ What is SQL (Structured Query Language)?

SQL stands for Structured Query Language.
It is a language used to communicate with databases β€” to store, retrieve, and manage data.


πŸ—„οΈ What is a Database (DB)?

A Database is a structured place where data is stored and retrieved efficiently.
Think of it as a digital filing cabinet.

πŸ” Why Do We Need a Database?

  1. βœ… Data Security – Protects data from unauthorized access.

  2. πŸ“¦ Handles Large Data – Can store millions of records efficiently.

  3. πŸ‘₯ Multi-User Access – Multiple users can access it simultaneously.

  4. πŸ” Access Control – Permissions can be granted or revoked.

  5. πŸ’Ύ Prevents Data Loss – Backup and recovery mechanisms help preserve data.


🧱 Database Structures (Models)

The structure or model defines how data is organized in a database.

1. Hierarchical Model

  • Data is stored in a tree-like structure (Parent–Child relationship).

  • Example: Each employee (child) belongs to one department (parent).

  • Disadvantage: You can’t easily access data from other branches.

2. Network Model

  • Data is stored using many-to-many relationships.

  • Advantage: You can access related records easily.

  • Disadvantage: More complex to design and query.

3. Entity-Relationship Model (ER Model)

  • Represents data as entities (tables) and their relationships.

  • Fast data fetching but may consume more space.

4. Relational Model (RDBMS)

  • Stores similar kinds of data in tables.

  • Allows relationships between tables using keys (Primary, Foreign).

  • Examples of RDBMS: Oracle, MySQL, SQL Server, Snowflake, Azure SQL Database


βš™οΈ SQL Commands and Categories

SQL commands are grouped into five main categories:


1. DDL – Data Definition Language

Used to define or modify the structure of the database.

Commands:

  • CREATE – Create new tables or databases

  • ALTER – Modify table structure

  • RENAME – Rename a table

  • DROP – Delete a table completely

  • TRUNCATE – Delete all records but keep the table structure

-- Remove the table permanently
DROP TABLE employees;

-- Delete all rows but keep structure
TRUNCATE TABLE employees;

πŸ†š DELETE vs TRUNCATE vs DROP

Command Action Keeps Structure? Can Rollback?
DELETE Removes selected rows βœ… Yes βœ… Yes
TRUNCATE Removes all rows βœ… Yes ❌ No
DROP Removes table completely ❌ No ❌ No

2. DML – Data Manipulation Language

Used to manipulate or modify the data inside tables.

Commands:

  • INSERT – Add new records

  • UPDATE – Modify existing records

  • DELETE – Remove records

Example:

INSERT INTO employees (id, name, salary) VALUES (1, 'John', 50000);
UPDATE employees SET salary = 60000 WHERE id = 1;
DELETE FROM employees WHERE id = 1;

3. TCL – Transaction Control Language

Used to manage transactions in a database (commit, rollback, checkpoints).

Commands:

  • COMMIT – Save all changes permanently

  • ROLLBACK – Undo uncommitted changes

  • SAVEPOINT – Create checkpoints to partially undo operations

Example:

UPDATE employees SET salary = salary + 5000 WHERE department = 'IT';
COMMIT;

DELETE FROM employees WHERE department = 'HR';
ROLLBACK;

SAVEPOINT before_delete_IT;
DELETE FROM employees WHERE department = 'IT';
ROLLBACK TO before_delete_IT;
COMMIT;


4. DCL – Data Control Language

Used to control user access to the database.

Commands:

  • GRANT – Give permissions

  • REVOKE – Remove permissions

-- Grant permissions
GRANT SELECT, INSERT ON my_database.employees TO 'john'@'localhost';

-- Revoke permissions
REVOKE SELECT ON my_database.employees FROM 'john'@'localhost';


5. DQL – Data Query Language

Used to fetch or query data from the database.

Command:

  • SELECT – Retrieve data from tables

Examples:

SELECT * FROM employees;
SELECT emp_name, salary FROM employees WHERE department = 'HR';

Common SQL Clauses

  • SELECT – Columns to fetch

  • FROM – Table name

  • WHERE – Conditions

Example:

SELECT roll_number, name FROM students WHERE grade = 'A';

βœ… Summary

On Day 1, you’ve learned:

  • What data and databases are

  • Why databases are needed

  • Database models and structures

  • SQL command types: DDL, DML, TCL, DCL, DQL

Next, you’ll learn about constraints, keys, and relationships between tables β€” essential for building strong SQL foundations.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button