AlgoMaster Logo

Exercise: DISTINCT

Last Updated: May 10, 2026

1 min read

For this exercise, we will work with below data:

SQL

Exercise 1

Easy

Return all distinct countries from the users table. Exclude NULLs and order alphabetically.

SQL

Expected Query

Expected Output

Exercise 2

Easy

Count how many distinct tracks have been streamed at least once. Use the streams table. Return a single value aliased as distinct_tracks.

SQL

Expected Query

Expected Output

Exercise 3

Medium

Return all unique (country, verified) pairs from the artists table. Order by country, then verified.

SQL

Expected Query

Expected Output

Exercise 4

Medium

Return all distinct countries that appear in the streams table for streams started on or after 2024-03-03. Order alphabetically.

SQL

Expected Query

Expected Output

Exercise 5

Medium

Count how many distinct users have streamed in each country. Return one row per country with country and the count aliased as unique_users. Use the streams table. Order by unique_users descending, then country ascending. Exclude NULL countries.

SQL

Expected Query

Expected Output

Exercise 6

Hard

Use DISTINCT ON to return the most recent stream per user along with the country of that stream. Show user_idcountrytrack_id, and started_at. Order the final result by user_id.

SQL

Expected Query

Expected Output