Essential SQL Query Cheat Sheet for Data Analysts
A structured, quick-reference syntax guide for Data Analysts covering essential SELECT queries, multi-table JOINs, GROUP BY aggregations, CTEs, and window functions.
Who This Guide Is For
Aspiring data analysts, freshers preparing for SQL technical rounds, and working analysts needing a quick reference for complex analytical syntax.
Common Workplace Use Cases:
- Extracting clean customer and order datasets from production relational databases.
- Deduplicating transactional tables using ROW_NUMBER() window functions.
- Calculating Month-over-Month (MoM) revenue shifts with LAG() and LEAD().
- Joining user demographic tables with transactional sales records.
Comprehensive Syntax & Formula Preview
Ungated Reference1. Relational Multi-Table Joins
How to join transactional records with dimension entities while properly handling NULLs.
-- Combining Orders with Customers and Products
SELECT o.order_id, c.customer_name, p.product_name, o.amount
FROM orders o
INNER JOIN customers c ON o.customer_id = c.customer_id
LEFT JOIN products p ON o.product_id = p.product_id;2. GROUP BY with HAVING Aggregation
Aggregating metric sums and filtering aggregated results after grouping.
-- Finding high-volume customer segments
SELECT customer_id, COUNT(order_id) AS total_orders, SUM(amount) AS total_revenue
FROM orders
WHERE status != 'cancelled'
GROUP BY customer_id
HAVING COUNT(order_id) >= 5 AND SUM(amount) > 10000;3. Common Table Expressions (CTEs)
Creating modular, named intermediate result sets instead of messy nested subqueries.
WITH regional_sales AS (
SELECT region, SUM(amount) AS revenue
FROM orders
GROUP BY region
)
SELECT region, revenue,
ROUND((revenue / SUM(revenue) OVER()) * 100, 2) AS revenue_share_pct
FROM regional_sales;4. Analytical Window Functions
Calculating rankings, running totals, and period-over-period differences without collapsing rows.
-- Deduplication and period-over-period variance
SELECT customer_id, order_date, amount,
ROW_NUMBER() OVER(PARTITION BY customer_id ORDER BY order_date DESC) AS recency_rank,
LAG(amount) OVER(PARTITION BY customer_id ORDER BY order_date) AS prior_order_amount
FROM orders;Students, educators, bloggers, and tech writers may cite or reference this quick-reference guide in coursework, portfolio articles, or technical guides with proper attribution:
Key Topics Covered
- Relational Joins: INNER, LEFT, and FULL OUTER syntax
- Aggregation & Filtering: GROUP BY and HAVING clauses
- Common Table Expressions: Modular WITH clauses
- Window Functions: ROW_NUMBER(), RANK(), DENSE_RANK(), LAG(), LEAD()
Apply These Formulas to Real Business Data
Master the full data analytics toolchain in our Live Online or Gurugram Classroom program with hands-on projects and interview preparation.