Lead and Lag functions in SQL Server 2012

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 ก.ย. 2024
  • In this video we will discuss about Lead and Lag functions.
    Lead and Lag functions
    Introduced in SQL Server 2012
    Lead function is used to access subsequent row data along with current row data
    Lag function is used to access previous row data along with current row data
    ORDER BY clause is required
    PARTITION BY clause is optional
    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 TH-cam channel. Hope you can help.
    / @aarvikitchen5572
    Syntax
    LEAD(Column_Name, Offset, Default_Value) OVER (ORDER BY Col1, Col2, ...)
    LAG(Column_Name, Offset, Default_Value) OVER (ORDER BY Col1, Col2, ...)
    Offset - Number of rows to lead or lag.
    Default_Value - The default value to return if the number of rows to lead or lag goes beyond first row or last row in a table or partition. If default value is not specified NULL is returned.
    We will use the following Employees table for the examples in this video
    SQL Script to create the 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', 1000)
    Insert Into Employees Values (2, 'John', 'Male', 2000)
    Insert Into Employees Values (3, 'Pam', 'Female', 3000)
    Insert Into Employees Values (4, 'Sara', 'Female', 4000)
    Insert Into Employees Values (5, 'Todd', 'Male', 5000)
    Insert Into Employees Values (6, 'Mary', 'Female', 6000)
    Insert Into Employees Values (7, 'Ben', 'Male', 7000)
    Insert Into Employees Values (8, 'Jodi', 'Female', 8000)
    Insert Into Employees Values (9, 'Tom', 'Male', 9000)
    Insert Into Employees Values (10, 'Ron', 'Male', 9500)
    Go
    Lead and Lag functions example WITHOUT partitions : This example Leads 2 rows and Lags 1 row from the current row.
    When you are on the first row, LEAD(Salary, 2, -1) allows you to move forward 2 rows and retrieve the salary from the 3rd row.
    When you are on the first row, LAG(Salary, 1, -1) allows us to move backward 1 row. Since there no rows beyond row 1, Lag function in this case returns the default value -1.
    When you are on the last row, LEAD(Salary, 2, -1) allows you to move forward 2 rows. Since there no rows beyond the last row 1, Lead function in this case returns the default value -1.
    When you are on the last row, LAG(Salary, 1, -1) allows us to move backward 1 row and retrieve the salary from the previous row.
    SELECT Name, Gender, Salary,
    LEAD(Salary, 2, -1) OVER (ORDER BY Salary) AS Lead_2,
    LAG(Salary, 1, -1) OVER (ORDER BY Salary) AS Lag_1
    FROM Employees
    Lead and Lag functions example WITH partitions : Notice that in this example, Lead and Lag functions return default value if the number of rows to lead or lag goes beyond first row or last row in the partition.
    SELECT Name, Gender, Salary,
    LEAD(Salary, 2, -1) OVER (PARTITION By Gender ORDER BY Salary) AS Lead_2,
    LAG(Salary, 1, -1) OVER (PARTITION By Gender ORDER BY Salary) AS Lag_1
    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

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

  • @elektro9te
    @elektro9te ปีที่แล้ว +3

    I rarely comment on videos but bro, my outmost respect and gratitude to you for this simple but concise explanation.

  • @clintbanzet2958
    @clintbanzet2958 5 ปีที่แล้ว +13

    Absolutely the best explanation I have found on this topic. OVER, ORDER BY, PARTITION BY, always confused me. Thank you for taking the time to explain it so thoroughly. And you did it quickly with no unnecessary waiting or delays. Thank you so much. Also in my mind, Microsoft has the names for LEAD and LAG swapped. After hearing your explanation though, I can at least see where they were coming from.

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

    Venkat rules! Thank you for educating community! You are master of SQL Server. I'm the greatest fan of your tutorial. I watched all your videos. Thank U for all your job.

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

    Very good video. Please don't take it the wrong way but usually people form India makin the videos will mispronounce the words and/or not enunciate them properly or they talk really fast. Your pronunciation, speed and enunciations of words was well done. Thanks

  • @nikhilmekala3446
    @nikhilmekala3446 6 ปีที่แล้ว

    Kudvenkat explain these concepts in a simple way? Absolutely!

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

    simple and brilliant explanation! thanks

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

    old but gold, this is the best explanation for me. thank you so much brother!

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

    Best explanation and very simple

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

    Simple explaning that tells everything! thanks again !!!

  • @user-rj2wc8uo6t
    @user-rj2wc8uo6t 6 หลายเดือนก่อน

    Very much of clear explanation

  • @karthikyammanur7881
    @karthikyammanur7881 3 ปีที่แล้ว +3

    Hi Venkat, As always you are the best. My career did build on your videos. I was asked in a interview on how to build the same as lead and lag using self joins and sub queries without using these window functions. Could you please provide your insight on this? Thanks much for everything you have provided to the techies across the world.

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

    Thank you for your explanation! It was very easy to understand through your examples! :)

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

    Appreciate your great efforts and awesome teaching skills 👍🏻

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

    Excellent video

  • @VIJAYKUMAR-qr8st
    @VIJAYKUMAR-qr8st ปีที่แล้ว

    Good explanation 👍

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

    Thank you so much for this brilliant demonstration!

  • @timstrack4573
    @timstrack4573 4 ปีที่แล้ว

    Excellent!! You explain the function with simple explanation.

    • @Csharp-video-tutorialsBlogspot
      @Csharp-video-tutorialsBlogspot  4 ปีที่แล้ว

      Hello Tim - 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.

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

    Great explanation!

  • @TheStarryboy
    @TheStarryboy 5 ปีที่แล้ว

    Thank you Venkat Sir. You are the best.

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

    Crisp and to the point

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

    Thanks for your efforts sir. Keep doing more vedios on SQL 🙏

  • @shaikhs6299
    @shaikhs6299 5 ปีที่แล้ว

    Thanks Venkat
    I dont say about that
    U r superb talented person in orcale

  • @anusuyashanmugam957
    @anusuyashanmugam957 3 ปีที่แล้ว

    Very nice video

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

    Superb explanation of these functions, thank you.

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

    It was clear explanation, thank you sir!

  • @ShubhamRai06
    @ShubhamRai06 3 ปีที่แล้ว

    Great

  • @devarakondasankararao3802
    @devarakondasankararao3802 5 ปีที่แล้ว

    Easy understanding thank you Venkat sir

  • @DebashishP82
    @DebashishP82 3 ปีที่แล้ว

    excellent explanation. many thanks for the video

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

    Thanks a lot for explanation!

  • @sohanyennu3750
    @sohanyennu3750 7 ปีที่แล้ว

    Thanks a ton...This function eases the number code lines...

  • @swevirgo
    @swevirgo 5 ปีที่แล้ว

    Thanks fr the simple and easy explanation

  • @jackchatten3777
    @jackchatten3777 6 ปีที่แล้ว

    Great simple explanation. Very helpful

  • @couragemath9333
    @couragemath9333 6 ปีที่แล้ว

    Excellent video. Thank you sir

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

    Thank you!

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

    This is SUCH a great video! thanks for the explanation!!!

  • @anjanummadi4808
    @anjanummadi4808 7 ปีที่แล้ว

    Hi Venkat,
    We are really gratefull to you. Thanks for your time to provide all these vedios for the learners. could you please let us know if you have any vedios for control statements like FOR,WHILE, DO WHILE and IF ELSE Statements with examples.
    Eagerly waiting for your response.
    Can you also let me know if you have any clssess to attend by paying money to learn PLSQL?

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

    Thanks venkat sir!

  • @ashokstephen2175
    @ashokstephen2175 7 ปีที่แล้ว

    Hai venkat sir...thank you so much sir.
    v r getting good knowledge from ur videos.
    can u pls share realtime videos on ETL,BI testing.

  • @dollarmenu9618
    @dollarmenu9618 6 ปีที่แล้ว

    great video thank you - one of the best that i found

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

    Thank you so much for the video :)

  • @mohsenhs
    @mohsenhs 6 ปีที่แล้ว

    As always, another amazing tutorial, thanks

  • @alonben-david4929
    @alonben-david4929 6 ปีที่แล้ว

    thanks It was very helpful

  • @rajmothe3716
    @rajmothe3716 7 ปีที่แล้ว

    Thank you Venkat Sir !

  • @bernsbats5995
    @bernsbats5995 7 ปีที่แล้ว

    WOW! Awesome functions to use!

  • @emchammer1815
    @emchammer1815 3 ปีที่แล้ว

    now it makes sense. thanks buddy

  • @alechenry9994
    @alechenry9994 7 ปีที่แล้ว

    thank you for the video - extremely clear

  • @JorgeGonzalez-pq7wh
    @JorgeGonzalez-pq7wh 4 ปีที่แล้ว

    Great video, thanks!

  • @raqibul1000
    @raqibul1000 9 ปีที่แล้ว

    Thanks a Billion.Thank you so much.

  • @shortsmore278
    @shortsmore278 4 ปีที่แล้ว

    Fantastic !!!!

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

    thanks once again sir

    • @Csharp-video-tutorialsBlogspot
      @Csharp-video-tutorialsBlogspot  9 ปีที่แล้ว +1

      +govindraj kannan Thanks a million for taking time to give feedback. I am glad you found the videos useful.
      I am Venkat (Software Architect) with 13 years of experience building highly transactional software applications
      Free Dot Net & SQL Server videos for web developers
      th-cam.com/users/kudvenkatplaylists?view=1&sort=dd
      If you need DVDs for offline viewing, you can order them using the link below
      www.pragimtech.com/Order.aspx
      Code Samples, Text Version of the videos & PPTS on my blog
      csharp-video-tutorials.blogspot.com
      Tips to effectively use our channel
      th-cam.com/video/y780MwhY70s/w-d-xo.html
      Want to receive email alerts, when new videos are uploaded, please subscribe to our channel using the link below
      th-cam.com/users/kudvenkat
      Please click the THUMBS UP button below the video, if you think you liked them
      Thank you for sharing these links with your friends
      Best
      Venkat

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

      If you provide some real time use of this kind of function that will be very helpful sir. if you speak the scenario that would be sufficient sir. thanks a lot for all this videos :)

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

    thanks

  • @sayi77
    @sayi77 8 ปีที่แล้ว

    perfect as ur other lessons.

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

    cool job :)

  • @TheEsisia
    @TheEsisia 8 ปีที่แล้ว

    You are awesome!

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

    In lag function if we don't give any parameters the default lag step value 1 is right?
    If we give Like lag(id) it will down 1 step below for all the rows

  • @chengareddy4269
    @chengareddy4269 6 ปีที่แล้ว

    plz make one video for
    provide performance tuning in SQL server

  • @jeanhyppolite1101
    @jeanhyppolite1101 6 ปีที่แล้ว

    you the man

  • @prafullakumarseelam6500
    @prafullakumarseelam6500 8 ปีที่แล้ว

    How can use the lag function in CASE -WHEN statement ? I need to perform an increment by 1 of the previous value if my condition is true else it should retain the same value. I am using lag in case-when statement but not getting the desired output. Please help.

  • @adityaraj-jr8ph
    @adityaraj-jr8ph 4 ปีที่แล้ว

    yo bro thanks

    • @norpriest521
      @norpriest521 4 ปีที่แล้ว

      I don't find these two functions useful

  • @justinli19901027
    @justinli19901027 8 ปีที่แล้ว

    this man is a god!

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

    Hello
    I'm getting this error when I'm trying to use same thing on MYSQL DATAGRIP:
    [42000][1064] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(ORDER BY Salary) AS LEAD
    What can I do here to solve this problem? Any help will be appreciated

  • @c-sharptricks9899
    @c-sharptricks9899 3 ปีที่แล้ว

    how to use where clause at end?

  • @vidharthkate1897
    @vidharthkate1897 4 ปีที่แล้ว

    Why did Lead doesn't throw error even when it is a windows function?

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

    Could someone please help me know the pronunciation of this gentleman's name?

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

    What is real use of this ?

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

    who is watching this in 2022

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

    Venkat rules! Thank you for educating community! You are master of SQL Server. I'm the greatest fan of your tutorial. I watched all your videos. Thank U for all your job.

  • @anjanummadi4808
    @anjanummadi4808 7 ปีที่แล้ว

    Hi Venkat,
    We are really gratefull to you. Thanks for your time to provide all these vedios for the learners. could you please let us know if you have any vedios for control statements like FOR,WHILE, DO WHILE and IF ELSE Statements with examples.
    Eagerly waiting for your response.
    Can you also let me know if you have any clssess to attend by paying money to learn PLSQL?