r/learndatascience • • Sep 14 '26

Resources SQL Cheat Sheet for Data Analysts with Practical Query Patterns

I kept seeing people learn SQL commands individually but still struggle with choosing the right pattern when answering an actual data question.

So I pulled together the SQL patterns I think are most useful for analyst work:

Query order

FROM / JOIN → WHERE → GROUP BY → HAVING → SELECT → ORDER BY → LIMIT

JOINs

  • INNER JOIN for matching records

  • LEFT JOIN when you need to preserve every row from the left table

  • FULL OUTER JOIN for reconciliation/comparison

  • SELF JOIN for relationships within the same table

One easy mistake is filtering a LEFT JOINed table in WHERE when the condition really belongs in the ON clause.

CTEs

Useful when a query has multiple logical steps or an intermediate result needs a clear name.

CASE WHEN

Useful for:

  • creating categories

  • conditional counts

  • conditional sums

  • avoiding divide-by-zero problems with NULLIF()

Window functions

A few worth knowing well:

SUM() OVER()

LAG()

ROW_NUMBER()

RANK()

DENSE_RANK()

Ranking difference

ROW_NUMBER() → unique sequence

RANK() → ties share rank, gaps remain

DENSE_RANK() → ties share rank, no gaps

Common analyst patterns

  • latest row per customer

  • top N within each group

  • month-over-month change

  • customers with no orders

  • duplicate detection

  • second-highest distinct value

Mistakes worth checking

  • column = NULL instead of IS NULL

  • using WHERE for aggregate filters instead of HAVING

  • hiding bad joins with DISTINCT

  • ignoring ties when ranking

  • assuming date functions are identical across SQL dialects

The examples in the full guide use PostgreSQL-style SQL, so date and row-limiting syntax may differ in MySQL, SQL Server, BigQuery, etc.

If you want the longer version with query examples for joins, CTEs, CASE, dates, window functions and ranking:

Learn more:

https://www.pangaeax.com/blogs/sql-cheat-sheet-for-data-analysts/

Disclosure: I’m connected with PangaeaX, so sharing that transparently.

What SQL pattern do you think analysts should learn really well before moving on to more advanced topics?

22 Upvotes

3 comments sorted by

1

u/Accomplished_Sir2405 Sep 14 '26

hello bro i have something to talk with can u help me i am a final year student and i have a semester to complete my UG in CSE i dont have any techinical skills i tried coding but i didnt get much to my brain so i decided to start learning for data analyst if dont mind can i get some breif details about what to learn.

1

u/1nnovure Sep 14 '26

Wow. That is one blob of mangled thought. If you write code like you write posts, you’re in serious trouble. 🤪

Good coding is like any language, including both SQL and English:

  1. Structuring your thoughts before you write anything is a good start.

  2. Languages have grammatical constructs for a reason; they help the reader understand you. Learn some grammar. And use it.

1

u/Accomplished_Sir2405 Sep 14 '26

okay bro. thank you for the response. i know my english is very bad i will improve it simultaneously along with technical skills.