Learn how to select songs that cost 0.99 and are longer than the average length using a simple PostgreSQL query. Perfect for beginners learning SQL.
Hello little learner! Let me explain this like you're 5.
Imagine you have a collection of songs (that's the track table). Each song has a price (unit_price) and how long it lasts in milliseconds (length). You want to find all the songs that cost 99 cents (0.99) and last longer than the average length of all songs.
To do this, you can ask the database:
The query looks like this in PostgreSQL:
SELECT * FROM track
WHERE unit_price = 0.99
AND milliseconds > (
SELECT AVG(milliseconds) FROM track
);
This query will give you the songs you want. Now, if you run this on the course database, you will get one of the answers you have: 494, 282, 1558, or 3502 rows.
Since I can't run the query for you, please run this query in your PostgreSQL environment to find the exact number.
If you want help writing more queries or planning lessons about databases, check out our tools:
Happy learning! 🎉