You need to assign a sequential number to each track within its album, ordered by the track's position (TrackId). This is useful for creating numbered playlists.
Write a SQL query that assigns a row number to each track within its album, ordered by TrackId.
| Column | Type |
|---|---|
| TrackId | INTEGER (Primary Key) |
| Name | TEXT |
| AlbumId | INTEGER (Foreign Key → Album.AlbumId) |
| MediaTypeId | INTEGER (Foreign Key) |
| GenreId | INTEGER (Foreign Key → Genre.GenreId) |
| Composer | TEXT |
| Milliseconds | INTEGER |
| Bytes | INTEGER |
| UnitPrice | NUMERIC(10,2) |
| Column | Type |
|---|---|
| AlbumId | INTEGER (Primary Key) |
| Title | TEXT |
| ArtistId | INTEGER (Foreign Key → Artist.ArtistId) |
ROW_NUMBER() OVER (PARTITION BY column ORDER BY column)
Ready to take your skills to the next level? Enroll in our comprehensive Data Analyst Career Track to master SQL, Python, Excel, and Power BI.
Run your code to see the result here.