Over clause in SQL Server

Поділитися
Вставка
  • Опубліковано 29 вер 2024
  • over partition by in sql server 2008
    sql server over clause partition
    partition by clause in sql server 2008
    over partition by clause in sql
    In this video we will discuss the power and use of Over clause 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 UA-cam channel. Hope you can help.
    / @aarvikitchen5572
    The OVER clause combined with PARTITION BY is used to break up data into partitions.
    Syntax : function (...) OVER (PARTITION BY col1, Col2, ...)
    The specified function operates for each partition.
    For example :
    COUNT(Gender) OVER (PARTITION BY Gender) will partition the data by GENDER i.e there will 2 partitions (Male and Female) and then the COUNT() function is applied over each partition.
    Any of the following functions can be used. Please note this is not the complete list.
    COUNT(), AVG(), SUM(), MIN(), MAX(), ROW_NUMBER(), RANK(), DENSE_RANK() etc.
    Example :
    SQl Script to create Employees table
    Create Table Employees
    (
    Id int primary key,
    Name nvarchar(50),
    Gender nvarchar(10),
    Salary int
    )
    Go
    Insert Into Employees Values (1, 'Mark', 'Male', 5000)
    Insert Into Employees Values (2, 'John', 'Male', 4500)
    Insert Into Employees Values (3, 'Pam', 'Female', 5500)
    Insert Into Employees Values (4, 'Sara', 'Female', 4000)
    Insert Into Employees Values (5, 'Todd', 'Male', 3500)
    Insert Into Employees Values (6, 'Mary', 'Female', 5000)
    Insert Into Employees Values (7, 'Ben', 'Male', 6500)
    Insert Into Employees Values (8, 'Jodi', 'Female', 7000)
    Insert Into Employees Values (9, 'Tom', 'Male', 5500)
    Insert Into Employees Values (10, 'Ron', 'Male', 5000)
    Go
    Write a query to retrieve total count of employees by Gender. Also in the result we want Average, Minimum and Maximum salary by Gender.
    This can be very easily achieved using a simple GROUP BY query as show below.
    SELECT Gender, COUNT(*) AS GenderTotal, AVG(Salary) AS AvgSal,
    MIN(Salary) AS MinSal, MAX(Salary) AS MaxSal
    FROM Employees
    GROUP BY Gender
    What if we want non-aggregated values (like employee Name and Salary) in result set along with aggregated values
    You cannot include non-aggregated columns in the GROUP BY query.
    SELECT Name, Salary, Gender, COUNT(*) AS GenderTotal, AVG(Salary) AS AvgSal,
    MIN(Salary) AS MinSal, MAX(Salary) AS MaxSal
    FROM Employees
    GROUP BY Gender
    The above query will result in the following error : Column 'Employees.Name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause
    One way to achieve this is by including the aggregations in a subquery and then JOINING it with the main query as shown in the example below. Look at the amount of T-SQL code we have to write.
    SELECT Name, Salary, Employees.Gender, Genders.GenderTotals,
    Genders.AvgSal, Genders.MinSal, Genders.MaxSal
    FROM Employees
    INNER JOIN
    (SELECT Gender, COUNT(*) AS GenderTotals,
    AVG(Salary) AS AvgSal,
    MIN(Salary) AS MinSal, MAX(Salary) AS MaxSal
    FROM Employees
    GROUP BY Gender) AS Genders
    ON Genders.Gender = Employees.Gender
    Better way of doing this is by using the OVER clause combined with PARTITION BY
    SELECT Name, Salary, Gender,
    COUNT(Gender) OVER(PARTITION BY Gender) AS GenderTotals,
    AVG(Salary) OVER(PARTITION BY Gender) AS AvgSal,
    MIN(Salary) OVER(PARTITION BY Gender) AS MinSal,
    MAX(Salary) OVER(PARTITION BY Gender) AS MaxSal
    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

КОМЕНТАРІ • 110

  • @alekseyr7394
    @alekseyr7394 8 років тому +37

    Amazing video, thanks a lot!

    • @Csharp-video-tutorialsBlogspot
      @Csharp-video-tutorialsBlogspot  8 років тому +6

      +Алексей Руденко Thank you very much for taking time to give feedback. This means a lot. I am very glad you found the videos useful.
      I have organised all the Dot Net & SQL Server videos in to playlists, which could be useful to you
      ua-cam.com/users/kudvenkatplaylists?view=1&sort=dd
      If you need all the videos and slides for offline viewing please visit
      www.pragimtech.com/kudvenkat_dvd.aspx
      Slides and Text Version of the videos can be found on my blog
      csharp-video-tutorials.blogspot.com
      Tips to effectively use my youtube channel.
      ua-cam.com/video/y780MwhY70s/v-deo.html
      If you want to receive email alerts, when new videos are uploaded, please subscribe to my youtube channel.
      ua-cam.com/users/kudvenkat
      If you like these videos, please click on the THUMBS UP button below the video.
      May I ask you for a favor. I want these tutorials to be helpful for as many people as possible. Please share the link with your friends and family who you think would also benefit from them.
      Good Luck
      Venkat

  • @krzysztofs8535
    @krzysztofs8535 7 років тому +1

    OVER Partintion by RULES. And Venkat Rules ! Tahnk U for educating community. I watched all your videos and i'm impressed. Thank U . Yoy are the best teacher in the world and youtube !

  • @onemanenclave
    @onemanenclave 5 років тому +3

    This is the only correct way of explaining OVER: by relating it with GROUP BY.

  • @MatthewPimenta
    @MatthewPimenta 3 роки тому

    Thank you for this video! Clean, clear, and I really appreciate the comparisons to other methods.

  • @prasadrsby5274
    @prasadrsby5274 8 років тому +1

    Dear sir it s very nice and i am watching ur videos up to 68 part i learning every day new concepts

  • @nasarazam
    @nasarazam 9 років тому +31

    Among the best Teachers I have found on UA-cam. He has that skills of shredding complex things and explaining it in a simple manner. Kudos !

  • @leonidasparigoris1389
    @leonidasparigoris1389 3 роки тому

    You're a great teacher. Thanks

  • @slavaseagull7349
    @slavaseagull7349 8 років тому

    Thank you! It is a very helpfull video tutorial. Now the Over clause is realy clear for me.

  • @RaviShankar-jm1qw
    @RaviShankar-jm1qw 8 років тому

    Awesome video. So nicely explained!!!!

  • @muhammadrehbarsheikh8498
    @muhammadrehbarsheikh8498 9 років тому +1

    Thank you sir!!

  • @kamalpatwa123
    @kamalpatwa123 7 років тому

    Thank You

  • @ashishrao5635
    @ashishrao5635 9 років тому +1

    Hi, Could you please also explain how to us Red Gate s/w for transferring data from one server to another

  • @temprmj1
    @temprmj1 6 років тому

    Forgive my ignorance but wouldn't the query in the JOIN clause be a Derived table and not a sub query?

  • @swathibandarupalli803
    @swathibandarupalli803 2 роки тому +1

    Hello Venkat,
    In this example, in over clause, we are not specifying rows between clause. How its summing up all the rows values. Ideally it should take unbounded preceeding and current row as default.
    Can you please explain

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

      The default window frame without the ORDER BY is the whole partition. But when you use the ORDER BY, the default window frame is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW.

  • @timhxf1
    @timhxf1 7 років тому +1

    why this is no ROWS clause in OVER() but the AVG() calculates for all rows in the partition?

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

    SELECT COUNT(GENDER) AS GENDER_TOTAL,GENDER,NAME,SALARY,AVG(SALARY) AS AVG_SAL,
    MIN(SALARY) AS MIN_SAL,
    MAX(SALARY) AS MAX_SAL
    FROM EMPLOYEES
    GROUP BY GENDER; -- thIs works in MYSQL dont know how

  • @raqibul1000
    @raqibul1000 9 років тому +3

    The best Teachers and also the best Programmer who have too much clear concept I have found on UA-cam

  • @funkychimp
    @funkychimp 6 років тому +2

    Hi Venkat. Thank you for your tutorials. Can I ask. I thought the OVER clause had to be used with the ORDER BY clause. I'm just wondering here why the default 'RANGE BY UNBOUNDED PRECEEDING AND UNBOUNDED CURRENT ROW' does not apply here. Is it because you have used the PARTITION BY clause without the ORDER BY clause? Thanks funkychimp

    • @shubamsalaria971
      @shubamsalaria971 11 місяців тому

      same question....does anyone has it's answer

  • @santhoshb4545
    @santhoshb4545 3 роки тому

    At 6:22 in over clause used table, how do you get Name column Tom Ron Ben Todd Mark John in this sorting. Can someone xplain please, Thanks in advance

  • @shubamsalaria971
    @shubamsalaria971 11 місяців тому

    why didn't you use "unbounded preceding and unbounded following" here as u used in other video?.....could u please explain it

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

    The number of views for videos such as these makes me worry for our future. This video should have 2M views by now.... oh wait, let's go watch baby shark one more time

  • @umut2243
    @umut2243 4 роки тому +1

    I really understood. Before, I always stucked with this problem: whenever I want to retrieve columns like names, salary etc. as grouping with another column that error comes up. However, if we use subquery we'll able to fix that. But subquery may be fuzzy so that we use over. It's brillant, I'm really happy to understand this.

  • @carmenliu3765
    @carmenliu3765 7 років тому +1

    Thanks for the video and making it easy to understand!

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

    We are partitioning 4 times in this example. Which query would be an optimal one in terms of performance?

  • @richardlew3667
    @richardlew3667 3 роки тому

    On page 6 of the slide, how come Gender is in the subquery when it doesn't have aggregations?

  • @88spaces
    @88spaces 3 роки тому +1

    What I love about your videos is that you stick to one topic. You don't add material that is not directly related. It makes my life easier. Thank you.

  • @kimbowayasin6940
    @kimbowayasin6940 3 роки тому +1

    You have really simplified my work in SQL queries, thank you sir

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

    thank you for sharing this knowledge...it really helped me today. ✌

  • @edwinmchaffie5091
    @edwinmchaffie5091 5 років тому +1

    Venkat, You should have a lot more subscribers.. Your vids are tops!!

  • @libertymudzingwa1206
    @libertymudzingwa1206 4 роки тому +1

    This is amazing!!! You are very good at explaining complex concepts and make them easy to understand

  • @anweshgandham6776
    @anweshgandham6776 3 роки тому

    Does over function don't take default range as unbounded preceding and current row ??

  • @SanjaySony303
    @SanjaySony303 9 років тому +1

    hi sir .good afternoon
    thank you for providing video tutorial of .net
    sir would you teach, about data View and its uses and properties
    And DataTable

  • @VersinKettorix
    @VersinKettorix 8 років тому +1

    Thank you for taking the time to post this video. Your explanation of this function was the best I have seen when trying to figure out the use of the "Partition.. Over()" function. Very helpful.

  • @TheThodi
    @TheThodi 7 років тому +1

    easily understandable, great job!

  • @Frickenchies
    @Frickenchies 7 років тому +2

    Very well explained, thank you!

  • @ni12907
    @ni12907 3 роки тому

    Very good explanation, superrrrr !!! thanks 💕

  • @mickeysaini
    @mickeysaini 3 роки тому

    Very Well explained
    Alternate Solution using CTE
    select Gender,count(Gender) GenderTotal,avg(Salary) AvgSalary, Min(Salary) as MIN , Max(Salary) as MaxSalary from
    (select Salary,Gender,Count(Gender) over (partition by Gender) as RN from Employees )
    result
    group by Gender

  • @MrCardeso
    @MrCardeso 9 років тому +1

    Very nice! Another useful tool! Thanks.

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

    This actually the way I would like to teach. You show how complicated a solution is and you show and explain a simpler solution, thanks kudos excilent!

  • @ateeqhalikhan4012
    @ateeqhalikhan4012 2 місяці тому

    Superb explanation thanks alot

  • @ALLCAPS
    @ALLCAPS 3 роки тому

    what a powerful tool! -over.

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

    Kudos very well explained sir good example taken. Please bring a SQL interview series. Nowadays it well appreciated by audience.

  • @brindhaganesan3580
    @brindhaganesan3580 6 років тому +1

    This is an excellent channel. I am so glad I found it!

  • @izulien
    @izulien 3 роки тому

    Super neat concept! Is there a large performance difference between sub query vs over method?

  • @petetalbert
    @petetalbert 7 років тому +1

    Such clear explanation, thank you!

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

    This was an amazing and easy to understand explanation of over and partition by in relation to aggregate functions!!! Thank you!!!

  • @anthonyfranz8317
    @anthonyfranz8317 4 роки тому

    This was an excellent example for the OVER and PARTITION BY clauses. I learned a lot by watching this, again, excellent!!!

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

    Thank you very very much!!!🙏🙏

  • @vikasmatrupally
    @vikasmatrupally 7 років тому

    Thanks for the amazing n interesting videos. Helps a lot in our daily tasks.
    I am not sure the discussion forum is active. Could you please explain how to extract only the Max. Salaries for Male and Female along with other column fields for that record. Thanks !

  • @allangao4783
    @allangao4783 4 роки тому

    You are a great teacher. Very good examples, very helpful tutorials, and your English is really good so it is easy to follow along to your instruction!

  • @jayjayf9699
    @jayjayf9699 6 років тому

    How come the avg in the over clause is not generating running average, I am confused what generates a running aggregate or just one value per partition by

  • @AliHassanLearnMVC
    @AliHassanLearnMVC 9 років тому +1

    very nice...thank u so much...

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

    Awesome Explanation!

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

    To the point this is good stuff👌👍

  • @dosovi4548bustayes
    @dosovi4548bustayes 9 років тому +1

    thank you merlin :-D

  • @ReinaldoHernandezOchoa
    @ReinaldoHernandezOchoa 3 роки тому

    great video, thanks!

  • @saurabhdsawant
    @saurabhdsawant 5 років тому

    Thanks @kudvenkat for such clean explanation! I was confused about use of over for long time and I struck the gold here

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

    thankyou very much

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

    this is so amazing stuff

  • @ladhkay
    @ladhkay 5 років тому +1

    Nicely explained!

  • @tannubajpai4782
    @tannubajpai4782 3 роки тому

    thnx u for ur services

  • @bajanguy2664
    @bajanguy2664 4 роки тому

    Very well explained! New subscriber inserted to the table.

  • @mahipalkamanchi
    @mahipalkamanchi 9 років тому +1

    AWESOME ....!
    THANK U SIR...!

  • @calvinharris4771
    @calvinharris4771 2 місяці тому

    Thank you 💌

  • @davidvo30
    @davidvo30 7 років тому

    Excellent tutorial video with practical samples, thank you.

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

    thankyou great tut

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

    Well explained 👍

  • @shrikanthsingh8243
    @shrikanthsingh8243 3 роки тому

    simply awesome

  • @YenNguyen-nf9uv
    @YenNguyen-nf9uv 3 роки тому

    Amazing tutorial. Thank you

    • @Csharp-video-tutorialsBlogspot
      @Csharp-video-tutorialsBlogspot  3 роки тому

      Thank you Yen. Means a lot. Your review and feedback really helps. Hope you have a few minutes to write a review. Thank you for your time.
      g.page/r/CdKOVS3cTnYnEAg/review

  • @yurifontoura2189
    @yurifontoura2189 8 років тому

    Indian, Always amazing! What would about us, without them?!

    • @jmjohnson42342
      @jmjohnson42342 8 років тому

      +Yuri Fontoura What would about us, indeed.

    • @malharjajoo7393
      @malharjajoo7393 8 років тому

      +Yuri Fontoura _ at least he is explaining difficult concepts man pardon his english

    • @yurifontoura2189
      @yurifontoura2189 8 років тому +1

      +malhar jajoo I'm not criticizing him, in any way. Quite opposite of that. I just noticed the Indians are in everywhere seeding great materials. As SQLAuthority and JavaBrains channel have done as well.

  • @Freeman1125
    @Freeman1125 6 років тому

    Excellent video,clear logic ! thanks a million man

  • @davidgreenfeld9691
    @davidgreenfeld9691 6 років тому +1

    Great!

  • @raptorslam84
    @raptorslam84 4 роки тому

    Genus on the way you explained this !! Thank you !!

  • @excaliber4ps
    @excaliber4ps 3 роки тому

    👍

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

    thanks

  • @xuanquangnguyen2291
    @xuanquangnguyen2291 4 роки тому

    Thank you, the video is so useful!!!

  • @UnstoppableBird
    @UnstoppableBird 4 роки тому

    This is very clear, thank you so much!

  • @juanpablo-kw6th
    @juanpablo-kw6th 8 років тому

    this mean a lot for me thanks for teaching the world

  • @amanansari5567
    @amanansari5567 6 років тому

    Thank you so much it was great help

  • @idhamhafidz
    @idhamhafidz 6 років тому

    Thank you so much. You are an awesome teacher

  • @choudharymustansar3749
    @choudharymustansar3749 5 років тому

    Easy and comprehensive
    Thanks

  • @marshalljackson1385
    @marshalljackson1385 6 років тому

    Well explained video. Thank you.

  • @mudasir989
    @mudasir989 4 роки тому

    You are ray of light for lot of people Venkat. … God bless you !!!!!

    • @Csharp-video-tutorialsBlogspot
      @Csharp-video-tutorialsBlogspot  4 роки тому

      Hello Mudasir - Thank you very much Greatly honoured. 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/
      If you have 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.

  • @ricardovalle8360
    @ricardovalle8360 6 років тому

    Great Video, pretty clear and didactic.

  • @QuickZ_
    @QuickZ_ 3 роки тому

    Excellent teaching as always!

  • @DaveClarkFive
    @DaveClarkFive 6 років тому

    Great explanation and examples.

  • @luizfabrette7450
    @luizfabrette7450 7 років тому

    Well explained! Thanks

  • @RyanHeaney42
    @RyanHeaney42 6 років тому

    Thanks! This helped!

  • @עדיפרי-כ2ז
    @עדיפרי-כ2ז 7 років тому

    Thanks you! very useful!

  • @asitkumarmohanty2579
    @asitkumarmohanty2579 7 років тому

    superbly done. Kudos.. :)

  • @heathermari83
    @heathermari83 6 років тому

    Thank you very much!!!

  • @303TheAlex
    @303TheAlex 7 років тому

    Thank you for these tutorials, they are really great.

  • @AjayChorat
    @AjayChorat 8 років тому

    Thank you Venkat.. :)

  • @bahadirezici
    @bahadirezici 5 років тому

    Yu da best!!!

  • @fathallahilyes529
    @fathallahilyes529 4 роки тому

    WHAT DO OVER(*) ?

  • @aliazad1118
    @aliazad1118 5 років тому

    As usual perfect

  • @spicytuna08
    @spicytuna08 6 років тому

    you get a 10+.

  • @ivankevamo
    @ivankevamo 8 років тому

    Thanks mate!

  • @codyhayes5926
    @codyhayes5926 5 років тому

    I love you

  • @f1r3shoot
    @f1r3shoot 8 років тому

    My respect