SQL Window Functions: The Key to Succeeding in Data Science Interviews

แชร์
ฝัง
  • เผยแพร่เมื่อ 21 ก.ค. 2024
  • Window functions are a part of SQL that is useful on a daily basis for data scientists. This video will give you an overview of SQL window functions by explaining:
    - When are window functions
    - Syntax of window function
    - How to define a window
    - How to select a function
    👉 Window function problems in Leetcode: / sql-window-functions
    ✔️ 3 types of SQL questions to ace interviews: • Ace Your Data Science ...
    ✔️ SQL Window Function with Examples • Master SQL Window Func...
    ✔️ T-SQL documentation by Microsoft docs.microsoft.com/en-us/sql/...
    🟢Get all my free data science interview resources
    www.emmading.com/resources
    🟡 Product Case Interview Cheatsheet www.emmading.com/product-case...
    🟠 Statistics Interview Cheatsheet www.emmading.com/statistics-i...
    🟣 Behavioral Interview Cheatsheet www.emmading.com/behavioral-i...
    🔵 Data Science Resume Checklist www.emmading.com/data-science...
    ✅ We work with Experienced Data Scientists to help them land their next dream jobs. Apply now: www.emmading.com/coaching
    // Comment
    Got any questions? Something to add?
    Write a comment below to chat.
    // Let's connect on LinkedIn:
    / emmading001
    ====================
    Contents of this video:
    ====================
    00:00 What is a Window Function
    02:28 Syntax of Window Functions
    03:23 Define a Window Frame using Over Clause
    04:00 PARTITION BY
    04:50 ORDER BY
    05:23 ROWS/RANGE
    08:35 Aggregate Functions
    09:11 Ranking Functions
    11:16 Analytic Functions

ความคิดเห็น • 53

  • @emma_ding
    @emma_ding  2 ปีที่แล้ว +12

    Thanks DJ jiang for pointing out that negative offset of LEAD or LAG works in PostgreSQL and Snowflake. I was referring to T-SQL that negative offset is not allowed and I should have clarified it in the video. Details here docs.microsoft.com/en-us/sql/t-sql/functions/lag-transact-sql?view=sql-server-ver15.

    • @tahirmuhammad9646
      @tahirmuhammad9646 2 ปีที่แล้ว

      Hey Emma, these are great! Can you put this in a playlist called ACE The SQL interview and put future videos here about the topic? :) TYVM!

    • @emma_ding
      @emma_ding  2 ปีที่แล้ว

      @@tahirmuhammad9646 Great suggestion, Tahir! Here you go th-cam.com/play/PLY1Fi4XflWStrBVJc2o9SbSQp9dRt3l9W.html! :)

  • @QuantumImperfections
    @QuantumImperfections 2 ปีที่แล้ว +11

    This..... WAS FANTASTIC! I've got 4+ years of experience in SQL and I needed a quick refresher for window functions and this was exactly what I needed. Thank You!

    • @emma_ding
      @emma_ding  2 ปีที่แล้ว

      Thanks! Glad you find it helpful :)

  • @raquelc.4073
    @raquelc.4073 11 หลายเดือนก่อน +2

    This is the best video about window functions that I have seen so far! Wow! Congrats and thanks!

  • @austinbai9641
    @austinbai9641 2 ปีที่แล้ว +2

    This is the best vedio talking about window fundction on TH-cam, thanks Emma! Please keep up the great work!

  • @danielkoo8000
    @danielkoo8000 ปีที่แล้ว +1

    Thanks Emma for sharing. What you shared helped me comprehend window functions. Most importantly, I applied those window functions to my day to day work, which boosted efficiency. Appreciated :)

  • @krishnamvats2238
    @krishnamvats2238 ปีที่แล้ว +2

    stopped in between of the video, the best part about the video is that it explains the difference between group by and window function. This is something not many youtuber do. I was struggling to visualize how it happens. Thanks a lot

    • @emma_ding
      @emma_ding  ปีที่แล้ว

      Glad to hear it was helpful for you, Krishnam! Thanks for watching. 💛

  • @xiaomengwu3399
    @xiaomengwu3399 2 ปีที่แล้ว +2

    Your channel is the best on all DS interviews! well organized, clear and short!

    • @emma_ding
      @emma_ding  2 ปีที่แล้ว

      Wow, thanks!

  • @LorenzoAnderson-ih5ke
    @LorenzoAnderson-ih5ke ปีที่แล้ว +1

    Super clear and orginised, thank you!!! 👍

  • @osoriomatucurane9511
    @osoriomatucurane9511 6 หลายเดือนก่อน +1

    This is awesome, crystal clear!

  • @swastikchowdhury9655
    @swastikchowdhury9655 ปีที่แล้ว +1

    This was very helpful. Beautiful content

  • @sofiayz7472
    @sofiayz7472 2 ปีที่แล้ว +1

    Great content as always!

  • @alienfunbug
    @alienfunbug หลายเดือนก่อน

    This video is so condensed and packed full concise and well explained materials. Thank you for the fantastic content.

  • @TechWithAbee
    @TechWithAbee 2 หลายเดือนก่อน +2

    Thank you very much! 🔥

  • @sw2144
    @sw2144 2 ปีที่แล้ว

    Emma luv u. Your explanations are very elaborative and succinct at the same time.

  • @AVChian01
    @AVChian01 ปีที่แล้ว

    Great lecture!!! Exactly what I was looking to solve my problem at work. I am definitely subscribing to your channel.

    • @emma_ding
      @emma_ding  ปีที่แล้ว

      So happy to hear you found my video helpful! Thanks for watching. 😊

  • @FelipeCampelo0
    @FelipeCampelo0 หลายเดือนก่อน

    I have been storing so many CTEs using the WITH statement lol. I think these window functions could make my queries less verbose.
    Great content!

  • @maweryoutube9852
    @maweryoutube9852 10 หลายเดือนก่อน

    Very clear, detailed, and amazing explanation
    SUBSCRIBED from more great videos adventure
    Thank you

  • @djjiang3718
    @djjiang3718 2 ปีที่แล้ว

    Hello Emma, your videos are superb and great and very useful! Just one thing, we can use negative value when we use LEAD or LAG, basically lead(date,-1) over (order by date) equivalent of lag(date,1) over(order by date), which I tested in PostgreSQL and Snowflake, and they both works.

    • @kirusha_kras
      @kirusha_kras 2 ปีที่แล้ว +1

      Yes, but it's syntactic sugar) For example in McSQL it won't work

    • @emma_ding
      @emma_ding  2 ปีที่แล้ว +1

      Thanks DJ for pointing it out! I was focusing on T-SQL docs.microsoft.com/en-us/sql/t-sql/functions/lag-transact-sql?view=sql-server-ver15 and should have mentioned it in the video. Thanks again!

    • @djjiang3718
      @djjiang3718 2 ปีที่แล้ว

      @@emma_ding No worries, you are great! Your videos are very detailed and clear and well organized and very high level! Thank you and Keep up your hard works!

  • @rajmaharjan466
    @rajmaharjan466 ปีที่แล้ว

    Amazing explanation!

    • @emma_ding
      @emma_ding  ปีที่แล้ว

      Glad you found it helpful, Raj! 😊

  • @ashleyblanchard8993
    @ashleyblanchard8993 2 ปีที่แล้ว

    Hi Emma, what app do you use to record yourself and the screen?

  • @nishantjain1469
    @nishantjain1469 ปีที่แล้ว +1

    Shouldn't the SUM() at 9:09 do a cumulative sum since order function is mentioned ?

  • @kirusha_kras
    @kirusha_kras 2 ปีที่แล้ว +2

    Emma, your content is great. Your English is beautiful and clear. My respect
    For subscribers:
    I would like to use the comments to find ways to improve my English. If you are starting your journey in data analytics and you are a native speaker of English, we can be useful to each other) For example, weekly calls or something similar
    Best regards, Senior Data Analyst from Russia

    • @roczhang2009
      @roczhang2009 2 ปีที่แล้ว +1

      Hi there, as a non-native speaker who's working in an English-speaking country, what I found most helpful is not to have English conversations, but to pay closer attention to all the sentences and expressions that come from the native speakers. Think about how you would have phrased your words vs. how they did. Find the difference and work on it. You could use everything as the source for that purpose: movies, youtube videos etc.
      But this is not to say that having conversations in English is not helpful. It is, but just maybe not as important as many people think. Cuz I know many non-native speakers, who have worked in English every day for many years, but still speak bad English.

    • @emma_ding
      @emma_ding  2 ปีที่แล้ว

      Thank you! 😃

  • @arunabhsingh5448
    @arunabhsingh5448 2 ปีที่แล้ว

    Keep going!

  • @stonewatertx
    @stonewatertx 2 ปีที่แล้ว

    Sorry for another dumb question: I tried something like "select rank(a) over(order by a) from t" in PostgreSQL. It'll show error telling me to add WITHIN GROUP. But if I write "select rank() over(order by a) from t" then it's fine. Seems like I can't pass anything in the () after rank. Could anyone clarify?

  • @user-yb7tr3hy6j
    @user-yb7tr3hy6j 11 หลายเดือนก่อน

    awesome

  • @stonewatertx
    @stonewatertx 2 ปีที่แล้ว +2

    Great video! One quick question: At 9:10, I thought SUM(value) OVER(ORDER BY value) will return cumulative sum; but SUM(value) OVER() ORDER BY value will return the SUM shown in the video. or is it a PostgreSQL thing? I'm a little confused now.

    • @yankeephan87
      @yankeephan87 2 ปีที่แล้ว

      There must be an error in her table bc it should be a rolling sum bc the sum(value) cannot be the same if it is ordered by the value for all rows

    • @fkarcher7149
      @fkarcher7149 2 ปีที่แล้ว +1

      " I thought SUM(value) OVER(ORDER BY value) will return cumulative sum; but SUM(value) OVER() ORDER BY value will return the SUM shown in the video".
      This is the correct statement. There must have been an error in the video.

    • @apamwamba
      @apamwamba ปีที่แล้ว

      You are 100%. Reason: (1.) When ORDER BY is specified and FRAMING is allowed, the DEFAULT FRAME is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW i.e all values up to the current value in the partition ( 2.) When ORDER BY is not specified and FRAMING is allowed, the DEFAULT FRAME is ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING i.e The entire partition

  • @user-ie5dm8zc8p
    @user-ie5dm8zc8p 10 หลายเดือนก่อน

    Emma: How are you? I am from Brazil. Do you have a video about partitions and frames?

  • @Cplanet-uo1sf
    @Cplanet-uo1sf 3 หลายเดือนก่อน

    Can you explain how this query works ?
    SELECT
    COUNT(p) OVER(order by p ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) as p_count,
    p, c,d
    FROM `d.t`
    SELECT
    COUNT(p) OVER(order by p ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) as p_count,
    p
    FROM `d.t`
    Why is the window affected when I specify c and d columns?
    I would expect to count 1 before, current one and next one. So for first row it would be 2 for next ones except last one 3 and for last one to be 3, because the window is made of 3 items so it will count 3 most of the time.
    But no.. it returns different counts that does not make any sense to me. I read the bigquery documentation and I really don't get it.

  • @minjeongkim9708
    @minjeongkim9708 2 ปีที่แล้ว

    would you mind sharing some videos for python coding interview as well?

    • @emma_ding
      @emma_ding  2 ปีที่แล้ว

      Hi Minjeong, this one might be helpful to you.
      th-cam.com/video/hAqg2dlNeUc/w-d-xo.html

  • @nishantjain1469
    @nishantjain1469 ปีที่แล้ว

    The order on the playlist is not correct

  • @billj2229
    @billj2229 2 ปีที่แล้ว

    It's sad that you can't use variable to specify rows in window functions.

  • @MrPushupp
    @MrPushupp ปีที่แล้ว

    but why does the rank function skip 4 and 6 though??

    • @apamwamba
      @apamwamba ปีที่แล้ว

      Its by design. Think Olympics when there is a tie.

  • @ashleyblanchard8993
    @ashleyblanchard8993 2 ปีที่แล้ว

    i don't get rank and dense rank

  • @IKostman
    @IKostman ปีที่แล้ว

    Nice job young lady 🙂

  • @andyw810
    @andyw810 4 หลายเดือนก่อน

    The table at ~9:00 is wrong. should go with running total, running average, etc.