SQL Question: Getting the row of min and max only

jamesave

Golden Member
Aug 27, 2000
1,610
0
76
suppose that I have a table with the following data:

Item Price
Apple $1.50
Orange $1.75
Kiwi $2.00
Tomato $0.99

What would be the correct SQL command to display the item with the max and min price at the same time?

TIA
 

KLin

Lifer
Feb 29, 2000
30,427
745
126
a union query.

SELECT Item, Max(Price) as ItemPrice from Table1 Group By Item
UNION
SELECT Item, min(Price) as ItemPrice from Table1 Group By Item
 

Hyperblaze

Lifer
May 31, 2001
10,027
1
81
SELECT max(Price) as maxPrice, min(Price) as minPrice
FROM Table1

(mind you, I have not tested this out, but it should work)
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
Originally posted by: Hyperblaze
SELECT max(Price) as maxPrice, min(Price) as minPrice
FROM Table1

(mind you, I have not tested this out, but it should work)

I think it's actually...

SELECT maxPrice AS Max(Price), minPrice AS Min(Price) ... etc.

I considered suggesting this too, but it's hard to tell what it is the OP really needs to do. This query will get you the two numbers, but not the item names. For that the union query suggested by KLin is the way to go (but of course returns two rows, and who knows what the OP meant by "at the same time"), but I haven't quite figured out why the 'group by' is necessary.

As a general note to all young programmers, and even the not so young ones: we're in a technical business, and precision of language and description is not elective. I'm continually amazed by how vaguely people will describe programming challenges and problems when asking for help.
 

Hyperblaze

Lifer
May 31, 2001
10,027
1
81
Originally posted by: Markbnj
Originally posted by: Hyperblaze
SELECT max(Price) as maxPrice, min(Price) as minPrice
FROM Table1

(mind you, I have not tested this out, but it should work)

I think it's actually...

SELECT maxPrice AS Max(Price), minPrice AS Min(Price) ... etc.

I considered suggesting this too, but it's hard to tell what it is the OP really needs to do. This query will get you the two numbers, but not the item names. For that the union query suggested by KLin is the way to go (but of course returns two rows, and who knows what the OP meant by "at the same time"), but I haven't quite figured out why the 'group by' is necessary.

As a general note to all young programmers, and even the not so young ones: we're in a technical business, and precision of language and description is not elective. I'm continually amazed by how vaguely people will describe programming challenges and problems when asking for help.

If you type this....

SELECT maxPrice AS Max(Price), minPrice AS Min(Price) ... etc.

You'll be getting the maxPrice field labelled as "Max(Price)"

The way I have it, it finds the max price and min price and labells the field without any special characters
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
Originally posted by: Hyperblaze
Originally posted by: Markbnj
Originally posted by: Hyperblaze
SELECT max(Price) as maxPrice, min(Price) as minPrice
FROM Table1

(mind you, I have not tested this out, but it should work)

I think it's actually...

SELECT maxPrice AS Max(Price), minPrice AS Min(Price) ... etc.

I considered suggesting this too, but it's hard to tell what it is the OP really needs to do. This query will get you the two numbers, but not the item names. For that the union query suggested by KLin is the way to go (but of course returns two rows, and who knows what the OP meant by "at the same time"), but I haven't quite figured out why the 'group by' is necessary.

As a general note to all young programmers, and even the not so young ones: we're in a technical business, and precision of language and description is not elective. I'm continually amazed by how vaguely people will describe programming challenges and problems when asking for help.

If you type this....

SELECT maxPrice AS Max(Price), minPrice AS Min(Price) ... etc.

You'll be getting the maxPrice field labelled as "Max(Price)"

The way I have it, it finds the max price and min price and labells the field without any special characters

Yep, you're right. Your original ordering of the terms was correct.
 

KLin

Lifer
Feb 29, 2000
30,427
745
126
Actually my solution does not work :eek:. Try this

Select tblMax.Item, MaxPrice, tblMin.Item, MinPrice
FROM
(SELECT Top 1 Item, Price As MaxPrice from Table1 Order by Price DESC) tblMax,
(SELECT Top 1 Item, Price As MinPrice from Table1 Order By Price ASC) tblMin