r/SQL • • 5d 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?

18 Upvotes

20 comments sorted by

20

u/just_variousphilosop 5d 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.

4

u/Healthy_Coconut1636 5d 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/kktheprons 4d ago

Be careful with this -- CTEs can be much less performant than using temp tables, depending on your data sets and indexes.

1

u/A_name_wot_i_made_up 4d ago

The difference between row_number, rank, and dense_rank is small, but super important.

4

u/ReleaseTheBlacken 4d ago

Try the free sql library at learn dot inkwelltools dot com

3

u/kagato87 MS SQL 5d ago edited 5d 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.

2

u/Dangerous_Cup3607 5d ago edited 5d 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

2

u/PuzzleheadedTax7404 5d 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/Free-Good9203 5d 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 5d 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 5d ago

Window functions amd CTE.

1

u/Persistent_Port 4d 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

1

u/major_clout21 4d ago

ROW_NUMBER() OVER (PARTITION BY X ORDER BY Y)

1

u/Discordbothost 4d ago

In Postgres, ROW_NUMBER can pick either row when the sort keys tie. Try finding the latest stock count per SKU, then add two counts with the same timestamp. Add the count ID to ORDER BY to make the choice consistent.

1

u/Funny-Ocelot8202 4d ago

Window functions & CTE. Utmost priority. I’d also recommend getting comfortable with functions such as CASE WHEN & then CAST (especially for those pesty date/ timezone datatypes). As you practice , you would realize how it makes your query simple & efficient. Goodluck mate!

1

u/tommyfly 4d ago

Learn about query performance. You'll save your DBA some headaches.

1

u/lilbrofm 1d ago

Past joins and grouping analyst zal depth shows in window fucntions and clean CTEs since those turn messy event data into readable reports Bootdev could help add programming alongside swl for heavier data work though vendor documentation also covers the advanced clauses

2

u/madam_zeroni 4d ago

90% fine with just Joins and groups. But that 10% is gonna kill you. Learn some basic window functions