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

FIRST_VALUE, LAST_VALUE, Nth_VALUE

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 FIRST_VALUE() to retrieve the first row’s value in a partition
  • Use LAST_VALUE() to get the last row’s value
  • Apply NTH_VALUE() to fetch a specific row based on position
  • Write SQL queries to analyze Meta post engagement trends

🎬 Scenario: Identifying Key Engagement Posts

Your manager at Meta, Jordan, wants insights into the first, last, and most engaging posts within user activity groups.

Can you provide engagement insights by retrieving the most liked post for each user, as well as their first and last post?

Your task is to use FIRST_VALUE(), LAST_VALUE(), and NTH_VALUE() to analyze user engagement patterns. 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 FIRST_VALUE(), LAST_VALUE(), and NTH_VALUE()

Window functions allow you to retrieve specific row values while keeping the full dataset.

FunctionWhat It DoesExample Use Case
FIRST_VALUE()Returns the first row’s value in a partitionIdentify the first post a user made
LAST_VALUE()Returns the last row’s value in a partitionFind the latest post a user made
NTH_VALUE(n)Retrieves the n-th row’s value in a partitionGet the third most liked post

Syntax for FIRST_VALUE, LAST_VALUE, and NTH_VALUE

1SELECT column1,  
2       function(column2) OVER (PARTITION BY column ORDER BY column) AS result  
3FROM table_name;
  • PARTITION BY groups data into subsets (optional).
  • ORDER BY column defines row sequence.

2. Using FIRST_VALUE() to Find the First Post per User

Jordan wants to identify the first post each user made based on post date.

Example Query: Retrieving First Post Per User

1SELECT user_id, post_id, post_date, likes,  
2       FIRST_VALUE(post_id) OVER (PARTITION BY user_id ORDER BY post_date) AS first_post  
3FROM meta_posts;
user_idpost_idpost_datelikesfirst_post
132024-01-15 10:20:001403
152024-01-25 12:00:001503
222024-01-10 18:30:001002

What’s Happening?

  • FIRST_VALUE(post_id) OVER (PARTITION BY user_id ORDER BY post_date) retrieves the earliest post each user made.
  • The first post remains the same across all of a user’s rows.

3. Using LAST_VALUE() to Find the Most Recent Post

Jordan now wants to identify the last post made by each user.

Example Query: Retrieving Last Post Per User

1SELECT user_id, post_id, post_date, likes,  
2       LAST_VALUE(post_id) OVER (PARTITION BY user_id ORDER BY post_date ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS last_post  
3FROM meta_posts;
user_idpost_idpost_datelikeslast_post
44“2024-01-18T20:45:00”509
49“2024-02-01T14:00:00”2009
11“2024-01-10T14:30:00”1206
16“2024-01-25T16:15:00”956
33“2024-01-15T18:10:00”808
38“2024-01-29T08:55:00”1108
22“2024-01-12T09:20:00”2507
27“2024-01-27T12:40:00”707
55“2024-01-20T10:05:00”18010
510“2024-02-05T11:30:00”6510

What’s Happening?

  • LAST_VALUE(post_id) OVER (...) retrieves the latest post per user.
  • The function requires a frame specification (ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) to ensure it looks at all rows in the partition.

4. Using NTH_VALUE() to Find the Second Most Liked Post

Jordan wants to retrieve the second most liked post for each user.

Example Query: Finding the Second Most Liked Post

1SELECT user_id, post_id, post_date, likes,  
2       NTH_VALUE(post_id, 2) OVER (PARTITION BY user_id ORDER BY likes DESC) AS second_most_liked_post  
3FROM meta_posts;
user_idpost_idpost_datelikessecond_most_liked_post
38“2024-01-29T08:55:00”110null
33“2024-01-15T18:10:00”803
11“2024-01-10T14:30:00”120null
16“2024-01-25T16:15:00”956
49“2024-02-01T14:00:00”200null
44“2024-01-18T20:45:00”504
22“2024-01-12T09:20:00”250null
27“2024-01-27T12:40:00”707
55“2024-01-20T10:05:00”180null
510“2024-02-05T11:30:00”6510

What’s Happening?

  • NTH_VALUE(post_id, 2) OVER (PARTITION BY user_id ORDER BY likes DESC) retrieves the second most liked post per user.
  • If a user has only one post, the result is NULL.

✍️ SQL Exercises

Exercise 1: Finding the First Post Per User

Jordan wants to identify the first post made by each user.

Filter condition:

  • Retrieve user ID, post ID, post date, likes, and first post ID.
  • Sort by user_id and post_date in ascending order.
user_idpost_idpost_datelikesfirst_post
33“2024-01-15T18:10:00”803
38“2024-01-29T08:55:00”1103
44“2024-01-18T20:45:00”504
49“2024-02-01T14:00:00”2004
11“2024-01-10T14:30:00”1201
16“2024-01-25T16:15:00”951
22“2024-01-12T09:20:00”2502
27“2024-01-27T12:40:00”702
55“2024-01-20T10:05:00”1805
510“2024-02-05T11:30:00”655

Write an SQL query to return the requested data.

Upgrade to Pro

Exercise 2: Finding the Second Most Liked Post

Jordan also wants to retrieve the second most liked post per user.

Filter condition:

  • Retrieve user ID, post ID, post date, likes, and second most liked post ID.
  • Sort by user_id and likes in descending order.
user_idpost_idpost_datelikessecond_most_liked_post
38“2024-01-29T08:55:00”110null
33“2024-01-15T18:10:00”803
49“2024-02-01T14:00:00”200null
44“2024-01-18T20:45:00”504
11“2024-01-10T14:30:00”120null
16“2024-01-25T16:15:00”956
22“2024-01-12T09:20:00”250null
27“2024-01-27T12:40:00”707
55“2024-01-20T10:05:00”180null
510“2024-02-05T11:30:00”6510

Write an SQL query to return the requested data.

Upgrade to Pro
👉 Found this lesson helpful?