SQL (Structured Query Language) – Complete Notes
📌 1. What is SQL?
SQL stands for Structured Query Language
Used to create, manipulate, and query relational databases
Supported by DBMSs like MySQL, Oracle, SQL Server, PostgreSQL, etc.
🔹 2. SQL Categories
Category Common Commands
DDL (Data Definition) CREATE, ALTER, DROP, TRUNCATE
DML (Data Manipulation) SELECT, INSERT, UPDATE, DELETE
DCL (Data Control) GRANT, REVOKE
TCL (Transaction Control) COMMIT, ROLLBACK, SAVEPOINT
🔸 3. DDL Commands
✅ CREATE
Creates database objects like tables, views.
sql
CREATE TABLE Students (
ID INT PRIMARY KEY,
Name VARCHAR(100),
Age INT
);
✅ ALTER
Modifies table structure (add, delete, modify columns).
Sql
1
ALTER TABLE Students ADD Email VARCHAR(100);
✅ DROP
Deletes a table or database permanently.
sql
DROP TABLE Students;
✅ TRUNCATE
Deletes all rows but keeps the table/Deletes all data from a table but keeps the structure.
sql
TRUNCATE TABLE Students;
🔸 4. DML Commands
✅ SELECT
Retrieves data.
sql
SELECT Name, Age FROM Students WHERE Age > 18;
✅ INSERT
Adds new data.
sql
INSERT INTO Students (ID, Name, Age) VALUES (1, 'Ali', 20);
✅ UPDATE
Modifies existing data.
sql
UPDATE Students SET Age = 21 WHERE ID = 1;
✅ DELETE
Removes data/Deletes specific records.
sql
DELETE FROM Students WHERE ID = 1;
2
🔸 5. DCL Commands
✅ GRANT
Gives user access rights/Gives specific permissions to users (e.g., SELECT, INSERT).
sql
GRANT SELECT, INSERT ON Students TO user1;
✅ REVOKE
Removes access rights/Removes previously granted permissions.
sql
REVOKE SELECT, INSERT ON Students FROM user1;
🔸 6. TCL Commands
✅ COMMIT
Saves all changes made/Saves all changes made during the current transaction
sql
COMMIT;
✅ ROLLBACK
Undoes uncommitted changes/Undoes changes made in the current transaction.
sql
ROLLBACK;
✅ SAVEPOINT
Sets a save point to roll back partially/Sets a point within a transaction for partial rollback.
sql
SAVEPOINT sp1;
ROLLBACK TO sp1;
3
🔸 7. Clauses in SQL
Clause Purpose
WHERE Filters rows
ORDER BY Sorts results
GROUP BY Groups rows for aggregate functions
HAVING Filters groups
DISTINCT Removes duplicate rows
BETWEEN, LIKE, IN Used in WHERE clause conditions
🔸 8. Aggregate Functions
Function Description
COUNT() Counts number of rows
SUM() Adds values
AVG() Calculates average
MAX() Finds largest value
MIN() Finds smallest value
🔸 9. Joins in SQL
Join Type Description
INNER JOIN Matches records in both tables
LEFT JOIN All records from left + matched records from right
RIGHT JOIN All records from right + matched from left
FULL JOIN All records when there's a match in one of the tables
Example:
4
sql
SELECT Students.Name, Marks.Score
FROM Students
INNER JOIN Marks ON Students.ID = Marks.StudentID;
🔸 10. Subqueries and Nested Queries
A subquery is a query inside another query.
sql
SELECT Name FROM Students
WHERE Age = (SELECT MAX(Age) FROM Students);
🔸 11. Views in SQL
A view is a virtual table.
o A view is a virtual table in SQL based on the result of a SELECT query.
o It does not store data physically.
o It shows data from one or more tables.
o You can use views just like a table in your queries.
sql
CREATE VIEW young_students AS
SELECT * FROM Students WHERE Age < 20;
🔸 12. Indexing in SQL
sql
CREATE INDEX idx_name ON Students(Name);
Improves performance for large datasets.
🔸 13. Constraints in SQL
5
Constraint Purpose
PRIMARY KEY Uniquely identifies rows
FOREIGN KEY Ensures referential integrity
UNIQUE All values must be different
NOT NULL Column cannot have NULL
CHECK Validates values with a condition
DEFAULT Provides default value