SQL Commands Cheat Sheet
Complete SQL Reference for Database Operations
This cheat sheet uses two example tables: a 'rep' table containing sales representative information (name, ID, state) and a 'daily_sales' table tracking revenue and rep performance by date.
SQL Command Categories
Data Definition Language (DDL)
Commands like CREATE TABLE and ALTER TABLE that define database structure and schema modifications.
Data Manipulation Language (DML)
Commands including SELECT, INSERT, UPDATE, and DELETE for working with data within tables.
Data Query Language (DQL)
Advanced SELECT operations with joins, aggregations, and filtering for complex data retrieval.
Essential Table Operations
Use the AS keyword to rename columns for better readability in query results. This is especially useful when working with calculated fields or joining multiple tables.
Conditional Operators Comparison
| Feature | AND | OR |
|---|---|---|
| Logic Type | All conditions must be true | At least one condition must be true |
| Use Case | Narrow down results | Broaden search criteria |
| Performance | More selective, faster | Less selective, potentially slower |
Building Complex Conditions
Start with Basic WHERE
Begin with simple equality conditions to establish your base dataset
Add Range Filters
Use BETWEEN for date ranges or numerical boundaries to further refine results
Apply Pattern Matching
Implement LIKE with wildcards for flexible text searching capabilities
Combine with Logic Operators
Use AND/OR to create sophisticated multi-criteria filtering conditions
SQL Aggregate Functions
COUNT
Returns the number of rows that match specified criteria. Essential for data validation and reporting.
SUM & AVG
Calculate totals and averages for numerical data. Critical for financial and statistical analysis.
MAX & MIN
Find extreme values in datasets. Useful for identifying outliers and boundary conditions.
Sorting operations can be resource-intensive on large datasets. Consider adding indexes on frequently sorted columns to improve query performance.
JOIN Types Comparison
| Feature | INNER JOIN | LEFT JOIN |
|---|---|---|
| Data Returned | Only matching records | All left table records |
| Missing Data Handling | Excludes non-matches | Shows NULL for missing |
| Best For | Complete data analysis | Comprehensive reporting |
Always ensure proper indexes exist on join columns. Joining large tables without indexes can severely impact query performance and database responsiveness.
The following examples demonstrate practical applications using sales representative and daily sales data, showing how SQL commands work together in business scenarios.
SELECT name FROM rep WHERE ID = '555';
SELECT MAX(revenue) FROM daily_sales;
SELECT a.state, b.date, sum(revenue) FROM rep a JOIN daily_sales b ON a.ID = b.ID GROUP BY state, date;
Query Optimization Checklist
Ensure ID columns in both rep and daily_sales tables have indexes
Add WHERE clauses to limit date ranges for better performance
Ensure ID columns use consistent data types across tables
Verify query logic with known data before running on production
Key Takeaways