Window functions in SQL Server

Поделиться
HTML-код
  • Опубликовано: 6 сен 2024
  • sql server window function example
    window function sql server example
    sql server rows range clause
    sql server rows between 1 preceding and 1 following
    In this video we will discuss window functions in SQL Server
    Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our RUclips channel. Hope you can help.
    / @aarvikitchen5572
    In SQL Server we have different categories of window functions
    Aggregate functions - AVG, SUM, COUNT, MIN, MAX etc..
    Ranking functions - RANK, DENSE_RANK, ROW_NUMBER etc..
    Analytic functions - LEAD, LAG, FIRST_VALUE, LAST_VALUE etc...
    OVER Clause defines the partitioning and ordering of a rows (i.e a window) for the above functions to operate on. Hence these functions are called window functions. The OVER clause accepts the following three arguments to define a window for these functions to operate on.
    ORDER BY : Defines the logical order of the rows
    PARTITION BY : Divides the query result set into partitions. The window function is applied to each partition separately.
    ROWSor RANGE clause : Further limits the rows within the partition by specifying start and end points within the partition.
    The default for ROWS or RANGE clause is
    RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
    Let us understand the use of ROWS or RANGE clause with an example.
    Compute average salary and display it against every employee
    We might think the following query would do the job.
    SELECT Name, Gender, Salary,
    AVG(Salary) OVER(ORDER BY Salary) AS Average
    FROM Employees
    As you can see from the result, the above query does not produce the overall salary average. It produces the average of the current row and the rows preceeding the current row. This is because, the default value of ROWS or RANGE clause (RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) is applied.
    To fix this, provide an explicit value for ROWS or RANGE clause as shown below. ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING tells the window function to operate on the set of rows starting from the first row in the partition to the last row in the partition.
    SELECT Name, Gender, Salary,
    AVG(Salary) OVER(ORDER BY Salary ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS Average
    FROM Employees
    The same result can also be achieved by using RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
    Well, what is the difference between ROWS and RANGE
    We will discuss this in a later video
    The following query can be used if you want to compute the average salary of
    1. The current row
    2. One row PRECEDING the current row and
    3. One row FOLLOWING the current row
    SELECT Name, Gender, Salary,
    AVG(Salary) OVER(ORDER BY Salary ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS Average
    FROM Employees
    Text version of the video
    csharp-video-tu...
    Slides
    csharp-video-tu...
    All SQL Server Text Articles
    csharp-video-tu...
    All SQL Server Slides
    csharp-video-tu...
    All Dot Net and SQL Server Tutorials in English
    www.youtube.co...
    All Dot Net and SQL Server Tutorials in Arabic
    / kudvenkatarabic

Комментарии • 117

  • @salonikalsulkar363
    @salonikalsulkar363 11 месяцев назад +15

    It's been 7 years and still, this tutorial series is far better than every other material on SQL :)
    Hands Down, Thanks a lot!

  • @mostafatarek5088
    @mostafatarek5088 5 лет назад +155

    give this guy a medal

    • @RohitKumar-qo7pt
      @RohitKumar-qo7pt 3 года назад

      why ?

    • @IamDanish99
      @IamDanish99 2 года назад +6

      @@RohitKumar-qo7pt Because of the clarity in his presentation, the lucid way of explaining such concepts. He deserves to get a medal.

    • @shazibnomair7010
      @shazibnomair7010 Год назад

      Seriously.

  • @venkatashasidharreddychali7485
    @venkatashasidharreddychali7485 Год назад +2

    Hello Venkat gaaru, great work . Just to add on for the default value of ROWS or RANGE values in OVER() Clause, which varies based on ORDER BY clause
    --The default value of ROWS or RANGE clause is "RANGE BETWEEN UNBOUNDED PRECEEDING AND CURRENT ROW" If the ORDER BY clause is specified
    --The default value of ROWS or RANGE clause is "ROWS BETWEEN UNBOUNDED PRECEEDING AND UNBOUNDED FOLLOWING" If the ORDER BY clause is not specified i.e the entire window.
    Please correct me, if anything is wrong. Kudos to your work and Thank you🙏.

  • @krzysztofs8535
    @krzysztofs8535 7 лет назад +29

    Best tutorial available on the net . Thank you for your hard job . You are the best teacher on the planet. The way of teaching is just superb! Greetings from Poland !

  • @7aydarah
    @7aydarah Год назад +4

    After 20 years as software developer, it's the fist time that I pay attention to the adventage of window function, I'v been using the subqueries to get this kind of results, but with a very low performance in execution time.

  • @TheBaBaLand
    @TheBaBaLand 5 месяцев назад +1

    Great video! Thanks a lot!
    One thing I would add is that instead of writing BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING statement, we could remove the ORDER BY entirely and just write:
    AVG(SALARY) OVER() as Average

  • @alekzhdo
    @alekzhdo 7 лет назад +4

    This is gold! I have been struggling to understand Window functions and syntax and your explanation makes it all clear now.

  • @frenkyb123
    @frenkyb123 6 лет назад +19

    if you use just this: AVG(Salary) OVER() - then whole set is used for computing average. But as soon you put order by into OVER, default value for ROWS and RANGE are taken into calculation. I would expect that just using ORDER BY should not affect calculation of AVG, but it does - it completely changes result set on which calculation is made.

    • @saurabhdsawant
      @saurabhdsawant 5 лет назад

      would test , thanks for letting us know

    • @mermaidviv4517
      @mermaidviv4517 3 года назад +1

      Yes, AVG(Salary) OVER () seems to have the same result

    • @meghasyam427
      @meghasyam427 3 года назад

      Nice tip

  • @RubCalBat
    @RubCalBat 2 месяца назад +1

    Amazing explanation. Thank you!

  • @AhmadAbdallah0395
    @AhmadAbdallah0395 4 года назад +2

    I don't know what to say to you. Thank you very much. You saved me a lot of time and I'm in the middle of exams, usually I would've spent a lot of time searching for an even 50% good tutorial, but your videos are 100% good and precise.
    Keep up the great work. I hope you do more topics.

  • @AW-ld7uk
    @AW-ld7uk 7 лет назад +1

    You're a natural born teacher. You're doing what you were born to do. Keep up the good work!

  • @danieladetayo3711
    @danieladetayo3711 Год назад +1

    This is the best tutorial on windows functions on youtube. Thank you so much for this.

  • @just3963
    @just3963 4 года назад +11

    What an amazing teacher! Thank you so much for breaking down these concepts for us! :)

    • @Csharp-video-tutorialsBlogspot
      @Csharp-video-tutorialsBlogspot  4 года назад +3

      Hello Justin - Thank you very much for the feedback. Means a lot. I have included all the sql tutorial videos, slides and text articles in sequence on the following page. Hope you will find it handy.
      www.pragimtech.com/courses/sql-server-tutorial-for-beginners/
      When you have some time, can you please leave your rating and valuable feedback on the REVIEWS tab. It really helps us. Thank you. Good luck and all the very best with everything you are doing.

  • @yaravallirohit8014
    @yaravallirohit8014 2 года назад +1

    I was struggling to understand windows functions and you have explained it with so ease .Its just awesome.Please keep posting such videos.Thanks a lot :)

  • @sami3592
    @sami3592 3 года назад

    very very good and clear teaching.

  • @BRelding92
    @BRelding92 8 лет назад +7

    Can't believe this doesn't have more views!

  • @rohity294
    @rohity294 4 года назад +2

    Freely available at this quality is incredible...god bless!

  • @justinli19901027
    @justinli19901027 8 лет назад +5

    this guy is so good at teaching.

  • @hscsoft_tutorials
    @hscsoft_tutorials 8 месяцев назад

    You are awssssssom, spent entire day yesterday. think will view kudvenkat tomorrow sunday. found solution withing 5 minute. GREAT

  • @pardhasaradhik9234
    @pardhasaradhik9234 4 года назад +1

    thanks for all the parts in sql, i have no hope of learning before seeing your videos ,slowly learnt total course because of u , god may bless you with happy family

  • @meghasyam427
    @meghasyam427 3 года назад

    Good attention to detail
    Thanks a lot

  • @desisto007
    @desisto007 Год назад +2

    Your tutorials never disappoint! Thanks for sharing your knowledge!

  • @MrBhardwaj03
    @MrBhardwaj03 Год назад

    This video is exceptional, searched whole of the youtube and internet but no one explained so easily and clearly

  • @divyas5635
    @divyas5635 3 года назад

    Great sir This video helps me lot thank u so much 🙏🙏

  • @Tarikffm79
    @Tarikffm79 8 лет назад +9

    Best Tutorial in RUclips! I Love your detailed Videos! I hope you will start with Analysis (SSAS) one day!
    Thank you Venkat!

  • @shivaprasadmpshivu1882
    @shivaprasadmpshivu1882 2 года назад

    really a very detailed explanation.

  • @ladhkay
    @ladhkay 4 года назад +1

    Hats off bro...easy to follow and well explained!

  • @margimehta9609
    @margimehta9609 3 года назад +1

    One of the best explanation of the concept of Windows function! I have referenced to lots of tutorials online, but this one really stands out to me :) Kudos gentleman!

  • @akashdahiya9000
    @akashdahiya9000 2 года назад

    The best video on Window functions so far! highly recommend!!

  • @shahdanyal7542
    @shahdanyal7542 4 года назад

    Words cannot translate the emotions . Only can say excellent presentation in clearing the concepts. Thank you from core of heart.

  • @likithabh3944
    @likithabh3944 3 года назад

    you explained it so well, thankyooou

  • @cblucohigen8281
    @cblucohigen8281 5 лет назад +2

    Wow this is my first comment I’ve ever left on RUclips, you are an amazing teacher I wish I could be mentored by you. From South Africa

  • @Fawad06
    @Fawad06 Год назад

    this person is teaching better than paid courses...

  • @b2theb
    @b2theb 7 лет назад

    Total hero.....I have 70-461 coming up and you've helped me no end!!!!!

  • @MasterBetham
    @MasterBetham 7 лет назад +1

    You are a gem! Explained and demonstrated so well. Thank you very much good Sir!!

  • @shashireddy231
    @shashireddy231 4 года назад

    Venkat are too good. Unbelievably clear and simple.

  •  4 года назад +1

    This is a really comprehensive tutorial! Thank you so much!

  • @samuelatobatele5894
    @samuelatobatele5894 3 года назад

    The best of the very best! Thank you very much. Greetings from Nigeria

  • @jayashreejay2989
    @jayashreejay2989 3 года назад

    Worth watching

  • @onyu5813
    @onyu5813 Год назад

    Thanks! very helpful content

  • @preyaaatri2943
    @preyaaatri2943 4 года назад

    Best video for understanding window functions.

  • @hanansaleena
    @hanansaleena 3 года назад

    Awesome explanation

  • @hotechmanager
    @hotechmanager 4 года назад

    You are like a GOD for me....

  • @balrajvirdee1087
    @balrajvirdee1087 Год назад

    Great explanation 👍🏽

  • @balrajvirdee1087
    @balrajvirdee1087 Год назад

    Thanks great video 👍🏽

  • @pascalhauschildt856
    @pascalhauschildt856 3 года назад

    GREAT JOB!!!

  • @operationbroomstick
    @operationbroomstick Год назад

    Wow. Thanks brother ❤

  • @ibrahims6163
    @ibrahims6163 5 лет назад

    EXELENT !!!!! it is the best for 10 minutes !!! Perfect

  • @93gbz
    @93gbz 5 лет назад +1

    OMG Thanks for this absolutely amazing video

  • @shubhamsaxena9789
    @shubhamsaxena9789 7 лет назад

    WOW.....you are too good in explaining the things..cheers...

  • @TheThodi
    @TheThodi 7 лет назад +4

    keep it up! extremely well made tutorial! :)

  • @HygienicMaggot
    @HygienicMaggot 8 лет назад

    I'm reading Itzik Ben-Gan Fundamentals and joes2pro books but your videos really do give the best clarification on SQL topics. Subscribed and be checking the rest of your channel

  • @NoobLahe
    @NoobLahe 6 лет назад

    video was straight forward and detailed, thank you for sharing your knowledge

  • @imransaieen
    @imransaieen 8 лет назад

    Simplicity of your explanation is remarkable. Keep it up.

  • @ivywang3276
    @ivywang3276 3 года назад

    super helpful

  • @kakakukukakakuku
    @kakakukukakakuku 7 лет назад

    Very helpful video, clearly and nicely explained . Thanks !

  • @prvnkumar7
    @prvnkumar7 Год назад

    Thank you Venkat Sir.

  • @urspramila
    @urspramila 8 лет назад

    very well explained with simplicity. Great Work.

  • @NightKingMarhoom
    @NightKingMarhoom 3 года назад

    Sir u r great

  • @vaishalib4938
    @vaishalib4938 4 года назад

    very neat and to the point explanation !

  • @anmolpreetkaur711
    @anmolpreetkaur711 3 года назад

    What an explanation! Thank you!

  • @malvikachandhok8571
    @malvikachandhok8571 3 года назад

    Thank you so much! Best tutorial!

  • @Passrord
    @Passrord 5 лет назад

    you made it so clear and precise...thank you

  • @sundeepyadav6234
    @sundeepyadav6234 9 лет назад +1

    Very detailed video.... Thank you :)

  • @TheDhanoopjose
    @TheDhanoopjose 8 лет назад +2

    Thanks for the video. It was really helpful

    • @Csharp-video-tutorialsBlogspot
      @Csharp-video-tutorialsBlogspot  8 лет назад +1

      +Dhanup Thank you for the feedback. This means a lot. Very pleased you found these videos useful.
      Dot Net & SQL Server training videos to help you become a web developer
      ruclips.net/user/kudvenkatplaylists?view=1&sort=dd
      Download videos for offline viewing
      www.pragimtech.com/Order.aspx
      Slides, Code Samples & Text Version of the videos can be found on my blog
      csharp-video-tutorials.blogspot.com
      Tips to effectively use our youtube channel
      ruclips.net/video/y780MwhY70s/видео.html
      To receive alerts, when new videos are uploaded, please subscribe to our channel
      ruclips.net/user/kudvenkat
      If you like these videos, please click on the THUMBS UP button below the video
      May I request you to please share the link with your friends who you think would also benefit from them
      To teach is to learn
      Thanks
      Venkat

  • @aayushisaxena5738
    @aayushisaxena5738 3 года назад

    You are awesome!

  • @deepikakbify
    @deepikakbify 8 лет назад

    Thanks a lot for the videos. They are very well explained.

  • @doreenshen3690
    @doreenshen3690 3 года назад

    This is super helpful. Thanks!

  • @khaledrabah3001
    @khaledrabah3001 3 года назад

    5 star teacher

  • @parvathirajan.n
    @parvathirajan.n 3 года назад

    Amazing ! No words !!!

  • @antoniosol9863
    @antoniosol9863 9 лет назад +1

    El Maestro returns

  • @80amnesia
    @80amnesia 3 года назад

    Thank you Venkat

  • @HorrorTube1313
    @HorrorTube1313 4 года назад

    Can't thank you enough for these videos ! :)

  • @Sonny0276
    @Sonny0276 7 лет назад

    Great Video. Thank you for posting.

  • @TheHimaBindu
    @TheHimaBindu 8 лет назад

    Very informative

  • @mariyademirova5571
    @mariyademirova5571 8 лет назад

    Great video! Thank you, it is much clearer now!

  • @harveybains146
    @harveybains146 6 лет назад

    Great video, Many thanks

  • @santhoshgayakvad1013
    @santhoshgayakvad1013 6 лет назад

    To the point, very nice explanations . Thank you :)

  • @jamesabsalemlintu9517
    @jamesabsalemlintu9517 6 лет назад

    Thank you so much and appreciate your work.

  • @anekarvishal8477
    @anekarvishal8477 6 лет назад

    Very good explanation

  • @romiltube
    @romiltube 7 лет назад +1

    Hi Venkat, first off I enjoy your videos a lot, very helpful for interviews.
    I have a question related to analytic window function (sum)
    Could you please confirm which version of MS SQL Server are you using? Not 2008 right?
    Because, I tried running below query in 2008 version which is quite similar to what you demonstrated in of your slides in above video and it threw syntax error.
    select *, sum(sales) over(order by sales) as sumofsales from test1......error
    select *, sum(sales) over() as sumofsales from test1.......no error it ran successfully
    select *, sum(sales) over(partition by dealer) as sumofsales from test1.......no error it ran successfully
    The reason why the first query failed makes sense to me, because I believe there is no need at all for a order by argument in analytic window function; partition by argument might be required sometimes though (my 3rd query)
    I got confused because your query (quite similar to mine, as per above logic it should have failed since it contained order by arguement in analytical window function you demonstrated) ran successfully...
    Seems like you are using a different SQL version than mine...Just curious to know why your query did not fail :)
    Thanks again for comprehensive, well articulated and ofcourse free videos :)
    Regards,
    Romil

  • @smtzac002
    @smtzac002 7 лет назад

    Thank you. That was extremely clear

  • @dengzhonghan5125
    @dengzhonghan5125 3 года назад

    Good tutorial. Partition by Gender order by Salary. Why salary is not in ascending order?

  • @5tarda
    @5tarda 8 лет назад +1

    Thanks man, apreciate your work you've done here :) helpfull, keep doing good work :)

  • @haziqhumayun5878
    @haziqhumayun5878 6 лет назад

    Too good, thank you so much

  • @anshumanbupathi
    @anshumanbupathi Год назад

    venkat sir could u explain realtime queries in fucntions

  • @f1r3shoot
    @f1r3shoot 8 лет назад

    Отличное видео

  • @mohammadal-aqrabawi7145
    @mohammadal-aqrabawi7145 8 лет назад

    Excellent!!!

  • @soniasadeque4248
    @soniasadeque4248 7 лет назад

    great..

  • @cbdbee707
    @cbdbee707 5 лет назад

    Is there anyway to set the preceeding/following row clause programatically so that i can write one function that can output moving average of variable window width? (without using dynamic queries)

  • @raqibulAlam-su3jo
    @raqibulAlam-su3jo 9 лет назад +1

    Thanks

  • @edwardbrown7469
    @edwardbrown7469 6 лет назад

    Running average by accident, I love it!

    • @ssmahajan
      @ssmahajan 5 лет назад

      You are my Guru 🙏

  • @ishanvallabhkamat4254
    @ishanvallabhkamat4254 5 лет назад

    Why can't we directly use partition by instead of specifying Unbounded values? In video 108 this is achieved by Partitioning it right?

  • @muhammadrehbarsheikh8498
    @muhammadrehbarsheikh8498 9 лет назад

    Thanks sir!!

  • @UrBoyiVignesh
    @UrBoyiVignesh 4 года назад

    i think we have to use ID column for Order By Clause to avoid duplicates.....

  • @orxanbabashov
    @orxanbabashov 7 лет назад

    thanks so much

  • @monsterMonks
    @monsterMonks 3 года назад +1

    Your voice sounds like Sadhguru

  • @questscape
    @questscape 3 года назад

    If we just use OVER() without specifying anything inside over then it is an equivalent to mentioning UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING.

  • @amandeeppanwar9006
    @amandeeppanwar9006 3 года назад

    SQL Knowledge Hub on Internet _/\_

  • @vishallande2300
    @vishallande2300 5 лет назад

    For >> PARTITION BY GENDER ORDER BY SALARY >>>>>> order of salary is correct for FEMALE but why order is incorrect for MALE resultset