2017  Kodetalk | Feedback | Privacy Policy | Terms | About
userimage

SQL for Middle Value Rather than MIN/MAX or FIRST/LAST

Is there a SQL function to return the middle value of three?

For example, assume I have a table with people who have three cars, sorted alphabetically by AutoMaker.

John: Ford

John: Honda

John: VW

MIN(AutoMaker) returns Ford. MAX(AutoMaker) returns VW.

Is there a similar SQL function that will return Honda?

Thank you.

userimage

Longer answer: It's too specific. Hence, the "middle" in what you said is actually the second record. But if you had 5 records, it would be the third, and so on. If you need that in practice, just assign a row number to each row (Oracle, Access) and then select the ((n+1)/2)nd row (WHERE row_number = (n+1)/2).

PS - which is the middle row if you have 4 rows? :)

Answer is