The ALL and ANY operators allow us to query data by comparing a value with a list of values that are returned by a subquery. This is an important distinction for the ANY and ALL operators, as they are focused on the lists from a subquery.
The syntax of the operator looks like:
<columnname> <operator> [ANY/ALL] (subquery);
The ANY operator is less restrictive than the ALL when it comes to comparisons. The ANY operator returns true if any of the values in the subquery meets the condition, otherwise it returns false. The ALL operator returns true if ALL of the values in the subquery meets the condition, otherwise it returns false.
Let's take a look at an example where we’re needing to compare the average invoice totals per country:
SELECT AVG(total)
FROM invoice
GROUP BY billing_country;
We will use that average by country as our subquery. If we wanted to find invoices that have a value higher than the average of any of the totals from the country, we would use the following:
SELECT *
FROM invoice
WHERE total > ANY(
SELECT AVG(total)
FROM invoice
GROUP BY billing_country);
To find invoices that have a value higher than all of the averages from all countries:
SELECT *
FROM invoice
WHERE total > ALL(
SELECT AVG(total)
FROM invoice
GROUP BY billing_country);
Notice the count difference between the two queries. The first query (with the ANY operator) is less restrictive.
With the ANY and ALL operator, the subquery must return a single column to compare. If we return more than one column in the subquery, only the first result set is returned:
SELECT *
FROM invoice
WHERE (total,total) >= ALL(
SELECT AVG(total),max(total)
FROM invoice
GROUP BY billing_country);
If we just compared the total to the max of the totals:
SELECT *
FROM invoice
WHERE total >= ALL(
SELECT max(total)
FROM invoice
GROUP BY billing_country);
One record would be returned:
You may notice that the query above uses >= vs a >. We can compare the ANY and ALL using a variety of operators.
[MUSIC PLAYING] Sub-queries aren't an issue if it returns a single grow as part of the result set. However, if we have an instance where we want to try to group some information based off of certain criteria.
For example, we might want to group it based on the building country within this piece here. If we run this part individually, we'll be able to see that it returns multiple different rows and result sets. However, if we try to run this query now, you'll see that it comes up with an error. The reason for that is because the sub-query itself is returning multiple different items. However, with our comparisons can only compare to a single option. We can use any and all as options becomes as part of the sub-query component, so that if we use any and we do a value that's greater than any, it'll check any value that's going to be part of the result set.
So basically with a greater than check, it's a look for the lowest value associated with it, and then do a comparison if that total is greater than the lowest value being returned, it's going to allow that to be displayed.
If we made a modification to it using all, it's going to ensure that whatever toll this is greater than all the values that are being returned. So basically in this case here, it's going to be larger than the largest value.
[MUSIC PLAYING]
Source: Authored by Vincent Tran