mallikarjun sharma mallikarjun sharma - 4 months ago 12
MySQL Question

Retrieve minimum and maximum strings from database table

I have one database table data like following.

abc
bcd
cdef
ferd
testd


I need to get minimum and maximum values like following

abc
testd


How can I write SQL query to get above output?

Answer

Assuming you want to retrieve the min and max values in two separate rows:

SELECT your_column FROM your_table
    JOIN
    (SELECT
        min(your_column) AS min_v,
        max(your_column) AS max_v FROM your_table
    ) minmax
    WHERE your_column IN(minmax.min_v,minmax.max_v);
Comments