Mastering SQL: Queries, Joins, Aggregations & Performance Cheat Sheet
Quick lookup guide for SELECT, INNER/LEFT/RIGHT JOINs, GROUP BY, indexes, and ACID transactions.
Table of Contents
SQL Join Types Decoded
How relational joins merge rows across separate tables.
| Join Type | Description | SQL Syntax Example |
|---|---|---|
| `INNER JOIN` | Returns only rows where there is a match in BOTH tables. | `SELECT * FROM users u INNER JOIN orders o ON u.id = o.user_id;` |
| `LEFT JOIN` | Returns ALL rows from the left table, plus matched rows from the right table (NULL if no match). | `SELECT * FROM users u LEFT JOIN orders o ON u.id = o.user_id;` |
| `RIGHT JOIN` | Returns ALL rows from the right table, plus matched rows from the left table. | `SELECT * FROM users u RIGHT JOIN orders o ON u.id = o.user_id;` |
| `FULL OUTER JOIN` | Returns rows when there is a match in EITHER table (combines LEFT and RIGHT). | `SELECT * FROM users u FULL OUTER JOIN orders o ON u.id = o.user_id;` |
Core Query Clauses & Execution Order
Notice that SQL executes clauses in a different order than they are written!
| Execution Step | Clause | Purpose |
|---|---|---|
| 1 | `FROM` / `JOIN` | Identify target tables and join conditions |
| 2 | `WHERE` | Filter individual base rows before aggregation |
| 3 | `GROUP BY` | Aggregate rows into summary categories |
| 4 | `HAVING` | Filter aggregated groups (unlike WHERE which filters rows) |
| 5 | `SELECT` | Compute expressions, column aliases, and return fields |
| 6 | `ORDER BY` | Sort the finalized result set |
| 7 | `LIMIT` / `OFFSET` | Constrain number of returned rows for pagination |
Essential Aggregation & Indexing Recipes
Common SQL queries for production reporting.
SELECT user_id, COUNT(*) AS total_orders, SUM(amount) AS total_spent
FROM orders
WHERE status = 'completed'
GROUP BY user_id
HAVING COUNT(*) >= 5
ORDER BY total_spent DESC
LIMIT 20;CREATE INDEX idx_orders_user_status ON orders (user_id, status);Interactive Client-Side Tools for this Guide
Test and run these patterns directly inside your browser with zero server uploads:
Online SQL Formatter & Query Beautifier
Turn messy, minified SQL queries into clean, beautifully structured code with standardized uppercase keywords and custom indentation.
JSON Formatter & Validator (Beautify & Minify)
Clean up messy API responses, fix syntax glitches, and compress JSON payloads directly in your browser.
CSV to JSON & JSON to CSV Online Converter
Two-way in-browser data transformer for developers and data analysts. Automatic number/boolean casting with zero server uploads.
Frequently Asked Questions
What is the exact difference between WHERE and HAVING?
`WHERE` filters individual rows BEFORE they are grouped. `HAVING` filters aggregated group metrics (like `COUNT(*)` or `SUM(revenue)`) AFTER the grouping occurs.
How do I clean and beautify messy SQL scripts?
Use DocSwift's free SQL Formatter. It formats messy inline SQL queries into indented, uppercase-keyword statements client-side.