SQL examples
SQL Cheat Sheet for Data Analysis: Filter, Group, Join, Dates, and Ranking
One pattern per common analysis question, from previewing a new table to ranking rows inside groups, each linked to a lesson where you can practice it.
On This Page
Preview rows
Use a small LIMIT query whenever you start with a new file. It helps confirm table names, column names, and data shape.
SELECT *
FROM my_table
LIMIT 20;Filter rows
WHERE filters rows before grouping or sorting. Combine conditions when you need a precise slice of a file.
SELECT *
FROM orders
WHERE status = 'paid'
AND revenue >= 100
AND order_date >= DATE '2026-01-01';Group and aggregate
GROUP BY turns many detail rows into summaries. It is useful for totals by region, customer, product, status, or time period.
SELECT
region,
COUNT(*) AS rows,
SUM(revenue) AS revenue
FROM orders
GROUP BY region
HAVING SUM(revenue) > 10000
ORDER BY revenue DESC;Join related files
After adding multiple files, use JOIN to combine detail rows with lookup tables, metadata, or related entities.
SELECT
orders.order_id,
customers.customer_name,
orders.revenue
FROM orders
LEFT JOIN customers
ON orders.customer_id = customers.customer_id;Work with dates
Date queries help with monthly reporting, retention checks, and time-window analysis.
SELECT
date_trunc('month', order_date) AS month,
COUNT(*) AS orders
FROM orders
GROUP BY month
ORDER BY month;Rank rows with window functions
Window functions keep detail rows while calculating ranks, running totals, or comparisons inside groups.
SELECT
customer_id,
order_id,
revenue,
ROW_NUMBER() OVER (
PARTITION BY customer_id
ORDER BY revenue DESC
) AS revenue_rank
FROM orders;Label rows with CASE WHEN
CASE WHEN adds if/else logic per row, for buckets, flags, and readable labels. Check IS NULL first so missing values do not fall into the ELSE branch.
SELECT
order_id,
CASE
WHEN revenue IS NULL THEN 'missing'
WHEN revenue < 100 THEN 'small'
ELSE 'large'
END AS order_size
FROM orders;Break a question into steps with WITH
A CTE names an intermediate result so the final query stays readable. It is also how you filter on aggregates or window function results.
WITH customer_totals AS (
SELECT customer_id, SUM(revenue) AS total_revenue
FROM orders
GROUP BY customer_id
)
SELECT *
FROM customer_totals
WHERE total_revenue > 1000
ORDER BY total_revenue DESC;Stack files with UNION ALL
When the same data arrives in several files, such as one export per month, stack them with UNION ALL and add a label column so you can still tell them apart.
SELECT 'january' AS source, * FROM orders_january
UNION ALL
SELECT 'february' AS source, * FROM orders_february;Clean text before you group or join
Trailing spaces and inconsistent capitals split one value into several groups. Normalize text with TRIM and LOWER before grouping, joining, or checking for duplicates.
SELECT LOWER(TRIM(email)) AS email, COUNT(*) AS copies
FROM customers
GROUP BY LOWER(TRIM(email))
HAVING COUNT(*) > 1;Continue in the editor
Open SQL for Files to add your own CSV, JSON, or Parquet files and try these examples locally in your browser.
Related guides
SQL CASE WHEN Explained: Conditional Logic, Buckets, and Conditional Aggregation
CASE WHEN is SQL's if/else. Use it to label rows, build ranges, translate codes, and turn one grouped query into a compact pivot-style report.
SQL Subqueries vs CTEs: When to Use WITH, IN, EXISTS, and Correlated Subqueries
Subqueries nest one question inside another; CTEs give each step a name. Learn which form to use, and the NULL trap that makes NOT IN return nothing.
SQL Window Functions Explained: OVER, PARTITION BY, Running Totals, and LAG
Window functions add context beside each row: totals, group totals, ranks, running totals, and previous values without losing detail.
SQL String Functions for Cleaning Text: TRIM, LOWER, SPLIT_PART, and REGEXP_REPLACE
Exported text is messy. A handful of SQL string functions fix most of it: trim and lowercase values, join fields safely, split out parts, and strip characters with regex.