SQL notes
SQL notes
📌 SQL Categories
🔹 DDL (Data Definition Language) – Defines the structure of databases.
🔹 DQL (Data Query Language) – Retrieves data from databases.
🔹 DML (Data Manipulation Language) – Modifies and manipulates data.
🔹 DCL (Data Control Language) – Controls access to data.
🔹 TCL (Transaction Control Language) – Manages database transactions.
📌 SQL Commands
🔹 DDL Commands (Schema Definition)
Command Description
CREATE Creates a new table, database, or object.
DROP Deletes an existing table or database.
ALTER Modifies an existing table structure.
TRUNCATE Deletes all records but keeps the table structure.
RENAME Renames a table or column.
📌 SQL CHEATSHEET 1
INSERT Adds new records to a table.
UPDATE Modifies existing records.
DELETE Removes records from a table.
📌 SQL Operators
🔹 Arithmetic Operators
+ (Add) | - (Subtract) | * (Multiply) | / (Divide) | % (Modulo)
🔹 Comparison Operators
= | != | > | < | >= | <=
🔹 Logical Operators
✅ AND | ✅ OR | ✅ NOT | ✅ |✅
IN EXISTS | ✅ BETWEEN
🔹 Aggregation Functions
✔ AVG() – Average value
📌 SQL CHEATSHEET 2
📌 SQL Constraints
✅ NOT NULL – Ensures a column cannot have NULL values.
✅ UNIQUE – Ensures all values in a column are unique.
📌 SQL Joins
Join Type Description Example
Returns all records from the left table + SELECT * FROM A LEFT JOIN B
LEFT JOIN
matching ones from the right. ON A.id = B.id;
Returns all records from the right table SELECT * FROM A RIGHT JOIN
RIGHT JOIN
+ matching ones from the left. B ON A.id = B.id;
📌 SQL CHEATSHEET 3
Age INT
);
📌 SQL CHEATSHEET 4
🔹 Fetch Sum of a Column
SELECT SUM(Age) FROM Students;
📌 SQL CHEATSHEET 5