r/SQL • • 6h ago

PostgreSQL Moving into a data analyst role next year and figuring out how much SQL to learn past joins and GROUP BY

I'm an inventory planner and I've been writing my own queries against our Postgres reporting replica for about a year, mostly joins, GROUP BY and the odd subquery. I'm moving onto the analytics team in March and work is covering a course before then. The analysts there use window functions and CTEs everywhere, and I can read them but couldn't write one cold. Which SQL topics come up day to day in analyst work, and which can wait?

11 Upvotes

11 comments sorted by

6

u/just_variousphilosop 5h ago

I'd get comfortable with CTEs first since they make longer queries way easier to read and debug. Once those click, window functions are just a matter of practice, throw a ROW_NUMBER or SUM OVER on something and you'll wonder why you waited. Stuff like recursive queries and performance tuning can definitely wait until you hit a wall.

2

u/Healthy_Coconut1636 4h ago

If you have the permissions I have found that for me learning how to make temp tables has been hugely helpful. With temp tables your can basically build your own little database in the query. Really helps with breaking down complex queries into their fundamental parts. Later on it also helps you identify bottle necks that may slow down query performance. Once you have a final query working you can run it through AI and just ask it to convert to CTEs before pushing to prod.

1

u/Dangerous_Cup3607 5h ago edited 5h ago

CTE is just a wrapper for blocks of code executed in sequence. Also dates shifting can be an important aspect in sql such as last_day, date_trunc, to_date, sysdate-1, add_months etc. Such that you can dynamically create a CTE of dates such as beginning of this year until date that gets feed into the rest of the program.

with a
(
Do Something
),

b
(
Do Something
),

c
(
Do Something
)

Select everything from a,b,c

1

u/PuzzleheadedTax7404 4h ago

Since you can already read them, the fastest way in imo is rewriting a few of your own reports with window functions instead of the subgueries you use now. Your own data teaches more than toy tables do. If work is paying for something strucyured. Boot.dev has a data track that pairs SQL with Python and pandas, and Springboard has a data analytics bootcamp with a mentor if you'd rather have someone checking your work.

1

u/Candid_Recover3783 4h ago

Window functions for sure, ROW_NUMBER and LAG especially

1

u/kagato87 MS SQL 4h ago edited 4h ago

You're in for a fun ride.

CTEs and window functions are still basic. Understanding cardinality and relationships, and how certain patterns can interact is the real meat.

You know, WHEN to use each tool. I've seen subqueries used on a way that destroys the query. I've seen windows destroy the query. Even though both are powerful tools that can bring great speed.

And that's before performance and making actually fast queries.

1

u/Free-Good9203 3h ago

Since you are already querying a Postgres reporting replica for inventory planning, two Postgres-specific features will save you a ton of pain right after CTEs and window functions:

  1. FILTER (WHERE ...) on aggregates (for example SUM(qty) FILTER (WHERE status = 'backorder')). It replaces messy nested CASE WHEN pivots and makes multi-metric summary queries way easier to read and debug.
  2. generate_series() + LEFT JOIN for building clean date spines. In inventory and sales analytics, days with zero movement disappear from raw tables and silently skew 7-day or 30-day moving averages unless you left-join against a continuous date series.

Once you add those on top of CTEs + ROW_NUMBER / LAG / running SUM() OVER, you will cover 90%+ of real day-to-day analyst work.

1

u/DEE_PIZZLE 2h ago

Window functions and CTEs are the right instinct, thats most of what separates analyst SQL from report SQL. After that its date handling, how NULLs behave in joins and aggregates, and reading a query plan well enough to know why something takes 4 minutes. For the course part, Boot.dev's Learn SQL course has a chapter on indexes, the piece most report writers never touch, and you write and run every query yourself.

1

u/ProgrammerFew504 2h ago

Window functions amd CTE.

1

u/Persistent_Port 34m ago

Agree with the window functions comments. Learn ROW_NUMBER and LAG and youll look like a wizard to whoever is still doing it in Excel