…
…
In the Earbud music app, write the same business question three times with JOIN, subquery, and CTE, check that the results match, and pick the most readable solution.
The Earbud product team received the term report and has three more questions: which songs were never played, which songs free listeners play, and which songs are played above average. They also want each artist's hit song. The team lead sets one rule: "Your code will be read by someone else a year from now; choose the most readable solution."
There is no new concept in this task. You will use what you learned in the subqueries, set operations, and CTE lessons together: you will write the same question first with JOIN, then with a subquery, then with a CTE.
Data: five tables, familiar from Task 2. artist (id, name, genre, city), song (id, title, artist_id, duration_sec, release_year), listener (id, nickname, city, plan: free or premium, joined_on, invited_by_id), play (id, listener_id, song_id, played_on, seconds_played; each row is one play), and song_like (listener_id, song_id, liked_on). All values are fictional sample data.
Lessons used: joins (15–18), subqueries (20–21), set operations (22), and CTEs (23); aggregation and grouping (11–13).
One rule: the three solutions to the same question must give the same result. If they do not, one of them is wrong; find out which one and why.
Every step only reads; you can re-run the cells as many times as you like.
What you'll do
Required topics
earbud.sqlDownload file
Reading file…
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
The product team's first question: "Which songs are in the catalog but were never played?" Write it first with the familiar technique, an outer join.
Show the title (song) and the artist's name (artist) of songs that were never played; sort by song title. LEFT JOIN also returns the row with no match; in that row the columns of the right table are NULL.
Write a short reason on the first line: the "Why this query:" line tells the person who reads the code a year from now what you meant.
This step's code cell is for members.Sign up free
Think about the same question without a JOIN: "Songs that have no row at all in the play table." That sentence is almost word for word NOT EXISTS. One join is left for the artist's name; the play side moves into a subquery.
The output must be identical to step 1: same columns, same order, same 3 rows.
Why not `NOT IN`? WHERE s.id NOT IN (SELECT song_id FROM play) also gives the right result here, because play.song_id is defined
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
NOT NULLNULLNOT INNOT EXISTSNOT EXISTSThis step's code cell is for members.Sign up free
Third way: solve the question in set language first, then add the names. "All song ids, minus the ids of played songs" is an EXCEPT operation. Write a CTE that names this set unplayed, then join the CTE with the song and artist tables to get the names.
The output must again match step 1. EXCEPT removes duplicates by itself; even if a song was played ten times, its id is removed once.
Time to choose: you saw that all three solutions give the same 3 rows. Which of the three would you give to a teammate? Write your decision and your reason in one sentence on the "Why this query:" line. There is no single right answer; the reason matters.
This step's code cell is for members.Sign up free
Second question: "Which songs have been played at least once by free listeners?" The team wants to see the songs free listeners reach, for a premium campaign. Expected output: the song's id and title, each song once, sorted by id.
A teammate wrote the query below. It runs without errors, but the result is 110 rows. Yet the catalog has only 30 songs. Run it first and find out why the same song comes back many times, then fix it.
The hint is in the question: the same free listener may have played a song several times, and several listeners may have played it separately. JOIN makes every match a separate row; for a "who?" question, a result without duplicates does not come by itself.
This step's code cell is for members.Sign up free
Write the same question without a JOIN: "Songs whose id is among the song ids played by free listeners." This is an IN question. The subquery returns only a list of ids; the outer query reads from the song table, so each song appears at most once. DISTINCT is not needed.
To find the free listeners inside the subquery, you can use a second IN (two nested INs), or you can join play and listener inside the subquery. The output must match the result of step 4 exactly.
This step's code cell is for members.Sign up free
Third way: split the question into named steps. First a CTE named free_listener gives the ids of free listeners, then a CTE named free_listener_play gives the ids of the songs these listeners played (each song once). Finally, join this set with the song table and show id and title. The second CTE uses the first (chained CTEs).
The output must match steps 4 and 5. Where should the duplicates be removed this time? If you do not remove them in the second CTE, the final join again makes every play a separate row.
Time to choose: JOIN + DISTINCT, IN, or chained CTE. Which would you give to a teammate? Write your one-sentence reason on the "Why this query:" line.
This step's code cell is for members.Sign up free
Third question: "Which songs have a play count above the average play count per song?" Output: the song's title and its play count (plays); most played first, ties by song title. You choose the technique.
First make the question precise: "average per song" can be read two ways.
The two definitions give different results: under (b), songs played 8 times also rise above the average. The report asks for definition (a). Which average you calculate depends on how you count: an inner join does not bring the never-played songs at all, an outer join brings them with 0.
One more trap: the catalog has two songs with the same title ("Coffee Stain", by two different artists). If you group the count by song title, the two merge into one row. Group by id.
This step's code cell is for members.Sign up free
Last question: "What is each artist's most played song?" Output: the artist's name (artist), the song title (song), and the play count (plays). If an artist's two most played songs have the same number of plays, both should show. Order: most played first, ties by artist name, then song title. You choose the technique.
"The most" does not have to be one row. ORDER BY ... LIMIT 1 does not work separately for each artist and picks one arbitrarily on a tie. The safe way: find each artist's highest count, then get all songs equal to that count. There is no tie in this data right now, but your query must still be right when there is one tomorrow.
The suggested way is two CTEs: counts (play count per song and its artist) and tops (the highest count per artist); then match the two. It can also be solved with a correlated subquery.
Watch out: one artist has no songs at all. The answer to the question does not include it; the result shows 7 artists.
Time to choose: two CTEs or a correlated subquery? Which would you give to a teammate? One sentence.
This step's code cell is for members.Sign up free