SQL SELECT and WHERE for beginners
Use SELECT, WHERE, and ORDER BY to produce a short list of affordable albums that are in stock.
Sample query
SELECT title, price, stock
FROM albums
WHERE stock > 0 AND price < 20
ORDER BY price, title;Decide what belongs in the result
Suppose the record shop needs a shelf list for albums under 20 that customers can buy today. Each result row should represent one album. The title identifies it, the price helps staff arrange the display, and the stock tells them how many copies are available. Those three columns are enough for this list.
Turn the requirements into conditions
Start with the albums table. The condition stock > 0 removes albums with no copies. The condition price < 20 sets a strict upper bound, so an album priced at exactly 20 would be excluded. AND requires each row to pass both tests. ORDER BY price, title puts cheaper albums first and breaks price ties alphabetically.
Check the result against the request
Blue Hour comes first at 16, followed by Night Bus at 18 and Coastline at 19. Check one excluded album too. Static Bloom has copies in stock, but its price of 22 fails the second test. Change AND to OR and it would appear, because one passing condition would be enough.
Query result
| title | price | stock |
|---|---|---|
| Blue Hour | 16 | 3 |
| Night Bus | 18 | 7 |
| Coastline | 19 | 8 |
