How to Get the Previous Row in SQL (LAG and LEAD) #sql

preview_player
Показать описание

Hi
**Don't miss the SQL challenge at the end.**
In this tutorial we will see how you can fetch previous row value using LAG Function.
We will also see how you can retrieve previous row value without using LAG Function.

Video timeline:
00:00 Introduction
01:20 Use Lag function to fetch previous row value
02:35 Without Lag function fetch previous row value
04:20 Challenge to fetch next row using LEAD function and without using LEAD function

Queries used in the video :

/* how to get previous row value in sql - using LAG function */

select
catid,
lag(catid) over (order by catid) as previous_catid,
catgroup,
catname
from category;

/* how to get previous row value in sql without using LAG function */

select
catid,
min(catid) over (order by catid rows between 1 preceding and 1 preceding) as previous_catid,
catgroup,
catname
from category;

Link to video where I have explained ROWS BETWEEN clause in dettail:

Practice SQL questions on Data Lemur platform.
I will highly recommend to sign up for this platform.
I am sharing my referral link below for easy reference.
Рекомендации по теме
Комментарии
Автор

Hello Sir, What are the career options for an ETL developer (like data eng, data analyst, cloud)? What all other skills should one develop on the way ? Should I learn aws, tableau ? Kindly please help fellow ETL beginner. Thank you

msdhonifan