DBMS - SQL Chapter Notes
1. What is SQL?
SQL (Structured Query Language) is a standard language used to create, manage, and retrieve
data from relational databases.
2. Categories of SQL Commands:
- DDL (Data Definition Language): Defines structure (CREATE, ALTER, DROP).
- DML (Data Manipulation Language): Manipulates data (INSERT, UPDATE, DELETE).
- DCL (Data Control Language): Controls user permissions (GRANT, REVOKE).
- TCL (Transaction Control Language): Manages transactions (COMMIT, ROLLBACK,
SAVEPOINT).
Example of CREATE (DDL):
CREATE TABLE Student (
roll_no INT PRIMARY KEY,
name VARCHAR(30),
age INT
);
Example of INSERT (DML):
INSERT INTO Student (roll_no, name, age) VALUES (101, 'Rahim', 20);
3. Aggregate Functions:
SUM, AVG, MIN, MAX, COUNT
Example: SELECT AVG(age) FROM Student;
4. WHERE vs HAVING:
- WHERE filters rows before grouping.
- HAVING filters groups after grouping.
5. Joins:
- INNER JOIN: Returns rows matching in both tables.
- LEFT JOIN: Returns all rows from left table.
- RIGHT JOIN: Returns all rows from right table.
- FULL OUTER JOIN: All rows from both tables.
- SELF JOIN: Table joined with itself.
6. Views:
A virtual table defined using a query.
Example:
CREATE VIEW StudentMarks AS
SELECT [Link], [Link], [Link]
FROM Student
JOIN Marks ON Student.roll_no = Marks.roll_no;
7. Index:
Improves query speed.
Example: CREATE INDEX idx_name ON Student(name);
8. Subqueries:
Query inside another query.
Example:
SELECT name FROM Student
WHERE roll_no = (SELECT roll_no FROM Marks WHERE marks=100);