Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 query

Author  Topic 

indigo480
Starting Member

7 Posts

Posted - 2007-01-16 : 20:52:28
Hello,

I need assistance with a simple query.

I have a product table which includes a Product Name and ListPrice columns. I want to query for the highest price product from the table along with the name of the product.

My code is as follows:

select max(listprice) as price, name
from production.product
group by name

With this query, it returns all rows. My intent is only return the row/s of product with the max price.

Why is it that this query returns all, instead of the desired results. what should the query look like to accomplish this?

Thanks in advance.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2007-01-16 : 21:18:05
If you only want the rows that equal the max listprice, then this should do it:

select listprice, name
from production.product
where listprice = (select max(listprice) from production.product)

Your current query says to get me the max listprice for each of the distinct names it finds.

Tara Kizer
Go to Top of Page

indigo480
Starting Member

7 Posts

Posted - 2007-01-16 : 21:38:34
Oh, I see. The subquery filters for the max price only.

Thanks, I appreciate the assistance.

Go to Top of Page
   

- Advertisement -