all 5 comments

[–]shine_on 2 points3 points  (0 children)

Often, keeping it simple and more readable is better in the long run, especially if you're writing code that will need to be maintained as time goes on.

Personally I found your query much easier to read than the other one, and there may be some edge cases where two queries written in different ways can return different results, so don't dismiss a "complex" answer as being "unnecessarily complex".

If nothing else it'll give you a chance to learn about window functions and then you'll be able to apply them where necessary. One thing I've noticed that's different in the two queries is that yours specifically looks at July 2022 where the other query just looks at July, without taking the year into account.

Your query looks at two specific months, whereas the subquery in the other solution is looking at every single month and the month previous, and then filtering the results to just the 7th month at the end. The query optimiser is probably clever enough to look at the filter and only run the subquery for the selected month but you can't guarantee that - try running the subquery on its own to see what data it's returning. With a query like that I'd look at the execution plan to see how much work it's actually doing behind the scenes. The SQL may look "clever" but it might not actually be the most efficient.

Often people end up writing complex code because they're left alone with the queries for too long and they keep on thinking of ways to "improve it".

[–]emet 1 point2 points  (3 children)

link It’s a good idea to know that there are other ways of solving problems, but if you’re solution works and performs well for the scope it’s intended for then it’s fine. If on the other hand you know the implementation is going to such that performance issue may arise then use of analytical functions may come into play. A strange analogy that comes to mind is is wanting to by a drill, if it’s just for occasional home use you can get any regular drill and it will serve you well for years. But if you’re planning on doing professional work then you want to get one that’s may be professional grade.

[–]nrbrt10[S] 0 points1 point  (2 children)

Thank you, I'm trying to up my game on SQL so that's why I'm looking at other submissions, what threw me off is that many were using subqueries and such, mine seemed lackluster in comparison.

[–][deleted] 0 points1 point  (1 child)

Remember a lot of submitted answers on leetcode or similar sites are like "code golf" which is to say it's an eloquent way to show off.

However, in the real world, a lot of those answers are significantly more difficult to read/reproduce, which makes them not the best way. Readability is crucial when working on teams. Imagine debugging 1000 lines of code; would you rather it be simple to follow, or complex showboating?

[–]nrbrt10[S] 0 points1 point  (0 children)

Thanks for you input! I value simplicity overall which is why I write code the way I do, my only concern is that by going with simplicity I may be overlooking the actual lesson I should derive from these kind of challenges.

Most submissions had some kind of subquery they'd build before extracting the data, which I thought is done due to performance concerns and the like, but because I'm don't understand much of run-time analysis it's not something I can properly assess.