Sci-Fi Viewers
Return distinct user_id values for all users who have watched at least one Sci-Fi movie, ordered by user_id.
- Joins
- Filtering
- Sorting
- Distinct values
Challenge brief
Understand the request
Recommendations — Sci-Fi Cluster The recommendations team is building a Sci-Fi affinity segment to test a targeted content push.
Return distinct Sci-Fi viewer IDs and order the result by user ID.
Return
- user_id
Constraints
- Return identified users who watched at least one 'Sci-Fi' movie
- Return each user once
- Order by user ID
Data you will use
Review the relevant tables before deciding how to join, filter, or aggregate them.
watch_history
user_idINTEGERmovie_idINTEGER
movies
movie_idINTEGERgenreVARCHAR(50)
Hints, when you need them
Open one clue at a time so you still do the reasoning.
Hint 1
Connect watch facts to catalog genres through movie ID.
Hint 2
Filter the genre and deduplicate identified viewers.
Hint 3
Return viewer IDs in stable order.
Verified SQL answer
Attempt the problem first, then compare structure and reasoning—not just syntax.
Reveal solution and explanation
SELECT DISTINCT w.user_id FROM watch_history w INNER JOIN movies m ON w.movie_id = m.movie_id WHERE w.user_id IS NOT NULL AND m.genre = 'Sci-Fi' ORDER BY w.user_idWhy this works
Joining watch_history to movies links each viewing session to its genre. WHERE genre = 'Sci-Fi' keeps only Sci-Fi sessions. DISTINCT prevents a user appearing twice if they watched multiple Sci-Fi titles.
Success check
Returns the complete deterministic result for sci-fi viewers
Expected result
Use this output to verify values, aliases, ordering, and row count.
| user_id |
|---|
| 1 |
| 2 |
| 5 |
| 6 |
| 8 |
Learn the concepts behind this answer
Strengthen your understanding with these targeted learning topics:
Continue practicing
Explore related company challenges
Google
Independent Google-style search, advertising, user-engagement, and video-product SQL practice.
Meta
Independent Meta-style social-product, engagement, content, community, messaging, and advertising SQL practice.
Airbnb
Independent Airbnb-style marketplace, booking, listing, payment, review, and guest analytics SQL practice.
Return to the complete interview preparation experience.