Last Chance to Join Data Science Interview MasterClass (this week) 🚀 | Just 2 spots remaining...

PRO SQL Modules

SQL Basics
1h 20m • 4 lessons
  1. SQL SELECT
  2. LIMIT
  3. ORDER BY
  4. DISTINCT
Data Filtering
1h 20m • 4 lessons
  1. WHERE
  2. LIKE
  3. IN
  4. BETWEEN
Aggregations
40m • 2 lessons
  1. GROUP BY
  2. HAVING
Multiple Tables
1h 40m • 5 lessons
  1. INNER JOIN
  2. LEFT & RIGHT JOIN
  3. FULL OUTER JOIN
  4. SELF JOIN
  5. INTERSECT & EXCEPT
Query Restructuring
40m • 2 lessons
  1. Subquery
  2. Common Table Expressions (CTE)
Data Transformation
1h 40m • 5 lessons
  1. CASE
  2. String
  3. Date Manipulation
  4. Math Operations
  5. COALESCE
Analytical SQL
2h • 6 lessons
  1. Ranking - ROW_NUMBER(), RANK(), DENSE_RANK()
  2. Running Totals
  3. LAG and LEAD
  4. FIRST_VALUE, LAST_VALUE, Nth_VALUE
  5. PARTITION BY
  6. SQL BETWEEN and PRECEDING
Unlock Premium
FREE

LAG and LEAD

Course Author Daniel Lee
Instructor

Dan Lee

Learn SQL for free with interactive exercises designed to help aspiring Data Engineers, Data Analysts, and Data Scientists. Learn the basics and advanced SQL concepts by solving real-world problems from MANGO (Meta, Amazon, Netflix, Google, OpenAI)

Lesson Objectives

By the end of this lesson, you will:

  • Learn how to use LAG() to retrieve the previous row’s value
  • Use LEAD() to retrieve the next row’s value
  • Write SQL queries to analyze user engagement trends on Meta posts

🎬 Scenario: Analyzing Engagement Trends Over Time

Your manager at Meta, Jordan, wants insights into how engagement changes between consecutive posts.

Can you compare the likes of each post with the previous and next post?

Your task is to use LAG() and LEAD() to analyze engagement trends over time. Use the meta_posts table in this lesson:

post_iduser_idpost_contentpost_datelikessharespost_status
11“Had a great day at the beach!”“2024-01-10T14:30:00”12045“Public”
22“Launching our new product!”“2024-01-12T09:20:00”25090“Friends Only”
33“Just finished a 10K run!”“2024-01-15T18:10:00”8030“Public”
44“Hosting a webinar next week.”“2024-01-18T20:45:00”5025“Private”
55“Traveling to Japan soon!”“2024-01-20T10:05:00”18075“Public”
61“Excited for the weekend!”“2024-01-25T16:15:00”9540“Friends Only”
72“Need recommendations for a new laptop.”“2024-01-27T12:40:00”7020“Public”
83“Learning SQL window functions!”“2024-01-29T08:55:00”11050“Public”
94“Working on a new AI model.”“2024-02-01T14:00:00”200100“Friends Only”
105“Book recommendations?”“2024-02-05T11:30:00”6515“Public”

1. Understanding LAG() and LEAD()

FunctionWhat It DoesExample Use Case
LAG()Returns the value from the previous rowCompare the current post’s likes to the previous post
LEAD()Returns the value from the next rowPredict engagement changes by looking ahead

Syntax for LAG() and LEAD()

1SELECT column1,  
2       LAG(column2) OVER (ORDER BY column3) AS prev_value,  
3       LEAD(column2) OVER (ORDER BY column3) AS next_value  
4FROM table_name;
  • ORDER BY column3 defines the sequence of rows.
  • LAG() retrieves data from the previous row.
  • LEAD() retrieves data from the next row.

2. Using LAG() to Compare Engagement with Previous Posts

Jordan wants to compare likes between consecutive posts.

Example Query: Finding Previous Post Likes

1SELECT post_id, post_date, likes,  
2       LAG(likes) OVER (ORDER BY post_date) AS prev_likes  
3FROM meta_posts;
post_idpost_datelikesprev_likes
1“2024-01-10T14:30:00”120null
2“2024-01-12T09:20:00”250120
3“2024-01-15T18:10:00”80250
4“2024-01-18T20:45:00”5080
5“2024-01-20T10:05:00”18050
6“2024-01-25T16:15:00”95180
7“2024-01-27T12:40:00”7095
8“2024-01-29T08:55:00”11070
9“2024-02-01T14:00:00”200110
10“2024-02-05T11:30:00”65200

What’s Happening?

  • LAG(likes) OVER (ORDER BY post_date) retrieves likes from the previous post.
  • The first row has NULL since no previous post exists.

3. Using LEAD() to Compare Engagement with Next Posts

Jordan now wants to compare each post’s likes to the next post.

Example Query: Finding Next Post Likes

1SELECT post_id, post_date, likes,  
2       LEAD(likes) OVER (ORDER BY post_date) AS next_likes  
3FROM meta_posts;
post_idpost_datelikesnext_likes
1“2024-01-10T14:30:00”120250
2“2024-01-12T09:20:00”25080
3“2024-01-15T18:10:00”8050
4“2024-01-18T20:45:00”50180
5“2024-01-20T10:05:00”18095
6“2024-01-25T16:15:00”9570
7“2024-01-27T12:40:00”70110
8“2024-01-29T08:55:00”110200
9“2024-02-01T14:00:00”20065
10“2024-02-05T11:30:00”65null

What’s Happening?

  • LEAD(likes) OVER (ORDER BY post_date) retrieves likes from the next post.
  • The last row has NULL since no next post exists.

✍️ SQL Exercises

Exercise 1: Comparing Likes with Previous Posts

Jordan wants to compare the likes of each post with the previous post.

Filter condition:

  • Retrieve post ID, post date, likes, and previous likes.
  • Sort by post_date in ascending order.
post_idpost_datelikesprev_likes
1“2024-01-10T14:30:00”120null
2“2024-01-12T09:20:00”250120
3“2024-01-15T18:10:00”80250
4“2024-01-18T20:45:00”5080
5“2024-01-20T10:05:00”18050
6“2024-01-25T16:15:00”95180
7“2024-01-27T12:40:00”7095
8“2024-01-29T08:55:00”11070
9“2024-02-01T14:00:00”200110
10“2024-02-05T11:30:00”65200

Write an SQL query to return the requested data.

Upgrade to Pro

Exercise 2: Comparing Likes with Next Posts

Jordan also wants to compare the likes of each post with the next post.

Filter condition:

  • Retrieve post ID, post date, likes, and next likes.
  • Sort by post_date in ascending order.
post_idpost_datelikesnext_likes
1“2024-01-10T14:30:00”120250
2“2024-01-12T09:20:00”25080
3“2024-01-15T18:10:00”8050
4“2024-01-18T20:45:00”50180
5“2024-01-20T10:05:00”18095
6“2024-01-25T16:15:00”9570
7“2024-01-27T12:40:00”70110
8“2024-01-29T08:55:00”110200
9“2024-02-01T14:00:00”20065
10“2024-02-05T11:30:00”65null

Write an SQL query to return the requested data.

Upgrade to Pro
👉 Found this lesson helpful?