Nukodi
Nukodi

Reputation: 357

How to find the lowest price item(s) in a list

I want to write a query to find the cheapest item(s) in a table. So lets say I have a table with:

ID   Price
123   4    
234   5   
456   6    
567   4

So I would want a result that prints out:

123    
567

I have written

select id, min(price)
from list

but this prints out both the price and id. And I only want the id.

I also tried

select id, min(price)
from list
group by id

to try and get multiply items if some items have the same lowest price. But this ended up printing out the entire table. I'm stumped on what to do.

Upvotes: 1

Views: 7579

Answers (2)

GrabNewTech
GrabNewTech

Reputation: 641

Try the below query.

SELECT
  id
FROM list
WHERE price IN (SELECT
  MIN(price)
FROM list)

Upvotes: 1

Pham X. Bach
Pham X. Bach

Reputation: 5432

You could just use this:

SELECT id
FROM list
WHERE price = (SELECT MIN(price) FROM list);

Upvotes: 6

Related Questions