A correlated subquery references the outer query. Find tracks that are longer than the average length of tracks in their own genre.
Write a query using a correlated subquery to find tracks longer than their genre's average duration.
| 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 |
|---|---|
| GenreId | INTEGER (Primary Key) |
| Name | TEXT |
SELECT * FROM TableA a
WHERE column > (SELECT AVG(column) FROM TableA WHERE condition = a.condition)
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.