473,396 Members | 1,809 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,396 software developers and data experts.

The EXISTS operator in A97 SQL - I cannot find documentation on it in A97 HELP

MLH
Mr Leigh Purvis gave me a very clever piece of SQL to accomplish what
is probably an uncommon objective. In it, he uses the EXISTS operator.
I can find no documentation on it in A97 HELP. I would like to read
more about this useful SQL operator. Suggestions?

SELECT CustID, OutType, EXISTS (SELECT CustID FROM tblCorrespondence
AS tblC
WHERE tblC.CustID = tblCorrespondence.CustID AND tblC.OutType = "01")
AS
LogicalField FROM tblCorrespondence WHERE tblCorrespondence.OutType
=
"05"

Nov 13 '05 #1
15 1884
MLH wrote:
Mr Leigh Purvis gave me a very clever piece of SQL to accomplish what
is probably an uncommon objective. In it, he uses the EXISTS operator.
I can find no documentation on it in A97 HELP. I would like to read
more about this useful SQL operator. Suggestions?

SELECT CustID, OutType, EXISTS (SELECT CustID FROM tblCorrespondence
AS tblC
WHERE tblC.CustID = tblCorrespondence.CustID AND tblC.OutType = "01")
AS
LogicalField FROM tblCorrespondence WHERE tblCorrespondence.OutType
=
"05"


http://msdn.microsoft.com/library/de...lce_exists.asp

SQL Server does a great job of optimising this, IME Access does not, I
see most people use exists(select * from...), which looks resource
hungry but SQL Server doesn't actually select all columns in this
instance. Not sure what Access does though.

You'll have to test for yourself but you might find using DCount() (or
my tCount()) to be more efficient against Access tables, YMMV.

Nov 13 '05 #2
What is the "uncommon objective"?

Nov 13 '05 #3
Execution Plans: Which is more efficient?

--- Query15 ---

**** (inserted by poster) ****
SELECT *
FROM Table1
WHERE EXISTS(SELECT NUMBER FROM Table2 WHERE Table1.ID=Table2.Number);
**** (end insert) ****

- Inputs to Query -
Table 'Table2'
Table 'Table1'
- End inputs to Query -

01) Sort table 'Table2'
02) Semi Join table 'Table1' to result of '01)'
using temporary index
join expression "Table1.ID=Table2.Number"

--- Query16 ---

**** (inserted by poster) ****
SELECT *
FROM Table1 INNER JOIN Table2 ON Table1.ID = Table2.Number;
**** (end insert) ****

- Inputs to Query -
Table 'Table1'
Using index 'ID'
Having Indexes:
ID 10 entries, 1 page, 10 values
which has 1 column, fixed, clustered and/or counter
Table 'Table2'
- End inputs to Query -

01) Inner Join table 'Table2' to table 'Table1'
using index 'Table1!ID'
join expression "Table2.Number=Table1.ID"

----
I never use EXISTS, preferring JOINS.

Nov 13 '05 #4
MLH
Well, I was probably wrong in saying that. I thought it more
common for a query to extract say 3 rows from a 10 row table
displaying data from ONLY the 3 of the 10 records than to
extract the same 3 rows, but mention (with an extra field)
data from say another 4th and 5th row of the table being
examined.

What is the "uncommon objective"?


Nov 13 '05 #5
Bri

MLH wrote:
Mr Leigh Purvis gave me a very clever piece of SQL to accomplish what
is probably an uncommon objective. In it, he uses the EXISTS operator.
I can find no documentation on it in A97 HELP. I would like to read
more about this useful SQL operator. Suggestions?


It is in the help for 'Subqueries - SQL Subqueries' in the Index or look
for EXISTS in Find to get to the same place.

--
Bri

Nov 13 '05 #6
MLH
FOUND IT! Thank-you very much, Bri.

It is in the help for 'Subqueries - SQL Subqueries' in the Index or look
for EXISTS in Find to get to the same place.

A subquery is a SELECT statement nested inside a SELECT,
SELECT...INTO, INSERT...INTO, DELETE, or UPDATE statement or inside
another subquery.

Syntax

You can use three forms of syntax to create a subquery:

comparison [ANY | ALL | SOME] (sqlstatement)
expression [NOT] IN (sqlstatement)
[NOT] EXISTS (sqlstatement)

A subquery has these parts:

Part Description
comparison An expression and a comparison operator that compares
the expression with the results of the subquery.
expression An expression for which the result set of the subquery
is searched.
sqlstatement A SELECT statement, following the same format and
rules as any other SELECT statement. It must be enclosed in
parentheses.
Nov 13 '05 #7
"lylefair" <ly******@yahoo.ca> wrote in
news:11*********************@g44g2000cwa.googlegro ups.com:
Execution Plans: Which is more efficient?

--- Query15 ---

**** (inserted by poster) ****
SELECT *
FROM Table1
WHERE EXISTS(SELECT NUMBER FROM Table2 WHERE
Table1.ID=Table2.Number); **** (end insert) ****

- Inputs to Query -
Table 'Table2'
Table 'Table1'
- End inputs to Query -

01) Sort table 'Table2'
02) Semi Join table 'Table1' to result of '01)'
using temporary index
join expression "Table1.ID=Table2.Number"

--- Query16 ---

**** (inserted by poster) ****
SELECT *
FROM Table1 INNER JOIN Table2 ON Table1.ID = Table2.Number;
**** (end insert) ****

- Inputs to Query -
Table 'Table1'
Using index 'ID'
Having Indexes:
ID 10 entries, 1 page, 10 values
which has 1 column, fixed, clustered and/or counter
Table 'Table2'
- End inputs to Query -

01) Inner Join table 'Table2' to table 'Table1'
using index 'Table1!ID'
join expression "Table2.Number=Table1.ID"

----
I never use EXISTS, preferring JOINS.

Query16, which uses the join, will return as many rows as there
are in table 2. Repeat your plan with SELECT DISTINCT and you
may not find it as speedy.
--
Bob Quintal

PA is y I've altered my email address.
Nov 13 '05 #8
No.
Query16 returns only one row with all the fields from both Table1 and
Table2. This row has both Table1.ID and Table2.Number = 9. This is the
only match that exists.

Query15 also returns one row, but only the fields of Table1 are
included.

Nov 13 '05 #9
Reading the rest of your post: of course,this is not MY plan. It is
JET's compilation plan and manifests itself in showplan.out after
inserting this key in the registry:

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Jet\4.0\Engi nes\Debug]
"JETSHOWPLAN"="ON"

And as speedy as what? I didn't say it was speedy and I don't know if
it's speedy. We were discussing whether or not JET optimizes The EXISTS
operator.
After looking at the compilation plan my guess is ... probably not.

Nov 13 '05 #10
Whatever that means.

Nov 13 '05 #11
"lylefair" <ly******@yahoo.ca> wrote in
news:11**********************@f14g2000cwb.googlegr oups.com:
No.
Query16 returns only one row with all the fields from both
Table1 and Table2. This row has both Table1.ID and
Table2.Number = 9. This is the only match that exists.

Query15 also returns one row, but only the fields of Table1
are included.

Right. In the real world, there is often a need to return only
the rows from one table which meet a criterium which exists in
one or more rows of a second table. If there are more than one
row, a join will return duplicate values. Using EXISTS will
result in the query returning rows from table 1 only the number
of rows in table one which match the criterium.

Using the DISTINCT keyword in the query will also return only
the rows meeting the criterium, but that will be less efficient
as the database engine must create a scratch table then compress
that to the final result.

--
Bob Quintal

PA is y I've altered my email address.
Nov 13 '05 #12
I think you misread what I said, for the second or third time in this
thread.

If you want to make some point about exists and joins then by all means
do it, but I would appreciate it if you did independently of the point
I am trying to make, which is that Exists in JET may be inefficient.

I don't want to do anything with these queries. I created them only to
see their JET compilation plan.

If you want to discuss you point then I ask:

Can you post in full a query string where EXISTS will do something more
effectively or efficiently than a JOIN? (assuming JET 4.0).

Nov 13 '05 #13
"lylefair" <ly******@yahoo.ca> wrote in
news:11**********************@g49g2000cwa.googlegr oups.com:
I think you misread what I said, for the second or third time
in this thread.

If you want to make some point about exists and joins then by
all means do it, but I would appreciate it if you did
independently of the point I am trying to make, which is that
Exists in JET may be inefficient.

I don't want to do anything with these queries. I created them
only to see their JET compilation plan.

If you want to discuss you point then I ask:

Can you post in full a query string where EXISTS will do
something more effectively or efficiently than a JOIN?
(assuming JET 4.0).

I'll have to assume Jet 3.51, went back to Access '97 because of
crap in '2000.

select invoice_no from invoice where exists (select * from
invoice_line where shipped_date is null and invoice.invoice no=
invoice_line.invoice_no);

vs:

select distinct invoice_no from invoice , invoice_line inner
join invoice.invoice no = invoice_line.invoice_no where
invoice_line where shipped_date is null;


--
Bob Quintal

PA is y I've altered my email address.
Nov 13 '05 #14
"lylefair" <ly******@yahoo.ca> wrote in
news:11**********************@g43g2000cwa.googlegr oups.com:
Reading the rest of your post: of course,this is not MY plan. It
is JET's compilation plan and manifests itself in showplan.out
after inserting this key in the registry:

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Jet\4.0\Engi nes\Debug]
"JETSHOWPLAN"="ON"

And as speedy as what? I didn't say it was speedy and I don't know
if it's speedy. We were discussing whether or not JET optimizes
The EXISTS operator.
After looking at the compilation plan my guess is ... probably
not.


ShowPlan never shows subquery optimizations -- it says somewhere in
the ShowPlan output, I believe, that subquery optimization is not
shown, that MS never got around to implementing it.

That is, if I'm remembering correctly.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #15
Yes, I think you are right.

Nov 13 '05 #16

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

Similar topics

2
by: Newbie | last post by:
Hi, Could someone please tell how MINUS operator works for comparing and giving non-matching records in Table1? Does MINUS operator compares all records of Table1 with all records of Table2 to...
16
by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class,...
34
by: Pmb | last post by:
I've been working on creating a Complex class for my own learning purpose (learn through doing etc.). I'm once again puzzled about something. I can't figure out how to overload the assignment...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
2
by: Javier Estrada | last post by:
1. For types smaller than int, when I compile: class MyClass { static void Main(string args) { x = 10; y = -x; }
6
by: Chad Crowder | last post by:
Getting the following error on my production server whether the file exists or not: "System.IO.IOException: Cannot create a file when that file already exists." Here's the code generating the...
10
by: Geoff Jones | last post by:
Hi I'm trying to drop a table by using: Dim cmd As New OleDbCommand("DROP TABLE IF EXISTS books", myconnection) cmd.ExecuteNonQuery() but I get a syntax error: "Syntax error in DROP TABLE...
19
by: scroopy | last post by:
Is it impossible in C++ to create an assignment operator for classes with const data? I want to do something like this class MyClass { const int m_iValue; public: MyClass(int...
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.