SQL (Structured Query Language) is the universal language for interacting with relational databases, enabling analysts to extract, aggregate, and transform data from structured tables with unparalleled precision. For data analysis, SQL's true power lies not just in retrieving rows but in window functions, CTEs, joins, statistical aggregates, and JSON processingβtools that turn raw tables into insights. Unlike procedural programming, SQL operates in declarative sets: you describe what you want, and the query optimizer figures out how. Master these patterns, and you'll unlock everything from trend analysis to hierarchical traversals without leaving the database.
What This Cheat Sheet Covers
This topic spans 24 focused tables and 151 indexed concepts. Below is a complete table-by-table outline of this topic, spanning foundational concepts through advanced details.
Table 1: Join Types
| Type | Example | Description |
|---|---|---|
SELECT * FROM orders oINNER JOIN customers c ON o.customer_id = c.id | Returns only rows with matching values in both tables β most common join for relating normalized data. | |
SELECT * FROM employees eLEFT JOIN departments d ON e.dept_id = d.id | Returns all rows from left table, plus matched rows from right β unmatched right-side columns are NULL. | |
SELECT * FROM orders oRIGHT JOIN products p ON o.product_id = p.id | Returns all rows from right table, plus matched rows from left β mirrors LEFT JOIN with table positions swapped. | |
SELECT * FROM sales sFULL OUTER JOIN refunds r ON s.order_id = r.order_id | Returns all rows from both tables β unmatched rows from either side show NULL for missing columns. | |
SELECT * FROM colorsCROSS JOIN sizes | β’ Produces Cartesian product β every row from left paired with every row from right β’ use with care. |