…
…
Answer eight questions from a music app's product team, each from a single table; filter, sort, handle NULL correctly, add labels, and fix a broken query.
Earbud is a music app, and you have just joined the product team. In your first week the team asks you eight questions. Each one can be answered by looking at a single table; no joins or grouping are needed. You have not seen this database before, so for every question you will decide which table to look at first.
Data: the database has five tables; in this task you use only three of them, one table at a time.
artist (id, name, genre, city): 8 artists.song (id, title, artist_id, duration_sec, release_year): 30 songs. duration_sec is the length of the song in seconds, artist_id is the id of the artist who performs the song, and release_year is the year it came out.listener (id, nickname, city, plan, joined_on, invited_by_id): 25 listeners. plan is one of two values: 'free' or 'premium' (lowercase). invited_by_id is the id of the listener who invited this listener to the app; it is empty (NULL) if they came on their own.The play and song_like tables are in the database too; they belong to the task of the next unit. All values are fictional sample data.
Lessons used: 3–9 (selecting and calculating columns, filtering, condition tools, sorting and distinct lists, NULL, CASE, order of execution and reading errors). No new concepts.
The first line of every step's code starts with -- Why this query:; complete it with one sentence before you write the query (the check does not measure it, but it organizes your thinking). Every step only reads; you can re-run the cells as many times as you like. If you want to see what a table looks like, first look at it with a query such as SELECT * FROM song;.
What you'll do
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.
Free listeners hear a 15-second ad after every song. The team wants to see how long each song lasts with its ad.
Get the title (title) and duration (duration_sec) of every song, plus the duration with the ad as with_ads_sec. No order was requested: the check ignores row order and looks at column names.
This step's code cell is for members.Sign up free
The team wonders how much space the older songs take in the app's archive.
Get the title (title) and year (release_year) of songs released before 2012; 2012 is not included. Sort by year, and songs from the same year by title.
This step's code cell is for members.Sign up free
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 marketing team is planning a concert campaign in Lisbon, Toronto, and Nairobi and wants to tell the premium listeners in those cities first.
Get the nickname (nickname), city (city), and join date (joined_on) of premium listeners who live in Lisbon, Toronto, or Nairobi. When you have the result, check it once by eye: no free listener should be in the list.
This step's code cell is for members.Sign up free
The content team is building two playlists, "coffee" and "hour", and is looking for songs with these words in the title. The word can be at the start, in the middle, or at the end of the title, and it can be written in upper or lower case.
Get the title (title) and year (release_year) of songs with "coffee" or "hour" in the title; case should not matter.
Think (not checked): the result has two "Coffee Stain" songs. Are they the same song? Which column tells you? You can check with a SELECT yourself.
This step's code cell is for members.Sign up free
The team will draw a map and asks in which cities the app is used.
Get the cities where listeners live: each city should appear once, in alphabetical order.
The result has 7 cities. Many listeners share a city, so without removing duplicates you would get 25 rows.
This step's code cell is for members.Sign up free
The team is checking whether the invite system brings in premium listeners.
Get the nickname (nickname) and the inviter's id (invited_by_id) of listeners who joined through a friend's invitation and are premium. A listener who did not come through an invitation has an empty invited_by_id (NULL).
This step's code cell is for members.Sign up free
The team wants to see at a glance how each listener came to the app.
Get each listener's nickname (nickname), plan (plan), and how they came as arrival: Joined on their own if there is no inviter, Joined by invitation if there is. Sort by listener id (id).
Think (not checked): 18 of the 25 listeners came through an invitation. What does that number tell the team, and what does it not tell them?
This step's code cell is for members.Sign up free
The product team wants the songs longer than 300 seconds with the ad, from longest to shortest; these are the first candidates for shortening the ad. The intern's query is below, but it does not work.
Run it first and read the error message. When you fix it you are not done: compare the result with what was asked and make sure the order is right.
This step's code cell is for members.Sign up free