-- Database by Doug -- Douglas Kline -- 10/19/2018 -- ANY and ALL -- comparing a value to a column of values -- (aka acomparing a scalar to a vector of scalars) -- consider this setup for testing -- if the WHERE condition is true or false SELECT 1 AS [isTrue?] WHERE 10 = 10 -- testing if this is true or false SELECT 1 AS [isTrue?] WHERE 10 <> 10 -- testing if this is true or false -- we get a record if it is true, otherwise no record -- also, see how I can create a literal table SELECT tempField FROM (VALUES(11),(12),(7)) tempTable(tempField) -- note that this creates a single column of values -- which could be used in something like IN -- for example SELECT 1 WHERE 12 IN ( SELECT tempField FROM (VALUES(11),(12),(7)) tempTable(tempField)) -- I could rephrase this as: SELECT 1 WHERE 12 = ANY ( SELECT tempField FROM (VALUES(11),(12),(7)) tempTable(tempField)) -- back to the first example: SELECT 1 AS [isTrue?] WHERE 10 > 11 -- I'm comparing a single scalar value, 10, with another -- single scalar value, 11 -- now consider if I want to compare the value 10 -- to multiple other values SELECT 1 WHERE 10 > 11 OR 10 > 12 OR 10 > 7 -- this is three logical expressions -- combined with OR into the whole logical expression -- so if ANY of them are true -- the whole logical expression is true -- now let's say that the values 11, 12, 7 are in a column -- since we're in an RDBMS, they are *likely* to be in a column -- I can rephrase this with ANY like this: SELECT 1 WHERE 10 > ANY (SELECT tempfield FROM (VALUES (11), (12), (7) ) AS tempTable(tempfield)) -- this is exactly equivalent to: SELECT 1 WHERE 10 > 11 OR 10 > 12 OR 10 > 7 -- back to the first example again... -- back to the first example: SELECT 1 AS [isTrue?] WHERE 10 > 11 -- let's say I want to do multiple comparisons again -- but AND them together like this: SELECT 1 WHERE 10 > 11 -- logical expression 1 AND 10 > 12 -- logical expression 2 AND 10 > 7 -- logical expression 3 -- this is three logical expressions -- combined with ANDs into the whole WHERE -- logical expression -- so all three expressions must be true -- for the WHERE clause to be true -- I can make it true with different values: SELECT 1 WHERE 10 > 9 -- logical expression 1 AND 10 > 8 -- logical expression 2 AND 10 > 7 -- logical expression 3 -- I can rephrase this with ALL -- with the values in a column SELECT 1 WHERE 10 > ALL (SELECT tempfield FROM (VALUES (11), (12), (7) ) AS tempTable(tempfield)) -- and get it to be true with a change to the values SELECT 1 WHERE 10 > ALL (SELECT tempfield FROM (VALUES (9), (8), (7) ) AS tempTable(tempfield)) -- so you can think of ANY and ALL -- as comparison operator modifiers -- comparison operators usually take a scalar value on each side -- scalarscalar -- 3 < 7 -- 3 > 7 -- 3 = 7 -- 3 != 7 -- 3 <> 7 -- 3 <= 7 -- 3 >= 7 -- ANY and ALL take a scalar on the left, and a column on the right -- scalar ANY column -- scalar ALL column -- 3 < ANY ((1),(2),(3)) -- 3 >= ALL ((1),(2),(3)) -- and the column is generally created with a SELECT statement -- scalar ANY (SELECT FROM...) -- scalar ALL (SELECT FROM...) -- so here's a more concrete example using the Northwind database Orders table -- suppose we want to know a list of customers who paid more than -- $200 on freight on an order in 1996? -- in other words $200 < ANY(orders in 1996) SELECT CompanyName FROM Customers WHERE $200 < ANY (SELECT freight FROM Orders WHERE Orders.CustomerID = Customers.CustomerID AND YEAR(Orderdate) = 1996) ORDER BY CompanyName -- notice that this is a correlated subquery -- it refers to the outer query (Customers.customerID) -- also notice that this could be rephrased -- with a JOIN DISTINCT SELECT DISTINCT Customers.CompanyName FROM Customers JOIN Orders ON Customers.CustomerID = Orders.CustomerID WHERE Orders.Freight > $200 AND YEAR(Orders.OrderDate) = 1996 ORDER BY CompanyName -- is there a difference? why one and not the other -- it depends -- depending on your situation, one SQL phrasing might -- be clearer than another -- depending on your data model, volume, statistics, indexes, etc. -- one might be faster than the other -- in other words, you might get different query plans -- in general, I recommend writing your SQL in the clearest -- manner possible -- then carefully rephrasing to a better performing, but -- perhaps less understandable, form if there is a performance issue -- in summary, -- ANY and ALL modify comparison operators -- they succinctly compare a single scalar to -- a column of scalars -- thanks for watching! -- Database by Doug -- Douglas Kline -- 10/19/2018 -- ANY and ALL
Saturday, October 20, 2018
Using ANY and ALL in SQL
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment