SQL Window Functions: Explained (with examples)

Поділитися
Вставка
  • Опубліковано 2 січ 2025

КОМЕНТАРІ • 66

  • @eminjs2319
    @eminjs2319 2 роки тому +11

    First of all, thanks for clarification. Second, just to warn some people like me who could potentially be stumped by a little typo: in 6:09, in the presentation, write to the order_id 5 the date also 2022--04-03, and not 2022-03-03, otherwise we would not have a logical grouping. Again, I want to than you again Ben: only after watching this video I truly feel that I grasp the window functions completely. Kudos to your amazing clarification skills.

    • @DatabaseStar
      @DatabaseStar  2 роки тому

      Great, thanks for the correction and advice!

  • @andresil8330
    @andresil8330 2 роки тому +7

    Thanks, Ben! You nailed in the explanation and can show things in a very clear way.

  • @Port0101
    @Port0101 Рік тому +1

    You have done great when it comes to good video creation and Knowledge delivery. Nice! Learned what I needed. Nice!

    • @DatabaseStar
      @DatabaseStar  Рік тому

      Thanks! I’m glad you liked the video.

  • @alaeeddinehajji
    @alaeeddinehajji 2 роки тому +2

    you have the best SQL playlist, Thank you

  • @shirtless32
    @shirtless32 Рік тому +2

    Great teacher!!! One of the best I have seen.

  • @osoriomatucurane9511
    @osoriomatucurane9511 5 місяців тому

    Another Awesome tutorial, crystal clear and concise by the SQL Master! This man is truly a legend! After numerous tutorial, I finally got my head arround the order by argument in the over ( ) clause. Thank you a lot. This is the relative sorting against or over the particular group/window.

    • @DatabaseStar
      @DatabaseStar  5 місяців тому +1

      Thanks! I'm glad it helped you understand.

  • @huffypuff0
    @huffypuff0 Рік тому +4

    This is honestly such an amazing video. Thank you so much.

  • @nataliataranova7480
    @nataliataranova7480 Рік тому

    Clear & informative tutorial, thank you!

  • @samplaying4keeps
    @samplaying4keeps Рік тому

    Thank you so much for this. It was very clear and helpful.

  • @shiwanginishishulkar4744
    @shiwanginishishulkar4744 10 місяців тому

    very well explained

  • @mohammadreza.beygifard
    @mohammadreza.beygifard 5 місяців тому

    Great Video!

  • @parvezshahid8906
    @parvezshahid8906 Рік тому

    YOU ARE THE BEST MATE!

  • @huntermyers1046
    @huntermyers1046 Рік тому

    Thanks, this was helpful.

  • @stephenm3874
    @stephenm3874 2 роки тому

    Well presented!

  • @ModernPulse1
    @ModernPulse1 2 роки тому

    Great explanation. Thanks!

  • @severtone263
    @severtone263 2 роки тому

    This was very helpful! TY

  • @BetterWithOliver
    @BetterWithOliver 2 роки тому

    Really helpful! Thanks!

  • @mauroldanr
    @mauroldanr 3 місяці тому

    Are te windows function good for performance compared to other options retriving same results?

    • @DatabaseStar
      @DatabaseStar  3 місяці тому

      Good question. It depends on the size of your table and several other factors, but I've found that they perform better than using a correlated subquery (which is an alternative way of getting the same data). You could test other approaches on your database and see if it is better or worse.

  • @hintsoftware
    @hintsoftware Рік тому

    Can we use where clause or any kind of filters in any way for each windows function seperately ?
    We are unable to fully utilize window function without where clauses... or any trick to do so in sql server ?

    • @DatabaseStar
      @DatabaseStar  Рік тому

      Yes you should be able to use a window function and where clauses.
      The window function does the calculation and the where clause does the filtering.
      I don’t know if you can have separate where clauses for each window function. Perhaps surrounding it with a case statement?

    • @hintsoftware
      @hintsoftware Рік тому

      @@DatabaseStar can you please advise how to use where clause along with over clauses in windows function ?

  • @ChiaMIaLALALA
    @ChiaMIaLALALA 2 роки тому

    great video! very helpful! 😊

  • @cindystokes8347
    @cindystokes8347 Рік тому +1

    When adding the partition, I accidentally did not grab the "order by" for the outer query and it worked perfectly. So rather than add additional code to make it work, makes more sense to let it run with less code? Discovered this completely by accident. Here is the code.
    Select order_id,
    order_date,
    order_total,
    SUM(order_total) OVER (
    PARTITION BY order_date
    ORDER BY order_id ASC
    ) AS running_total
    From orders

    • @DatabaseStar
      @DatabaseStar  Рік тому +2

      Thanks for the comment and the code. Yes, this query would run because you don't need that final Order By statement.
      Without that last Order By statement, the records are not shown in any specific order. The running total calculation for each row is still correct, but because the rows can be displayed in a different order, it may seem like the running total is jumping around or not in an order.
      Adding the Order By to the end will make the results look clearer or look like the running total is being shown in the right order. But, without it, the calculation is still correct as you have mentioned.

  • @A_View_From_The_Shire
    @A_View_From_The_Shire Рік тому

    Could you not achieve the same results using a GROUP BY clause?

    • @DatabaseStar
      @DatabaseStar  Рік тому

      Yeah you could achieve a similar result. However, window functions allow you to perform a calculation on one group of data and display the data in another group. A Group By will both display and calculate on the same group of data.

  • @tamiratabebe6623
    @tamiratabebe6623 4 місяці тому

    How to databases configuration?

  • @ziad9533
    @ziad9533 Рік тому

    I like the video and the channel content looks cool => subscribed

  • @tajudeen2
    @tajudeen2 2 роки тому

    Nice 👍

  • @hasanmougharbel8030
    @hasanmougharbel8030 2 роки тому

    Hey man, glad to meet again.
    I have only a simple enquiry as a new sql learner.
    How order by clause differs from rank function in sql?
    Thanks for taking care of this.

    • @DatabaseStar
      @DatabaseStar  2 роки тому

      Good question. The Order By clause will sort the data in the order you specify. The Rank function will calculate where a row will fit in an order based on a criteria, but it doesn't change how the data is ordered.

    • @hasanmougharbel8030
      @hasanmougharbel8030 2 роки тому

      @@DatabaseStar
      I learned that rank () should be accompanied with over and order by clauses....So rank does dictate a change in the order of records? is that true?
      Here a sample of rank function in my query
      select id, rank() over(order by id asc)
      from val;
      Thanks

  • @Leopar525
    @Leopar525 2 роки тому

    BigQuery as well

  • @БарзуПарпишоев

    👍👍

  • @yourfutureself4327
    @yourfutureself4327 Рік тому

    💙

  • @saireddyksr
    @saireddyksr 2 роки тому

    ♥️🙏🤝

  • @pging8328
    @pging8328 2 роки тому

    appreciate the video - but you're introducing concepts a little too quickly here - namely the frame -- the default frame - unbounded preceding to the current row - and this is a little too tricky for people to grasp without a direct explanation - the running total would make sense in light of the default frame.

    • @DatabaseStar
      @DatabaseStar  2 роки тому

      Thanks for the feedback! Window functions are a hard concept to understand and explain, so I appreicate the feedback on how I've explained it!

  • @Pranjal-AI
    @Pranjal-AI Рік тому

    Very helpful, Thanks!