SELECT MIN(retail)
FROM books
WHERE category = 'COMPUTER'
works fine, but when I include title in select like:
SELECT MIN(retail), title
FROM books
WHERE category = 'COMPUTER'
it doesn't. Why? How to make it work?
Rhys's answer is correct, if that is what you mean, but you might have wanted the title
(s) where retail=MIN(retail)
, and that wording suggests how to get that answer:
SELECT title, retail
FROM books
WHERE category = 'COMPUTER'
AND retail = (SELECT MIN(retail) FROM books WHERE category = 'COMPUTER')
To reduce duplication you can use a WITH
clause (if you're using a recent version of SQL):
;WITH ComputerBooks AS (
SELECT title, retail
FROM books
WHERE category = 'COMPUTER')
SELECT title, retail
FROM ComputerBooks
WHERE retail = (SELECT MIN(retail) FROM ComputerBooks)