AlgoMaster Logo

Exercise: LATERAL JOIN

Last Updated: May 12, 2026

1 min read

For this exercise, we will work with below data:

SQL

Exercise 1

Easy

Use LATERAL to find the most recent stream for each user from the users and streams tables. Show usernametrack_id, and started_at. Include users with no streams (show NULLs). Order by username.

SQL

Expected Query

Expected Output

Exercise 2

Easy

Use LATERAL to find each user's highest-amount subscription from the subscriptions table. Show username and amount. Include users with no subscription (NULL amount). Order by username.

SQL

Expected Query

Expected Output

Exercise 3

Medium

Use LATERAL to find the 3 most recent streams per user with track title and artist name from the usersstreamstracksalbums, and artists tables. Show usernametrack_titleartist_name, and started_at. Only include users who have at least one stream. Order by username, then started_at descending.

SQL

Expected Query

Expected Output

Exercise 4

Medium

Use LATERAL to find each artist's most recent album. Show artist_namealbum_title, and release_date. Use the artists and albums tables. Order by artist_name.

SQL

Expected Query

Expected Output