Can anyone tell me how to select the minimum AND the maximum from a table using the MIN and MAX and UNION keywords.
I tried to use:
SELECT ename, MIN(sal)
FROM emp
UNION
SELECT ename, MAX(sal)
FROM emp;
But it's only displaying the same name of the MIN, like:
smith | 800
smith | 5000
Needs to be:
smith | 800
king | 5000
select name,max(salary) from employee where salary not in(select max(salary) from employee);
o/p:-amit 65000 Ex o/p:madhu 6500
Answer: select name,salary from employee where salary =(select max(salary) from employee where salary not in(select max(salary) from employee)); o/p: madhu 6500
How about:
Try this
Result:
Didn't actually test it, but this general idea should work:
Note that this can return more than 2 rows if there is more than one row with the (same) MIN or MAX value. It could also return just one row if both MIN and MAX happen to come from the same row (you could use UNION ALL to avoid that).
BTW, your query means: Get MIN(sal) and then get the
ename
from a random row (and the same for MAX). In both MIN and MAX cases the MySQL decided to return the same random row.However, in most databases other than MySQL, you could'n even have this kind of "random" query - you'd be required to include the
ename
in GROUP BY.