SQL Quick Reference (Cheat Sheet) π
Mentor's Note: Keep this page bookmarked! It contains the "Formula" for every major SQL command we've learned. π‘
ποΈ Structure (DDL)
| Action |
Syntax |
| Create Table |
CREATE TABLE table (col type, ...); |
| Add Column |
ALTER TABLE table ADD col type; |
| Drop Column |
ALTER TABLE table DROP COLUMN col; |
| Delete Table |
DROP TABLE table; |
| Clear Table |
TRUNCATE TABLE table; |
π Data Operations (DML)
| Action |
Syntax |
| Fetch All |
SELECT * FROM table; |
| Add Row |
INSERT INTO table (cols) VALUES (vals); |
| Modify Row |
UPDATE table SET col = val WHERE condition; |
| Remove Row |
DELETE FROM table WHERE condition; |
π Filtering & Sorting
| Filter |
Logic |
| Range |
WHERE salary BETWEEN 1000 AND 5000 |
| Set |
WHERE city IN ('Surat', 'Mumbai') |
| Pattern |
WHERE name LIKE 'A%' |
| NULLs |
WHERE email IS NULL |
| Sort |
ORDER BY name ASC|DESC |
πΈοΈ Joins
| Join Type |
Description |
| INNER JOIN |
Match in BOTH tables. β
|
| LEFT JOIN |
ALL from Left + Matches from Right. π |
| FULL JOIN |
EVERYTHING from both. π |
π Aggregates
COUNT(*): Total Rows.
SUM(col): Total Sum.
AVG(col): Mean.
MIN/MAX: Smallest/Largest.
π‘ Syntax Hierarchy
- SELECT
- FROM
- WHERE
- GROUP BY
- HAVING
- ORDER BY
π Learning Path