🗄️ SQL Commands Cheat Sheet¶
📋 Quick Reference¶
DDL (Data Definition Language)¶
CREATE TABLE¶
ALTER TABLE¶
DROP TABLE¶
DML (Data Manipulation Language)¶
INSERT¶
UPDATE¶
DELETE¶
SELECT Queries¶
Basic SELECT¶
SELECT with WHERE¶
SELECT with ORDER BY¶
🔍 Filtering & Conditions¶
Comparison Operators¶
=: Equal to!=or<>: Not equal to>: Greater than<: Less than>=: Greater than or equal to<=: Less than or equal to
Logical Operators¶
AND: All conditions must be trueOR: At least one condition must be trueNOT: Negates the condition
Pattern Matching¶
-- Starts with
WHERE column_name LIKE 'prefix%'
-- Ends with
WHERE column_name LIKE '%suffix'
-- Contains
WHERE column_name LIKE '%substring%'
-- Single character wildcard
WHERE column_name LIKE 'prefix_'
🤝 JOINs¶
INNER JOIN¶
LEFT JOIN¶
RIGHT JOIN¶
📊 Aggregate Functions¶
Common Aggregates¶
SELECT COUNT(column_name) FROM table_name;
SELECT SUM(column_name) FROM table_name;
SELECT AVG(column_name) FROM table_name;
SELECT MIN(column_name) FROM table_name;
SELECT MAX(column_name) FROM table_name;
GROUP BY¶
HAVING¶
🎯 Common Patterns¶
Pagination¶
Subquery¶
SELECT *
FROM table_name
WHERE column_name IN (SELECT column_name FROM another_table WHERE condition);
CASE Statement¶
SELECT column_name,
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
ELSE default_result
END AS new_column
FROM table_name;
🔍 Debugging Checklist¶
- Table and column names are correct
- Quotes are used properly for strings
- WHERE clause is included for UPDATE/DELETE
- JOIN conditions are correct
- Aggregate functions have GROUP BY when needed
- Subqueries return single values when required
⚡ Performance Tips¶
Optimization Guidelines¶
- ✅ Use indexes on frequently queried columns
- ✅ SELECT only needed columns, avoid
SELECT * - ✅ Use appropriate JOIN types
- ✅ Use LIMIT for large result sets
- ✅ Consider EXISTS instead of IN for subqueries
Common Pitfalls¶
- ❌ Missing WHERE clause in UPDATE/DELETE
- ❌ Cartesian products in JOINs
- ❌ NULL comparisons with
=(useIS NULL) - ❌ String comparisons without proper casing
Last updated: Phase 3 Implementation