SQL_Notes
SQL_Notes
for keeping that stuff depending on how you want to organize it.
Relational Database (the “Neat Box”)
Tools: MySQL, PostgreSQL
NoSQL Database (the “Flexible Box”)
Tools: MongoDB,Cassandra
Types
✅ Data Definition Language (DDL)
✅ Data Manipulation Language (DML)
✅ Data Control Language (DCL)
✅ Transaction Control Language (TCL)
✅ Data Query Language (DQL)
ALTER Changes the structure (add/remove column) Add drawer to toy box
TRUNCATE Empties all data inside the table Throw out all toys, keep the box
RENAME Changes the name of the table Give your toy box a new name
1. CREATE
building a brand-new toy box (a table)
✅ Syntax:
CREATE TABLE Students (
ID INT,
Name VARCHAR(50),
Age INT
);
2. ALTER
you already built a toy box, and now you want to
change somethings.
✅ Syntax:
ALTER TABLE Students ADD Grade VARCHAR(2);
3. DROP
This is like smashing your block tower to the ground
it's gone forever!
✅ Syntax:
DROP TABLE Students;
4. TRUNCATE
it will throw all the toys out but keep the box.
✅ Syntax:
TRUNCATE TABLE Students;
5. RENAME
Giving it a new name
✅ Syntax:
RENAME TABLE Students TO Kids;
2. UPDATE
Change Something About a Toy
✅ Syntax:
UPDATE ToyBox
WHERE ToyID = 1;
3. DELETE
Throw a Toy Away
✅ Syntax:
DELETE FROM ToyBox
WHERE ToyID = 1;
You found the toy with ID 1 and took it out of the box.
2. REVOKE
Take Permission Back
✅ Syntax:
REVOKE INSERT ON ToyBox FROM Sam;
1.COMMIT
Save Your Work
✅ Example:
INSERT INTO ToyBox VALUES (1, 'Car', 'Vehicle');
COMMIT;
2.ROLLBACK
Undo Everything
✅ Example:
INSERT INTO ToyBox VALUES (3, 'Robot', 'Electronic');
ROLLBACK;
3.SAVEPOINT
Set a Checkpoint
✅ Example:
INSERT INTO ToyBox VALUES (5, 'Puzzle', 'Brain');
SAVEPOINT Save1;
ROLLBACK TO Save1;
HAVING
The HAVING clause is used to filter groups created by the GROUP BY clause. It's similar to WHERE, but WHERE
filters rows before grouping, while HAVING filters groups after grouping.
SELECT customer, SUM(amount) AS total_spent
FROM sales_data
GROUP BY customer
SQL Functions
Aggregate Functions
Function Description
Scalar Functions
Function Description
Date Functions
Function Description
SQL Joins
Join Type Description
LEFT JOIN Returns all rows from the left table and matching rows from the right
RIGHT JOIN Returns all rows from the right table and matching rows from the left
FULL OUTER JOIN Returns all rows when there is a match in one of the tables
1. INNER JOIN
SELECT e.name, d.dept_name
FROM employees e
2. LEFT JOIN
FROM employees e
3. RIGHT JOIN
SELECT e.name, d.dept_name
FROM employees e
FROM employees e
5. CROSS JOIN
SELECT e.name, d.dept_name
FROM employees e
6. SELF JOIN
SELECT e.name AS employee,
m.name AS manager
FROM
employees e
LEFT JOIN
SQL Keys
Key Type Purpose
Unique Key Ensures all values in a column are unique (except NULLs)
Primary Key
CREATE TABLE students (
name VARCHAR(50),
age INT);
Foreign Key
CREATE TABLE enrollments (
student_id INT,
course_id INT,
);
Unique Key
CREATE TABLE teachers (
);
Composite Key
CREATE TABLE enrollments (
student_id INT,
course_id INT,
);
SQL Constraints
Constraint Description
NOT NULL
CREATE TABLE users (
);
DEFAULT
CREATE TABLE orders (
);
CHECK
CREATE TABLE employees (
);
SQL View
A custom lens to look at data
A saved query that acts like a table
1 Alice HR 60000
2 Bob IT 75000
3 Charlie HR 58000
FROM employees
SQL Index
An Index is like the index in a book
it helps you find data faster in a table.
use xyz;
ID INT,
Name VARCHAR(50),
Age INT
);
drop Grade;
UPDATE ToyBox
WHERE ToyID = 1;
SET SQL_SAFE_UPDATES = 0;
WHERE ToyID = 1;
BEGIN;
SAVEPOINT before_more_toys;
rollback;
COMMIT;
SELECT * FROM ToyBox ORDER BY ToyID desc;
FROM sales_data
GROUP BY customer
SELECT customer, amount FROM sales_data WHERE amount = (SELECT min(amount) FROM sales_data);
select length('SHRIDHAR');
select day('2025-04-23');
FROM employeess e
FROM employeess e
UNION
FROM employeess e
FROM employeess e
m.name AS manager
FROM
employeess e
INNER JOIN
name VARCHAR(50),
age INT);
student_id INT,
);
);
student_id INT,
course_id INT,
);
);
);
FROM sales_data
ON youtube_channel_stats (subscribers);
call youtube.empty();
call lakhsubs(600000);