473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Confusion over "ANY" keyword

I am studying for the MSCE/MCDBA exam 70-229. In the book I am using
("MCSA/MCSE/MCDBA Self-Paced Training Kit: Microsoft SQL Server 2000
Database Design and Implementation Exam 70-229, Second Edition") I am
looking at the section on the ANY/ALL keyword.

<QUOTE>
USE Pubs
SELECT Title
FROM Titles
WHERE Advance > ANY
(
SELECT Advance
FROM Publishers INNER JOIN Titles
ON Titles.Pub_id = Publishers.Pub_id
AND Pub_name = 'Algodata Infosystems')

This statement finds the titles that received an advance larger than
the minimum advance amount paid by Algodata Infosystems (which, in this
case, is $5,000). The WHERE clause in the outer SELECT statement
contains a subquery that uses a join to retrieve advance amounts for
Algodata Infosystems. The minimum advance
amount is then used to determine which titles to retrieve from the
Titles table.
</QUOTE>

I don't understand why this references the "minimum advance". If you
run the subquery on its own, it returns the following values:

5000.0000
5000.0000
5000.0000
7000.0000
8000.0000
NULL
From my limited understanding, the "ANY" keyword applies to at least

one value, but which one? How is this determined?

Any help gratefully received.

Edward
--
The reading group's reading group:
http://www.bookgroup.org.uk

Jul 23 '05 #1
4 5645
The value to the left of the comparison operator (Advance) is compared
to each of the values returned by the subquery. If it matches at least
one of those values then the result is True. Think of it as a series of
comparions linked by OR. For example your query could be expanded to
the following, which is logically equivalent:

SELECT Title
FROM Titles
WHERE
(advance > 5000
OR advance > 5000
OR advance > 5000
OR advance > 7000
OR advance > 8000
OR advance > NULL)

--
David Portas
SQL Server MVP
--

Jul 23 '05 #2

David Portas wrote:
The value to the left of the comparison operator (Advance) is compared to each of the values returned by the subquery. If it matches at least one of those values then the result is True. Think of it as a series of comparions linked by OR. For example your query could be expanded to
the following, which is logically equivalent:

SELECT Title
FROM Titles
WHERE
(advance > 5000
OR advance > 5000
OR advance > 5000
OR advance > 7000
OR advance > 8000
OR advance > NULL)


Thanks, David, that's extremely helpful. Am I right in thinking that
if I substitute the "ALL" keyword for the "ANY" keyword in the original
query, the expansion as above would be:

SELECT Title
FROM Titles
WHERE
(advance > 5000
AND advance > 5000
AND advance > 5000
AND advance > 7000
AND advance > 8000
AND advance > NULL)

If so, I'm back with the programme.

Edward
--
The reading group's reading group:
http://www.bookgroup.org.uk

Jul 23 '05 #3
That's correct. I should also say that there is a subtle catch that if
the subquery is empty then ALL always returns True whereas the ANY
returns False. Try:

SELECT Title
FROM Titles
WHERE Advance > ANY
(
SELECT Advance
FROM Publishers INNER JOIN Titles
ON Titles.Pub_id = Publishers.Pub_id
AND 1=0)

SELECT Title
FROM Titles
WHERE Advance > ALL
(
SELECT Advance
FROM Publishers INNER JOIN Titles
ON Titles.Pub_id = Publishers.Pub_id
AND 1=0)

--
David Portas
SQL Server MVP
--

Jul 23 '05 #4

David Portas wrote:
That's correct. I should also say that there is a subtle catch that if the subquery is empty then ALL always returns True whereas the ANY
returns False. Try:

SELECT Title
FROM Titles
WHERE Advance > ANY
(
SELECT Advance
FROM Publishers INNER JOIN Titles
ON Titles.Pub_id = Publishers.Pub_id
AND 1=0)

SELECT Title
FROM Titles
WHERE Advance > ALL
(
SELECT Advance
FROM Publishers INNER JOIN Titles
ON Titles.Pub_id = Publishers.Pub_id
AND 1=0)


Interesting, but I've been using SQL without either ANY or ALL for
about ten years, so I guess I'll carry on without. However, the book
I'm studying has alerted me to CUBE and ROLLUP which I can see some
serious uses for.

Edward
--
The reading group's reading group:
http://www.bookgroup.org.uk

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Greg Stark | last post by:
I find myself wishing I had a syntax "LIKE ANY (array)". I don't see much value in the = ANY, = ALL, <> ANY, <> ALL syntax since they're equivalent more or less to IN and NOT IN. But it could be...
2
by: AA Arens | last post by:
When I place a record serch utility (vos the button placement wizzrd), I discovered that the Match setting is "Whole Field" by default. How to make "Any Part Of Field" default?
60
by: K. G. Suarez | last post by:
Hello everyone. I am new to programming and my uncle gave me a copy of "C For Dummies 2nd Edition". I am up to chapter 9 right now. He probably saw me struggling with "The C Programming...
2
by: Lance Geeck | last post by:
I have many items that I lifted off from Microsoft's website several years ago. These samples were in VB6. I now want to convert an application to VB.NET. I am getting an error that says "As Any...
2
by: Lau Lei Cheong | last post by:
Hello, Actually, I'm wondering if there's anything of the sort avaliable in the wild - a developer oriented W3C browser. It's kinda W3C's online validation service, just that it runs on locahost...
6
by: Lord0 | last post by:
Hi there, How do I define in a schema that an element (<element>) may have any content i.e. text, other elements, partial elements, angle brackets etc? So all of the following would be valid:...
3
by: geebanga88 | last post by:
Hi i am using oracle sql developer and am making a sub query for a question. The question states: "Find Name of patients who have been treated by Dr Brian or who have had an Extended...
2
by: babakandme | last post by:
Hi everybody:D I've a string that contains the name of a class. Some members told that I can use """Stringizing Operator (#)""", but the problem is here, that I have the string, & I want...
6
by: Deano | last post by:
Every once in a while I modify one particular form (could be anything from adding code to tweaking properties as far as I can see) and upon loading I get; "You can't assign a value to this...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.