0% found this document useful (0 votes)
5 views6 pages

SQL Notes

SQL (Structured Query Language) is used for creating, manipulating, and querying relational databases, supported by various DBMSs. It includes categories such as DDL, DML, DCL, and TCL with commands for defining, manipulating, controlling, and managing transactions in databases. The document also covers clauses, aggregate functions, joins, subqueries, views, indexing, and constraints in SQL.

Uploaded by

ammar7070897
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)
5 views6 pages

SQL Notes

SQL (Structured Query Language) is used for creating, manipulating, and querying relational databases, supported by various DBMSs. It includes categories such as DDL, DML, DCL, and TCL with commands for defining, manipulating, controlling, and managing transactions in databases. The document also covers clauses, aggregate functions, joins, subqueries, views, indexing, and constraints in SQL.

Uploaded by

ammar7070897
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/ 6

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

You might also like