Skip to content

Advanced SQL Guidebook


🎯 Purpose

This guidebook expands your SQL skillset with intermediate-to-advanced techniques: window functions, CTEs, subqueries, CASE logic, date math, and analytics-friendly patterns.


πŸͺŸ 1. Window Functions

πŸ”Ή ROW_NUMBER / RANK / DENSE_RANK

SELECT user_id,
       signup_date,
       RANK() OVER (PARTITION BY region ORDER BY signup_date) AS regional_rank
FROM users;

βœ”οΈ Use to find top-N per group, tie-breakers, or recency

πŸ”Ή LAG / LEAD

SELECT user_id,
       event_date,
       LAG(event_date) OVER (PARTITION BY user_id ORDER BY event_date) AS prev_event
FROM events;

βœ”οΈ Useful for session tracking, churn windows, retention

πŸ”Ή SUM / AVG OVER WINDOW

SELECT user_id,
       revenue,
       SUM(revenue) OVER (PARTITION BY user_id ORDER BY event_date) AS cumulative_revenue
FROM orders;

🧱 2. CTEs (Common Table Expressions)

WITH recent_orders AS (
  SELECT *
  FROM orders
  WHERE order_date >= CURRENT_DATE - INTERVAL '30 days'
)
SELECT customer_id, COUNT(*)
FROM recent_orders
GROUP BY customer_id;

βœ”οΈ Improves readability, allows modular queries


πŸ” 3. Subqueries

πŸ”Ή Inline Scalar

SELECT id, (SELECT MAX(score) FROM scores WHERE user_id = u.id) AS top_score
FROM users u;

πŸ”Ή Derived Tables

SELECT region, AVG(spend)
FROM (
  SELECT region, user_id, SUM(amount) AS spend
  FROM transactions
  GROUP BY region, user_id
) t
GROUP BY region;

πŸ“… 4. Date + Time Functions

-- Extract month from timestamp
SELECT EXTRACT(MONTH FROM signup_date) AS signup_month

-- Date difference in days
SELECT DATE_PART('day', CURRENT_DATE - signup_date) AS days_since_signup

βœ”οΈ Use DATE_TRUNC, EXTRACT, DATE_DIFF, INTERVAL, NOW() as needed


🧠 5. Advanced CASE Patterns

SELECT user_id,
       CASE
         WHEN spend > 1000 THEN 'high'
         WHEN spend > 100 THEN 'mid'
         ELSE 'low'
       END AS segment
FROM user_totals;

βœ”οΈ Often used with windows, bins, and funnel logic


πŸ“ˆ 6. Percentile + Quantile Logic

SELECT PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY revenue) AS median_revenue
FROM orders;

βœ”οΈ Use for medians, quartiles, decile bucketing


βœ… Analyst Use Cases

  • Rank cohorts by conversion or spend
  • Calculate LTV or retention windows
  • Create derived metrics with WITH blocks
  • Build funnel or session-based tracking

πŸ’‘ Tip

β€œCTEs + windows = SQL superpowers. Learn them once, reuse them forever.”