Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL: do we need ANY/SOME and ALL keywords?

I'm using SQL (SQL Server, PostgreSQL) over 10 years and still I'm never used ANY/SOME and ALL keywords in my production code. All situation I've encountered I could get away with IN, MAX, MIN, EXISTS, and I think it's more readable.

For example:

-- = ANY
select * from Users as U where U.ID = ANY(select P.User_ID from Payments as P);

-- IN
select * from Users as U where U.ID IN (select P.User_ID from Payments as P);

Or

-- < ANY
select * from Users as U where U.Salary < ANY(select P.Amount from Payments as P);

-- EXISTS
select * from Users as U where EXISTS (select * from Payments as P where P.Amount > U.Salary);

Using ANY/SOME and ALL:

  • PostgreSQL
  • SQL Server
  • MySQL
  • SQL FIDDLE with some examples

So the question is: am I missing something? is there some situation where ANY/SOME and ALL shine over other solutions?

like image 584
Roman Pekar Avatar asked Jul 11 '13 08:07

Roman Pekar


People also ask

Why do you use the any and all operators?

The ANY and ALL operators allow you to perform a comparison between a single column value and a range of other values.

What is some and all in SQL?

SOME must match at least one row in the subquery and must be preceded by comparison operators. Suppose using greater than ( >) with SOME means greater than at least one value. Syntax: SELECT [column_name... | expression1 ] FROM [table_name] WHERE expression2 comparison_operator {ALL | ANY | SOME} ( subquery )

How does any and all work in SQL?

ANY means that the condition will be satisfied if the operation is true for any of the values in the range. ALL means that the condition will be satisfied only if the operation is true for all values in the range. mentioning that SOME and ANY are synonyms.

How many keywords are in SQL?

In this article, we have seen SQL keywords that are available in SQL. We have seen 44 SQL keywords, example them by giving their syntax and example. The Select is the most used statement in SQL that is used to select the data from a database, and that data is returned is stored in a table.


2 Answers

I find ANY and ALL to be very useful when you're not just testing equality or inequality. Consider

'blah' LIKE ANY (ARRAY['%lah', '%fah', '%dah']);

as used my answer to this question.

ANY, ALL and their negations can greatly simplify code that'd otherwise require non-trivial subqueries or CTEs, and they're significantly under-used in my view.

Consider that ANY will work with any operator. It's very handy with LIKE and ~, but will work with tsquery, array membership tests, hstore key tests, and more.

'a => 1, e => 2'::hstore ? ANY (ARRAY['a', 'b', 'c', 'd'])

or:

'a => 1, b => 2'::hstore ? ALL (ARRAY['a', 'b'])

Without ANY or ALL you'd probably have to express those as a subquery or CTE over a VALUES list with an aggregate to produce a single result. Sure, you can do that if you want, but I'll stick to ANY.

There's one real caveat here: On older Pg versions, if you're writing ANY( SELECT ... ), you're almost certainly going to be better off in performance terms with EXISTS (SELECT 1 FROM ... WHERE ...). If you're on a version where the optimizer will turn ANY (...) into a join then you don't need to worry. If in doubt, check EXPLAIN output.

like image 71
Craig Ringer Avatar answered Oct 14 '22 05:10

Craig Ringer


No, I've never used the ANY, ALL, or SOME keywords either, and I've never seen them used in other people's code. I assume these are vestigal syntax, like the various optional keywords that appear in some places in SQL (for example, AS).

Keep in mind that SQL was defined by a committee.

like image 32
Bill Karwin Avatar answered Oct 14 '22 06:10

Bill Karwin