0% found this document useful (0 votes)
6 views

SQL notes

This SQL cheatsheet serves as a quick reference for essential SQL commands, operators, joins, and functions, categorized into DDL, DQL, DML, DCL, and TCL. It includes examples of commands for creating, retrieving, updating, and deleting data, as well as SQL operators, constraints, and best practices. The document also covers SQL joins and set operations to manipulate and combine data from multiple tables.

Uploaded by

Shubham Kourav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

SQL notes

This SQL cheatsheet serves as a quick reference for essential SQL commands, operators, joins, and functions, categorized into DDL, DQL, DML, DCL, and TCL. It includes examples of commands for creating, retrieving, updating, and deleting data, as well as SQL operators, constraints, and best practices. The document also covers SQL joins and set operations to manipulate and combine data from multiple tables.

Uploaded by

Shubham Kourav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

📌 SQL CHEATSHEET

✅ Genuine | ✅ Authentic | ✅ Quality


SQL (Structured Query Language) is used to manage, query, and manipulate
databases. This cheatsheet provides a quick reference to essential SQL
commands, operators, joins, and functions.

📌 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.

🔹 DQL Commands (Data Retrieval)


Command Description
SELECT Retrieves records from a database.

🔹 DML Commands (Data Manipulation)


Command Description

📌 SQL CHEATSHEET 1
INSERT Adds new records to a table.
UPDATE Modifies existing records.
DELETE Removes records from a table.

🔹 DCL Commands (Access Control)


Command Description
GRANT Gives user access privileges.
REVOKE Removes user access privileges.

🔹 TCL Commands (Transaction Control)


Command Description
COMMIT Saves changes permanently.
ROLLBACK Reverts changes to the last commit.
SAVEPOINT Creates a save point for partial rollback.

📌 SQL Operators
🔹 Arithmetic Operators
+ (Add) | - (Subtract) | * (Multiply) | / (Divide) | % (Modulo)

🔹 Comparison Operators
= | != | > | < | >= | <=

🔹 Logical Operators
✅ AND | ✅ OR | ✅ NOT | ✅ |✅
IN EXISTS | ✅ BETWEEN

🔹 Aggregation Functions
✔ AVG() – Average value

✔ COUNT() – Number of records

✔ MAX() – Highest value


✔ MIN() – Lowest value

✔ SUM() – Total sum

📌 SQL CHEATSHEET 2
📌 SQL Constraints
✅ NOT NULL – Ensures a column cannot have NULL values.
✅ UNIQUE – Ensures all values in a column are unique.

✅ PRIMARY KEY – Uniquely identifies each record.


✅ FOREIGN KEY – Links two tables.

✅ CHECK – Ensures a condition is met.


✅ DEFAULT – Assigns a default value if no value is provided.

📌 SQL Joins
Join Type Description Example

Returns only matching records from SELECT * FROM A INNER JOIN


INNER JOIN
both tables. B ON A.id = B.id;

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;

SELECT * FROM A FULL JOIN B


FULL JOIN Returns all records from both tables.
ON A.id = B.id;

📌 SQL Set Operations


🔹 UNION – Combines results from multiple queries (removes duplicates).
🔹 UNION ALL – Combines results (keeps duplicates).
🔹 INTERSECT – Returns only matching values from both queries.
🔹 EXCEPT/MINUS – Returns values present in one query but not the other.
📌 SQL Examples
🔹 Creating a Table
CREATE TABLE Students (
ID INT PRIMARY KEY,
Name VARCHAR(50) NOT NULL,

📌 SQL CHEATSHEET 3
Age INT
);

🔹 Insert Data into a Table


INSERT INTO Students (ID, Name, Age)
VALUES (101, 'John Doe', 22);

🔹 Fetch All Data from a Table


SELECT * FROM Students;

🔹 Filter Data with WHERE Clause


SELECT * FROM Students WHERE Age > 20;

🔹 Updating Data in a Table


UPDATE Students
SET Name = 'Jane Doe'
WHERE ID = 101;

🔹 Delete Data from a Table


DELETE FROM Students WHERE ID = 101;

🔹 Order Records in Descending Order


SELECT * FROM Students ORDER BY Age DESC;

🔹 Count Number of Students


SELECT COUNT(*) FROM Students;

📌 SQL CHEATSHEET 4
🔹 Fetch Sum of a Column
SELECT SUM(Age) FROM Students;

🔹 Join Two Tables


SELECT Students.Name, Teachers.Name
FROM Students
INNER JOIN Teachers ON Students.TeacherID = Teachers.ID;

🔹 Full Outer Join


SELECT * FROM Students
FULL OUTER JOIN Teachers
ON Students.TeacherID = Teachers.ID;

📌 SQL Best Practices


✅ Use Aliases ( ) – Makes queries more readable.
AS

✅ Use Indexes – Improves search performance.


✅ Use or – Avoids fetching unnecessary data.
LIMIT TOP

✅ Normalize Tables – Reduces redundancy.


✅ Always Use with &
WHERE DELETE – Prevents accidental loss of data
UPDATE

📌 SQL CHEATSHEET 5

You might also like