USE tempdb
GO
CREATE TABLE Table1 (Id int)
GO
INSERT INTO Table1
VALUES (1), (2), (3), (4), (5)
CREATE TABLE Table2 (Id int)
GO
INSERT INTO Table2
VALUES (1), (3), (5)
-- =ANY sees whether the scalar value is
-- equal to any from Table2
-- Eg. This checks:
-- (Table1:Id1 = Table2:Id1)
-- OR (Table1:Id1 = Table2:Id3)
-- OR (Table1:Id1 = Table2:Id5)
SELECT Id FROM Table1
WHERE Id =ANY (SELECT Id FROM Table2)
-- Finally, code returns 1, 3, and 5.
-- =ALL sees whether the scalar value is
-- equal with all values in Table2.
-- Eg. (Table1:Id1 = Table2:Id1)
-- AND (Table1:Id1 = Table2:Id3)
-- AND (Table1:Id1 = Table2:Id5)
SELECT Id FROM Table1
WHERE Id =ALL (SELECT Id FROM Table2)
-- Code returns nothing.
-- >=ANY sees whether the scalar value is
-- greater than any value in Table2
-- Eg. (Table1:Id1 > Table2:Id1)
-- OR (Table1:Id1 > Table2:Id3)
-- OR (Table1:Id1 > Table2:Id5)
-- Since the smallest one in Table2 is 1,
-- As long as the Table1:Id is >= 1
-- It is added to the resultset.
SELECT Id FROM Table1
WHERE Id >=ANY (SELECT Id FROM Table2)
-- Code returns 1, 2, 3, 4, and 5
-- >=ALL sees whether the scalar value is
-- greater than all in Table2
-- Eg. (Table1:Id1 > Table2:Id1)
-- AND (Table1:Id1 > Table2:Id3)
-- AND (Table1:Id1 > Table2:Id5)
-- Since the highest number in Table2 is 5,
-- As long as Table1:Id is >= 5,
-- It is added to the resultset.
SELECT Id FROM Table1
WHERE Id >=ALL (SELECT Id FROM Table2)
-- Code returns 5 only.
GO
CREATE TABLE Table1 (Id int)
GO
INSERT INTO Table1
VALUES (1), (2), (3), (4), (5)
CREATE TABLE Table2 (Id int)
GO
INSERT INTO Table2
VALUES (1), (3), (5)
-- =ANY sees whether the scalar value is
-- equal to any from Table2
-- Eg. This checks:
-- (Table1:Id1 = Table2:Id1)
-- OR (Table1:Id1 = Table2:Id3)
-- OR (Table1:Id1 = Table2:Id5)
SELECT Id FROM Table1
WHERE Id =ANY (SELECT Id FROM Table2)
-- Finally, code returns 1, 3, and 5.
-- =ALL sees whether the scalar value is
-- equal with all values in Table2.
-- Eg. (Table1:Id1 = Table2:Id1)
-- AND (Table1:Id1 = Table2:Id3)
-- AND (Table1:Id1 = Table2:Id5)
SELECT Id FROM Table1
WHERE Id =ALL (SELECT Id FROM Table2)
-- Code returns nothing.
-- >=ANY sees whether the scalar value is
-- greater than any value in Table2
-- Eg. (Table1:Id1 > Table2:Id1)
-- OR (Table1:Id1 > Table2:Id3)
-- OR (Table1:Id1 > Table2:Id5)
-- Since the smallest one in Table2 is 1,
-- As long as the Table1:Id is >= 1
-- It is added to the resultset.
SELECT Id FROM Table1
WHERE Id >=ANY (SELECT Id FROM Table2)
-- Code returns 1, 2, 3, 4, and 5
-- >=ALL sees whether the scalar value is
-- greater than all in Table2
-- Eg. (Table1:Id1 > Table2:Id1)
-- AND (Table1:Id1 > Table2:Id3)
-- AND (Table1:Id1 > Table2:Id5)
-- Since the highest number in Table2 is 5,
-- As long as Table1:Id is >= 5,
-- It is added to the resultset.
SELECT Id FROM Table1
WHERE Id >=ALL (SELECT Id FROM Table2)
-- Code returns 5 only.
No comments:
Post a Comment