r/dataengineering • u/El_Cato_Crande • Sep 08 '23
Help SQL is trash
Edit: I don't mean SQL is trash. But my SQL abilities are trash
So I'm applying for jobs and have been using Stratascratch to practice SQL questions and I am really struggling with window functions. Especially those that use CTEs. I'm reading articles and watching videos on it to gain understanding and improve. The problem is I haven't properly been able to recognise when to use window functions or how to put it into an explanatory form for myself that makes sense.
My approach is typically try a group by and if that fails then I use a window function and determine what to aggregate by based on that. I'm not even getting into ranks and dense rank and all that. Wanna start with just basic window functions first and then get into those plus CTEs with window functions.
If anyone could give me some tips, hints, or anything that allowed this to click into place for them I am very thankful. Currently feeling like I'm stupid af. I was able to understand advanced calculus but struggling with this. I found the Stratascratch articles on window functions that I'm going to go through and try with. I'd appreciate any other resources or how someone explains it for themselves to make sense.
Edit: Wanna say thanks in advance to those who've answered and will answer. About to not have phone access for a bit. But believe I'll be responding to them all with further questions. This community has truly been amazing and so informative with questions I have regarding this field. You're all absolutely awesome, thank you
2
u/joseph_machado Writes @ startdataengineering.com Sep 09 '23
A lot of good points in the comments.
I'd also recommend understanding what these functions are meant for. Every table has a grain, basically a fancy way of saying each row will correspond to either some thing that happened in real world (aka fact table) e.g. a sale or information about a specific entity(aka dimension table) (e.g. user, employee, date, etc).
Most interview questions will involve a (1 or more) combination of the below
Show some-metric for some-entity. E.g. Show average sale price for every day: Join & GROUP BY. Here you use GROUP BY to aggregate sale price for a given day.
For some entity(s), rank the rows/keep top or bottom n rows/compare value between rows. This sort of question will involve typically involve WINDOW function. The Window allows you to specify a subset of rows (partition by) that you want to apply an operation (e.g. rank the rows/keep top or bottom n rows/compare value between rows) over.
A combination of the above. More complex questions can involve a combination of the above. e.g. Q: Show average sale price for every day, only for days which have the top 5 highest number of customers that month. This involves a GROUP BY part and a WINDOW part. While you can do this with subqueries, it can be easy to understand with a CTE, where you define them with appropriate names making understanding and modifying code easier.
The performance depends on the query engine and query planner. Hope this helps. LMK if you have any questions :)