Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

T-SQL "<> ANY(subquery)"

Tags:

sql-server

I have a question about the Any-Operator.

On Technet it says

For example, the following query finds customers located in a territory not covered by any sales persons.

Use AdventureWorks2008R2;
GO
SELECT
    CustomerID
FROM
    Sales.Customer
WHERE
    TerritoryID <> ANY
    (
        SELECT
            TerritoryID
        FROM
            Sales.SalesPerson
    );

Further

The results include all customers, except those whose sales territories are NULL, because every territory that is assigned to a customer is covered by a sales person. The inner query finds all the sales territories covered by sales persons, and then, for each territory, the outer query finds the customers who are not in one.

But that query returns all customers. I updated a customers TerritoryID to a value that no sales.person has, but still that query returns all customers, instead of that one I expected ..

Am I missing something ? Might it be that that article on technet is simply wrong ? https://technet.microsoft.com/de-de/library/ms187074(v=sql.105).aspx (german)

There is one customer with TerritoryID = 13

Inner query result (SELECT TerritoryID FROM Sales.SalesPerson) : 4 2 4 3 6 5 1 4 6 1 1 6 9 1 8 10 7

And in table Sales.Customer is a row with CustomerID = 13, which is the one not covered by a sales-person..

like image 501
Erik Mandke Avatar asked Oct 03 '16 15:10

Erik Mandke


People also ask

Is != and <> same in SQL?

Here is the answer – Technically there is no difference between != and <>. Both of them work the same way and there is absolutely no difference in terms of performance or result.

What does <> do in SQL Server?

<> operator means not equal to in MS SQL. It compares two expressions (a comparison operator). When you compare nonnull expressions, the result is TRUE if the left operand is not equal to the right operand; otherwise, the result is FALSE. If either or both operands are NULL, see the topic SET ANSI_NULLS (Transact-SQL).


2 Answers

create table #t1
(
id int
)

insert into #t1
values(1),(2),(3)

As you can see,T1 has three values

now lets see,how Any Works

When 'is Equal to ' is used with any ,it works like IN

select * from #t1 where id=
any(select 0)--no result

when Any is used with > or <> ,Any means get me all the values which are greater than minimum value

select * from #t1 where id<>
any(select 1)--2,3

select * from #t1 where id<>
any(select 0)--1,2,3

If your subquery returns one value,the outer query will try to get values which are greater than inner query

like image 115
TheGameiswar Avatar answered Oct 10 '22 04:10

TheGameiswar


<> ANY means any Sales.Customer with a TerritoryID that is Greater Than or Less Than any of the TerritoryID's in the Sales.SalesPerson

so TerritoryID = 13 is greater than all or your examples (4 2 4 3 6 5 1 4 6 1 1 6 9 1 8 10 7), so it's included.

<> ALL is the equivalent of NOT IN so that is what you're confusing <> ANY with

Look at <> ANY as, if there are any records in the set that are not equal to the quailifier, then include it.

like image 38
JamieD77 Avatar answered Oct 10 '22 03:10

JamieD77