Question

4. Consider the following schema for an online streaming service, where users are allowed to play...

4. Consider the following schema for an online streaming service, where users are allowed to play (stream) songs performed by different artists. Primary and foreign key constraints are also listed for the schema of each table.

User (ID, Password, Name, Location)   Primary key = ID

Artist (ID, Name, Birthyear)   Primary key = ID

Song (ID, Title, Album, ArtistID)   

    Primary key = ID

    Song(ArtistID) references Artist(ID)

Play (ID, UserID, SongID, Timestamp)   

    Primary key = ID

    Play(UserID) references User(ID)

    Play(SongID) references Song(ID)

Express each of the following query in SQL.

(a) Find the unique IDs of users who have played songs performed by the artist named Michael Jackson.

Solution:

(b) Find the unique IDs of users who have played more than one song performed by the artist named Michael Jackson. In other words, your query result must include only those users who have played at least 2 different songs performed by Michael Jackson.

Solution:

(c) Find the title of the most popular song, i.e., the song that received the highest play count (i.e., number of times it was played), and its corresponding play count. Note that a user can play the same song more than once (so the play count should sum up the total number of times each user has played a given song).

Solution:

(d) Find the titles of songs that have never been streamed (played) by any user.

Solution:

0 0
Add a comment Improve this question Transcribed image text
Answer #1

(a) Find the unique IDs of users who have played songs performed by the artist named Michael Jackson.

Solution:  SELECT DISTINCT p.UserID FROM Play p, Song s, Artist a WHERE p.SongID = s.SongID AND s.ArtistID = a.ID AND a.Name = 'Michael Jackson';

(b) Find the unique IDs of users who have played more than one song performed by the artist named Michael Jackson. In other words, your query result must include only those users who have played at least 2 different songs performed by Michael Jackson.

Solution: SELECT DISTINCT p.UserID FROM Play p, Song s, Artist a WHERE p.SongID = s.ID AND s.ArtistID = a.ID AND a.Name = 'Michael Jackson' GROUP BY p.UserID HAVING count(*) > 1;

(c) Find the title of the most popular song, i.e., the song that received the highest play count (i.e., number of times it was played), and its corresponding play count. Note that a user can play the same song more than once (so the play count should sum up the total number of times each user has played a given song).

Solution: SELECT s.Title, temp.COUNT FROM Song s, (SELECT SongID, max(sum(*)) AS COUNT FROM Play GROUP BY SongID, UserID) AS temp WHERE temp.SongID = s.ID;

(d) Find the titles of songs that have never been streamed (played) by any user.

Solution: SELECT Title FROM Song WHERE ID NOT IN (SELECT UserID FROM Play);

Add a comment
Know the answer?
Add Answer to:
4. Consider the following schema for an online streaming service, where users are allowed to play...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write an SQL query to return the users who posted the most number of jokes on...

    Write an SQL query to return the users who posted the most number of jokes on 1/1/2019. Database: create table User( userid varchar(30) not null, password varchar(30), firstname varchar(30), lastname varchar(50), email varchar(50), gender char(1), age integer, banned boolean, primary key(userid), unique(email)); create table MyFriends( userid varchar(30), friendid varchar(30), primary key(userid,friendid), foreign key(userid) references User, foreign key(friendid) references User(userid)); Create table Jokes( Jokeid integer, title varchar(100), description varchar(255), authorid varchar(30) not null, primary key(jokeid), posttime Datetime, foreign key(authorid) references User(userid));...

  • QUESTION: (Answer in SQL) Write an SQL query to find all pairs of users (A,B) such...

    QUESTION: (Answer in SQL) Write an SQL query to find all pairs of users (A,B) such that both A and B have blurted on a common topic but A is not following B. Your query should print the names of A and B in that order. BACKGROUND INFO: Users can post their thoughts in form of short messages that we call “blurts”. When signing up, users need to provide their email and a password of their choice. In addition, they...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT