SQL & Database Concepts Cheat Sheet
1. CRUD Operations
CREATE: Add new records
READ (SELECT): Fetch data
UPDATE: Modify existing records
DELETE: Remove records
2. Stored Procedures
Reusable SQL blocks stored in the database. Can accept parameters and return values.
3. Views
Virtual tables based on SELECT queries. Used for abstraction and security.
4. Pagination
Used to split results into pages. Example:
SELECT * FROM Table ORDER BY ID OFFSET 10 ROWS FETCH NEXT 10 ROWS ONLY
5. Joins
INNER JOIN: Matching rows
LEFT JOIN: All from left + matches from right
RIGHT JOIN: All from right + matches from left
FULL JOIN: All from both
CROSS JOIN: Cartesian product
6. Set Operations
UNION: Combine and remove duplicates
UNION ALL: Combine with duplicates
INTERSECT: Common rows
EXCEPT: Rows in first but not second
7. Backup & Restore
BACKUP DATABASE dbname TO DISK='[Link]'
RESTORE DATABASE dbname FROM DISK='[Link]'
8. Security
GRANT/REVOKE permissions
Use roles, users, and parameterized queries to avoid SQL injection
9. Indexes
SQL & Database Concepts Cheat Sheet
Speed up searches but slow down write operations
10. CTE & Subqueries
CTE: WITH temp AS (...)
Subqueries: Queries inside other queries
11. Window Functions
ROW_NUMBER(), RANK(), LEAD(), LAG() - used in analytics
12. Triggers
Automatic procedures after INSERT/UPDATE/DELETE
13. Table Types
Temp Tables: #Temp
Table Variables: DECLARE @temp TABLE (...)
14. Constraints
PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK, DEFAULT, NOT NULL
15. Normalization
Organize data to reduce redundancy and improve integrity