SQL & Relational Database Indexing Cheatsheet
Practical SQL cheat sheet: joins, window functions (ROW_NUMBER, RANK), CTEs, and B-Tree indexing rules.
π§ͺ
βΆ Run in Playground β
Run & Edit this Code Live in Browser
Zero setup required. Supports Python 3, modern JavaScript, and SQL.
1. Window Functions for Advanced Analytics
Window functions compute values across rows without collapsing rows like GROUP BY does.
CODE SNIPPET
-- Calculate running totals and rankings per department
SELECT
employee_id,
department,
salary,
ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) as salary_rank,
SUM(salary) OVER (PARTITION BY department ORDER BY hire_date) as running_payroll
FROM employees;
2. Common Table Expressions (CTE) & Deduplication
CTEs create readable, temporary result sets ideal for complex multi-stage mutations and deduplication.
CODE SNIPPET
WITH RankedDuplicates AS (
SELECT
id,
email,
ROW_NUMBER() OVER (PARTITION BY email ORDER BY created_at DESC) as rn
FROM users
)
DELETE FROM users
WHERE id IN (SELECT id FROM RankedDuplicates WHERE rn > 1);
Advertisement
View Blueprints β
Verified Partner
Quantitative Trading Systems & 30 AI Business Blueprints
Build predictable monthly recurring revenue with retainers & automated bots.
π Associated Learning Path & Monetization Blueprints
π Comprehensive Pillar Guide
SQL Database Mastery: Relational Design & Index Tuning
Advanced joins, window functions, B-Tree indexing, and ACID transaction mechanics.
Read Full Guide β
π° Monetizable AI Blueprint
Database-to-API Synthesizer Micro-SaaS ($199/mo)
Build an AI SaaS turning SQL database schemas into production REST and GraphQL endpoints.
Read Blueprint βNeed another cheat sheet?
We add new reference guides every week based on community requests.
Request a Cheatsheet β