AlgoMaster Logo

Exercise: Subquery Fundamentals

Last Updated: May 12, 2026

1 min read

For this exercise, we will work with below data:

SQL

Exercise 1

Easy

Find all tracks whose duration_seconds is greater than the average track duration. Show title and duration_seconds from the tracks table. Order by duration_seconds descending.

SQL

Expected Query

Expected Output

Exercise 2

Easy

Use a scalar subquery in WHERE to return the title and duration of the longest track on the tracks table.

SQL

Expected Query

Expected Output

Exercise 3

Medium

Find tracks whose duration_seconds is greater than the average track duration on the album with album_id = 1 (Ocean Drive). Show title and duration_seconds from the tracks table. Order by duration_seconds descending.

SQL

Expected Query

Expected Output

Exercise 4

Medium

Find tracks belonging to albums in the Pop genre using IN with a subquery on the albums and genres tables. Show title and duration_seconds from the tracks table. Order by title.

SQL

Expected Query

Expected Output

Exercise 5

Medium

Use a derived table to return each user's total duration_listened from the streams table, joined back to users for the username. Show username and total_seconds. Only include users with at least one stream. Order by total_seconds descending.

SQL

Expected Query

Expected Output

Exercise 6

Hard

Find tracks whose duration_seconds is at least half the longest duration on the platform. Use a scalar subquery with arithmetic in the WHERE clause on the tracks table. Show title and duration_seconds. Order by duration_seconds descending.

SQL

Expected Query

Expected Output