SQL-Notes

Chapter 1: Introduction to SQL

1.1 What is SQL?

SQL (Structured Query Language) is a domain-specific language used to manage and manipulate relational databases. It allows users to create, read, update, and delete (CRUD) data stored in structured formats. SQL is widely used in applications, analytics, and data-driven decision-making.

1.2 Features of SQL

1.3 Types of SQL Commands

SQL commands are categorized into five types:

  1. Data Definition Language (DDL) – Defines database structure.
    • CREATE – Creates a new table or database.
    • ALTER – Modifies an existing database structure.
    • DROP – Deletes a table or database.
    • TRUNCATE – Removes all records from a table without logging individual row deletions.
  2. Data Manipulation Language (DML) – Deals with data modification.
    • INSERT – Adds new records.
    • UPDATE – Modifies existing records.
    • DELETE – Removes specific records.
  3. Data Query Language (DQL) – Used to retrieve data.
    • SELECT – Fetches data from one or more tables.
  4. Data Control Language (DCL) – Manages access permissions.
    • GRANT – Gives privileges to users.
    • REVOKE – Removes privileges from users.
  5. Transaction Control Language (TCL) – Manages transactions.
    • COMMIT – Saves changes permanently.
    • ROLLBACK – Reverts changes.
    • SAVEPOINT – Sets a savepoint in a transaction to rollback partially.

1.4 Relational Database Concepts

A Relational Database Management System (RDBMS) stores data in a structured format using tables, relationships, and constraints.

1.5 Basic SQL Syntax

1.5.1 Creating a Database

CREATE DATABASE School;

1.5.2 Creating a Table

CREATE TABLE Students (
    ID INT PRIMARY KEY,
    Name VARCHAR(50),
    Age INT,
    Grade VARCHAR(10)
);

1.5.3 Inserting Data

INSERT INTO Students (ID, Name, Age, Grade) 
VALUES (1, 'John Doe', 15, '10th');

1.5.4 Retrieving Data

SELECT * FROM Students;

1.5.5 Updating Data

UPDATE Students 
SET Age = 16 
WHERE ID = 1;

1.5.6 Deleting Data

DELETE FROM Students 
WHERE ID = 1;

1.7 Advantages of SQL