SQL code to get min,max at a time

40 Views Asked by At

How to display min and max salary from a table at a time (2 records at a time, one with max and another with min)?

My input table data:

empid  ename sal
1       A    2000
2       B    1000
3       C    1500
4       D    5000
5       E    7000

Output:

sal
7000 -- max
2000 -- min
2

There are 2 best solutions below

0
Tom On BEST ANSWER

You can just use a Union:

Select MAX(Sal)
From TableA

UNION ALL

Select Min(Sal)
From TableA

This would give you your desired output:

sal
7000 -- max
2000 -- min

More information on Unions can be found here.

0
hbourchi On

You mean something like this?

select max(sal) sal from my_table union all select min(sal) sal from my_table