Pi4
☕ Buy me a coffee
The only reference you need for everyday data work.
You know the feeling: you need to write a quick report, you know exactly what
you want to do, but for a split second, you forget the exact syntax for PARTITION BY or date formatting.
This cheat sheet contains the 80% of SQL commands and functions you will actually need in practice. No fluff. Bookmark this page (Ctrl+D / Cmd+D) so you have it handy at work tomorrow.
| What you need | SQL Syntax | Practical Example |
|---|---|---|
| Unique values | SELECT DISTINCT column |
SELECT DISTINCT category FROM products; |
| Filtering multiple values | WHERE column IN (x, y) |
WHERE status IN ('Active', 'Pending'); |
| Pattern matching (Text) | WHERE column LIKE '%ptn%' |
WHERE email LIKE '%@gmail.com'; |
| Value range | WHERE column BETWEEN x AND y |
WHERE price BETWEEN 100 AND 500; |
| Empty values | WHERE column IS NULL |
WHERE delivery_date IS NULL; |
| What you need | SQL Syntax | Practical Example |
|---|---|---|
| Row count | COUNT(column) |
SELECT COUNT(id) FROM users; |
| Sum and Average | SUM(), AVG() |
SELECT SUM(revenue), AVG(revenue) FROM sales; |
| Grouping data | GROUP BY column |
SELECT department, COUNT(*) FROM employees GROUP BY department;
|
| Filter aggregated data | HAVING condition |
... GROUP BY department HAVING COUNT(*) > 10; |
| JOIN Type | Result | When to use |
|---|---|---|
| INNER JOIN | Intersection of both tables | You only want users who made an order. |
| LEFT JOIN | All from left, matches from right | You want all users and their potential orders. |
| RIGHT JOIN | All from right, matches from left | Opposite of LEFT JOIN (rarely used in practice). |
| FULL JOIN | Everything from both tables | You want to see absolutely everything, even if records don't match. |
| What you need | SQL Syntax | When to use |
|---|---|---|
| Ranking | RANK() OVER (ORDER BY col DESC) |
Finding the TOP 3 best sellers. |
| Dense Ranking | DENSE_RANK() OVER (...) |
Like RANK, but doesn't skip numbers on ties. |
| Cumulative sum | SUM(column) OVER (ORDER BY date) |
Calculating growing revenue over time (YTD). |
| Compare with previous | LAG(column, 1) OVER (ORDER BY date) |
Month-over-Month (MoM) growth comparison. |
| What you need | SQL Syntax | Practical Example |
|---|---|---|
| If / Then (Conditions) | CASE WHEN ... THEN ... |
CASE WHEN age > 18 THEN 'Adult' ELSE 'Child' END |
| Concatenating text | CONCAT(col1, ' ', col2) |
CONCAT(first_name, ' ', last_name) |
| Trimming spaces | TRIM(column) |
TRIM(email) (Ideal for data cleaning) |
| Replacing NULL | COALESCE(column, fallback) |
COALESCE(phone, 'Not provided') |
Creating and updating this cheat sheet took me dozens of hours to save you time in your daily work.
If this cheat sheet saves your sanity before a deadline, consider supporting my work.
☕ Buy me a coffee