2

I have a problem with a query. I have a db with car and their spec, every model has a different car related to that model with its price. I want to create a query that gives me the min and max price for each models.

something like this:

+--------+----------+
| model  | price    |
+--------+----------+
| golf   | 4000     | 
| golf   | 6000     |
| golf   | 10000    | 
| panda  | 3000     | 
| panda  | 5000     |
| panda  | 7000     |
+--------+----------+

the query give me this:

+--------+----------+
| model  | price    |
+--------+----------+
| golf   | 4000     | 
| golf   | 10000    | 
| panda  | 3000     | 
| panda  | 7000     |
+--------+----------+

Can you help me?

Erik
  • 4,833
  • 4
  • 28
  • 57

1 Answers1

6

You can simple use UNION ALL:

SELECT model, MIN(price) price
FROM YourTable
GROUP BY model
UNION ALL
SELECT model, MAX(price) price
FROM YourTable
GROUP BY model;
Lamak
  • 2,576
  • 1
  • 24
  • 30