assignment_180b
assignment_180b
3.45.3
Let's define some helper functions for running queries and printing results
file:///Users/kunalsahni/Downloads/assignment.html Page 1 of 9
assignment 20/02/25, 10:41 PM
cursor = conn.cursor()
cursor = conn.cursor()
cursor.execute("delete from Songs;")
file:///Users/kunalsahni/Downloads/assignment.html Page 2 of 9
assignment 20/02/25, 10:41 PM
file:///Users/kunalsahni/Downloads/assignment.html Page 3 of 9
assignment 20/02/25, 10:41 PM
Users
user_id name email
1 Mickey [email protected]
2 Minnie [email protected]
3 Daffy [email protected]
4 Pluto [email protected]
Songs
song_id title artist genre
10 DJ Mix DJ None
Listens
listen_id user_id song_id rating listen_time
1 1 1 4.5 None
2 1 2 4.2 None
3 1 6 3.9 None
4 2 2 4.7 None
5 2 7 4.6 None
6 2 8 3.9 None
7 3 1 2.9 None
8 3 2 4.9 None
9 3 6 NaN None
file:///Users/kunalsahni/Downloads/assignment.html Page 4 of 9
assignment 20/02/25, 10:41 PM
In [110… """ Goal: Learn basic forms of SELECT, FROM, WHERE, DISTINCT """
qry_classic_songs = """
-- Find the titles and artists of songs in the "Classic" genre.
SELECT Songs.title, Songs.artist
FROM Songs
WHERE Songs.genre = 'Classic';"""
runSql('Classic songs', qry_classic_songs)
qry_genres = """
-- List of all genres in the Songs table
SELECT genre
FROM Songs;"""
runSql('All genres in the Songs table', qry_genres)
qry_distinct = """
-- List of unique genres in the Songs table
SELECT DISTINCT genre
FROM Songs;"""
runSql('Unique genres in the Songs table', qry_distinct)
qry_taylor_count = """
-- Songs by Taylor Swift in different genres
SELECT genre, count(*) as num_songs
FROM Songs
where artist = 'Taylor Swift'
GROUP BY genre;"""
runSql('Count songs by Taylor Swift in different genres', qry_taylor_count)
Classic songs
title artist
Yesterday Beatles
file:///Users/kunalsahni/Downloads/assignment.html Page 5 of 9
assignment 20/02/25, 10:41 PM
Pop
Pop
Rock
Rock
Rock
Classic
Classic
Classic
Rock
None
Pop
Rock
Classic
None
Pop 2
Rock 1
Query that calculates average ratings of all songs. Only includes songs with
Listens
In [111… qry_join_songs_ratings="""
SELECT Songs.song_id, Songs.artist, Songs.title, AVG(Listens.rating) as avg_
FROM songs
JOIN Listens
ON Songs.song_id = Listens.song_id
GROUP BY Songs.song_id"""
runSql('Calculates average ratings for songs', qry_join_songs_ratings)
file:///Users/kunalsahni/Downloads/assignment.html Page 6 of 9
assignment 20/02/25, 10:41 PM
querying recommendations
song_id avg_rating
6 3.9
1 3.7
cursor = conn.cursor()
create_temp_query = """
CREATE TEMPORARY TABLE temp_minnie_id AS
SELECT user_id FROM Users WHERE name = "Minnie";
"""
recommendations_query = """
CREATE TEMPORARY TABLE temp_top_songs AS
file:///Users/kunalsahni/Downloads/assignment.html Page 7 of 9
assignment 20/02/25, 10:41 PM
insert_query = """
INSERT INTO RECOMMENDATIONS (user_id, song_id, recommendation_id, recommenda
SELECT
(SELECT user_id FROM temp_minnie_id),
song_id,
ROW_NUMBER() OVER (ORDER BY avg_rating DESC) - 1 as recommendation_id,
datetime('now')
FROM temp_top_songs;
"""
drop_temp_query = """
DROP TABLE temp_minnie_id;
DROP TABLE temp_top_songs;
"""
cursor.execute(create_temp_query)
cursor.execute(recommendations_query)
cursor.execute(insert_query)
conn.commit()
conn.close()
print_query
user_id song_id recommendation_id recommendation_time
2 6 0 2025-02-21 06:40:56
2 1 1 2025-02-21 06:40:56
runSql("retreival_query", retreival_query)
file:///Users/kunalsahni/Downloads/assignment.html Page 8 of 9
assignment 20/02/25, 10:41 PM
retreival_query
title artist
Yesterday Beatles
In [ ]:
file:///Users/kunalsahni/Downloads/assignment.html Page 9 of 9