What is the difference between any and all in SQL query statements?

Source: Internet
Author: User

Any requires that your results be equal to any one.
All is the

Cases:
To pick out the order amount from the sales list is greater than "E0013 the clerk in 1996/11/10, the amount of any order received" all orders, and display the relevant field information
Select sale_id, Tot_amt
From sales
where Tot_amt > any
(Select Tot_amt from Sales
where sale_id= ' e0013 '
and order _date= ' 1996/11/10 ')


When you change any to all, the statement means that the order amount is greater than "E0013" for all orders that the clerk received on this day in 1996/11/10.

Another example:

CREATE TABLE #A (ID int)
Go
INSERT into #A values (1)
INSERT into #A values (2)
INSERT into #A values (3)
INSERT into #A values (4)
Go

--all: The entire condition is true for all data, for example: 5 is greater than all returned IDs
SELECT *
From #A
where 5>all (select ID from #A)
Go

--any: As long as one data satisfies the condition, the whole condition is set up, for example: 3 is greater than 1, 2
SELECT *
From #A
where 3>any (select ID from #A)
Go
--some is the same as any

Note: In the usage of any, it is possible to use the method of Min/max value first select subquery in the case of a number pair, and in some cases it is more efficient

What is the difference between any and all in SQL query statements?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.