AlgoMaster Logo

Exercise: INTERSECT and EXCEPT

Last Updated: May 12, 2026

1 min read

For this exercise, we will work with below data:

SQL

Exercise 1

Easy

Write a query that returns the countries that appear in both the users.country column and the artists.country column. Exclude NULL countries.

SQL

Expected Query

Expected Output

Exercise 2

Easy

Write a query that returns track_id values present in the tracks catalog but missing from playlist_tracks

SQL

Expected Query

Expected Output

Exercise 3

Medium

Write a query using INTERSECT that returns user_id values for users who are currently on a premium plan (plan = 'premium' and end_date IS NULL) AND have created at least one public playlist.

SQL

Expected Query

Expected Output

Exercise 4

Medium

Write a query that returns the country values that have a users row but no matching country value in the streams table.

SQL

Expected Query

Expected Output

Exercise 5

Hard

Write a query using EXCEPT that returns the (user_id, artist_id) pairs from artist_follows where the user has NOT streamed any track by that artist. Tracks belong to albums which belong to artists.

SQL

Expected Query

Expected Output