Can I use ALL or ANY to find minimum value in SQL?
Question
Example of Table :
Table name : Ex1
Number
2
5
6
7
1
5
10
How to find the minimum value by using ALL or ANY in SQL ?
Solution
I believe you must rewrite your question describing a little bit more your answer, if possible in which database you would like to do this.
I don't know why would you like to do that instead of using MIN function.
In Oracle you could do this
select * from ex1 where number <= ANY (select number from ex1 where rownum=1 order by number asc)
But this will have the same result as:
select * from ex1 where rownum=1 order by number asc
You could also do this:
select * from ex1 where number <= ALL (select number from ex1)
You can find more information here.
OTHER TIPS
At the risk of upsetting my SQL server instance by using ANY (ALL would be better),
select number
from ex1
where number not in
(select number from ex1 where number > any (select number from ex1))
I feel dirty now.
Using ALL
SELECT *
FROM Ex1
WHERE Number <= ALL ( SELECT Number FROM Ex1 AS T2 );
Using ANY
SELECT *
FROM Ex1
WHERE NOT Number > ANY ( SELECT Number FROM Ex1 AS T2 );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow