Thanks. I learned a lot. I'm pretty new to the industry. I am working with SQL every day so videos that start with "what is SQL" and "this is a SELECT statement" are really not what I need but I am not in the level to write good/optimal code. The video was exactly what I needed. And not too long and not too short. Everything structured well.
@weiyang86503 жыл бұрын
thank you for your video, but WHERE should be put before the GROUP BY clause, right?
@Sanatos983 жыл бұрын
Yes!
@stratascratch3 жыл бұрын
Whoops. Yes! haha sorry about that.
@CruiserPup2 жыл бұрын
I've learned so much from your videos and my stratascratch premium subscription in the past year. Thank you thank you thank you! :)
@Sanatos983 жыл бұрын
I love your videos! Hope you get the recognition you deserve brother
@StanleySI3 жыл бұрын
Hi Nate, are you sure we can use where clause after group by clause ? [HAVING vs WHERE: (12:48)]
@stratascratch3 жыл бұрын
Nope! I made a mistake! I should have ran the code to double check to see if it worked. All you have to do is place the WHERE clause before the GROUP BY and it's fixed.
@StanleySI3 жыл бұрын
@@stratascratch The question asked 'Include only records where non-paying customers have more downloads than paying customers" - this may imply we need to use WHERE clause first to filter individual records, then aggregate results with GROUP BY date.
@waskjohnson2033 Жыл бұрын
Thank you as this helped me think differently around optimization, and that it's more intuitive than big and scary. At work I review a lot of other's queries (internal & client) and code readability is *always* an issue. Drives me insane as I always have to rewrite/reformat them before I even bother trying to understand them.
@stratascratch Жыл бұрын
Great to hear!
@bien.papachin3 жыл бұрын
great video but as a a rule of thumb run the SQL to see if it works, for example the 'where' clause at the last select was left after the 'group by' , nonetheless the explanation is great! thanks for the video... cheers
@stratascratch3 жыл бұрын
Yea you're right. I totally forgot to run the SQL query at the end to see if it worked. =)
@chaitanyas37411 ай бұрын
🎯 Key Takeaways for quick navigation: 00:00 *📝 Importance of Code Structure* - Understanding the importance of code structure for comprehension and logic, - Exploring a real SQL interview question to demonstrate code structuring, - Highlighting the significance of well-structured code in job interviews and professional work environments. 02:41 *🛠️ Refactoring Nested Queries* - Refactoring nested queries for clarity and improved logic flow, - Demonstrating the use of common table expressions (CTEs) for better code organization, - Emphasizing the importance of understanding and manipulating data before applying additional logic. 07:14 *🔄 Ensuring Consistent Aliases* - Utilizing consistent and descriptive aliases for tables and columns, - Improving code readability and comprehension through clear naming conventions, - Highlighting the significance of explicitness in code for future reference and collaboration. 10:42 *📊 Eliminating Unnecessary ORDER BY Clauses* - Streamlining code by reducing redundant ORDER BY clauses, - Optimizing code execution for efficiency and performance, - Emphasizing the importance of minimizing unnecessary code execution for code optimization. 12:47 *🧹 Simplifying Code with CASE Statements* - Simplifying code logic using CASE statements for concise and efficient code, - Demonstrating optimization by reducing the number of common table expressions (CTEs), - Streamlining code structure for improved readability and maintenance. 13:02 *🔍 Choosing Between HAVING and WHERE Clauses* - Differentiating between HAVING and WHERE clauses based on usage and optimization, - Opting for WHERE clauses over HAVING clauses for simpler and faster query execution, - Highlighting the importance of understanding SQL nuances for code optimization.
@israelgonzalez6773 жыл бұрын
In love with your videos. I am enjoying them a lot. Greetings from MX! 😀
@sagarmgandhi3 жыл бұрын
I think I could apply all this when I write my queries. Good session
@ahyoungkim82563 жыл бұрын
i love this contents, this is so useful to look back our codes! thank you so much Nate !
@insider-training14392 жыл бұрын
Very helpful video Nate, thanks for doing this.
@totleariss3 жыл бұрын
Love this video! I’m at the point where I can get through a lot of the problems, but the code is a mess and way longer than the optimized solution. It’s been a challenge to “think in SQL” to write a better code to begin with.
@stratascratch3 жыл бұрын
That'll come with time and experience. The initial code is always a mess. Don't worry too much about it. Just optimize at a later time!
@tran34903 жыл бұрын
Super helpful! Thank you!
@jennajia95223 жыл бұрын
Hi Nate, I cannot explain how much appreciation I have to you. Your video is extremely helpful and I have almost watch every single SQL one. All of the questions you explain in a clear way and the platform you built out is also the best. You are my best SQL teacher I want to say. Thank you so much. Keep up making these, we love it.
@stratascratch3 жыл бұрын
Thanks so much for the kind words! I'll keep making more content! You'll also see much more from my team. We're hoping to release a lot more videos this year!
@luckychitundu1070 Жыл бұрын
Hi @ Nate! Does StrataScratch have a shortcut for formatting your SQL code?
@user-dl3qr5hm3t3 жыл бұрын
Do we really need “group by date, n_nonpaying, n_paying” in the last query? cte returns data already grouped by date: one row - one date. PS. Surprised that it’s on Hard level. Seems pretty basic.
@stratascratch3 жыл бұрын
The groupby only facilitates any de-duping needed. I wouldn't expect any duplicates so it's not needed. The groupby might just be an artifact of some other code that was refactored. Agree with you that it's a basic question that would be asked on an interview. Probably a medium level question tbh. But with all the CTEs with joins and case statement, we graded it hard.
@user-dl3qr5hm3t3 жыл бұрын
@@stratascratch Thanks for prompt response, Nate. Highly appreciate what you do on your channel.
@zukeplastic3 жыл бұрын
great content!
@bocanegradev2 жыл бұрын
amazing! always it's better to understand best practices with examples like this... It would be awesome to see more like this
@parantikaghosh43963 жыл бұрын
This is an amazing video, it is sooooooooooo helpful!! :)
@bandhammanikanta3 жыл бұрын
I really appreciate your efforts, Nate. but please do improve the title of the video according to the question,.
@elleandish2 жыл бұрын
nate!!! i was looking up sql videos and thought i saw a familiar face from high school 😆 nice channel, very helpful! i’m not doing data science by title but wanted to get better at sql. 💖 janelle
@stratascratch2 жыл бұрын
Wow what a blast from the past. You got a great channel yourself. Way better than mine! If you want to learn SQL, check out my platform =) We'll start resuming the SQL videos next month so I hope you check back. Also, LeetCode is another great platform with a SQL component.
@rishikeshbharti50643 жыл бұрын
GREAT VIDEO🔥🔥
@dmitriyp37023 жыл бұрын
Hello. I prefer to use USING, instead of ON in INNER JOIN string. Do think it's a good idea?
@stratascratch3 жыл бұрын
Not many people I know use USING instead of ON so I can't really comment on if it's a good idea or not. Out of everyone I've ever interviewed for a job, I don't think anyone has ever used USING.
@amitbarak94758 ай бұрын
I dont understand why group by is necesery in the last select query
@ManishaParmar2 жыл бұрын
Really great optimizing techniques! These are some of the key things that I keep in mind while writing my solutions as well, and I like how you explain every step.
@alfatmiuzma3 жыл бұрын
Loved this video❤️. Everything is on point! I really liked the optimization techniques using WITH, CASE statements! Looking forward to more videos on SQL. Thanks a ton😊
@rakibraihan15723 жыл бұрын
great
@dhanushph81703 жыл бұрын
Can anyone help me , which is the best site to learn the syntax for window functions
@stratascratch3 жыл бұрын
Try mode analytics SQL tutorial for a free tutorial. That's how I learned how to write window functions. Then try StrataScratch to practice and get better. Good luck!
@diaconescutiberiu75353 жыл бұрын
Quick question (I've just started with sql). From what i learned so far for case when...then ... statement, after the "then" comes labeling (strings; all examples were using case when for binning). In your code i saw something else (dowlnds.downloads) ...what did you do here? (Is it also a labeling but with a downlds reference, or...?)
@stratascratch3 жыл бұрын
I'm just referencing the ms_download_facts table. I'm calling it downlds as you can see from the JOIN statement. So when I am referencing columns, SQL knows what table the column is in.
@diaconescutiberiu75353 жыл бұрын
@@stratascratch Got it. I was able to replicate this on some other exercise. You are referencing the downloads (from the downlds) column so as SUM knows what to ... sum. Can you please tell me, if we were to have 2 columns (something to sum and something to count) how would the case when look like? Would you just write 4 case when(s)? (2 with sums for paying & non_paying and 2 with counts for paying & non_paying)... or is it possible to optimize further into 2 case when(s)
@stratascratch3 жыл бұрын
@@diaconescutiberiu7535 It would be 4 cases just as you mentioned. You can't combine sum and counts when writing the cases. And you still need to split paying and non-paying so it's 4 cases in the end.
@raxmatillomaribjonov37873 жыл бұрын
thanks👍
@abaji4343 жыл бұрын
great!
@henrold62283 жыл бұрын
nate you are my best friend
@BBBBBBAAAl3 жыл бұрын
perfect
@de_da_oo3 жыл бұрын
Such a great video!
@nandiniguntur45093 жыл бұрын
Hey Nate! Kudos to the great series. You are a good tutor. I have a request, can you please make a playlist for machine learning & statistics with python? It would be extremely helpful.
@stratascratch3 жыл бұрын
We are definitely doing this soon!
@ayeoh472 жыл бұрын
Cleaner if you use an IF statement instead of CASE!
@stratascratch2 жыл бұрын
IF statement in SQL?
@techiewithcamera2 жыл бұрын
My Solution: with user_table as( select t1.user_id, t2.paying_customer from ms_user_dimension t1 inner join ms_acc_dimension t2 on t1.acc_id=t2.acc_id ), final_res as( select distinct t1.date, t2.paying_customer, sum(downloads) over (partition by date, paying_customer order by date) sum_downloads from ms_download_facts t1 inner join user_table t2 on t1.user_id=t2.user_id) select t1.date, t1.sum_downloads as "non-paying downloads", t2.sum_downloads as "paying downloads" from (select * from final_res where paying_customer='no') t1, (select * from final_res where paying_customer='yes') t2 where t1.date=t2.date and t1.sum_downloads>t2.sum_downloads order by 1 desc;