Quick Reference

Ultimate SQL Cheat Sheet

A comprehensive guide to essential SQL commands, operations, and optimization techniques. Perfect for interviews, exams, and daily data work.

Basic Queries

Select All Columns
SELECT * FROM users;
Select Specific Columns
SELECT name, email FROM users;
Filter Data
SELECT * FROM users WHERE country = 'USA';
Sort Results
SELECT * FROM users ORDER BY created_at DESC;

JOIN Operations

INNER JOIN (Default)
Returns records with matching values in both tables.
SELECT * FROM orders o JOIN users u ON o.user_id = u.id;
LEFT JOIN
Returns all records from the left table, and matched records from the right.
SELECT * FROM users u LEFT JOIN orders o ON u.id = o.user_id;

Aggregations

Group By & Count
SELECT country, COUNT(*) FROM users GROUP BY country;
Filter Aggregated Data
Use HAVING instead of WHERE for aggregated data.
SELECT country, COUNT(*) FROM users GROUP BY country HAVING COUNT(*) > 5;

Advanced Functions

Coalesce (Fallback Defaults)
Returns the first non-null value.
SELECT COALESCE(phone, 'N/A') FROM users;
Window Functions
Compute ranks without collapsing rows.
SELECT name, salary, RANK() OVER (ORDER BY salary DESC) FROM employees;

Indexing Types

B-Tree Index
Default standard. Best for exact matches and range queries.
Hash Index
Lightning-fast for exact matches only. No range support.
Bitmap Index
Best for low-cardinality columns (e.g., Status, Gender).

Optimization Tips

Use Explain
Check execution plans before optimizing.
EXPLAIN QUERY PLAN SELECT * FROM users WHERE id = 5;
Avoid Leading Wildcards
This prevents index usage (Full Table Scan).
-- Slow WHERE name LIKE '%son';
© 2026 SQLMarrow. All Rights Reserved. Built for high-speed indexing and AEO search discovery.