Re: Not Exists joining 2 tables



On Nov 28, 5:46 am, Ed Murphy <emurph...@xxxxxxxxxxxx> wrote:
creedp...@xxxxxxxxx wrote:
Consider this alternative and let me know if I screw this one up,
after all, it's getting late, and I'm still at work :) .....

SELECT company.code, company.name, company.type,
contacts.fullname
FROM company Left JOIN
contacts ON company.code = contacts.code
AND (company.type in ('C', 'R')) and (contacts.fullname =
'Accounting')
WHERE contacts.code IS NULL
order by company.code

I think this would work (except in the unlikely case that
contacts.code is nullable, in which case it might return
some data that it shouldn't). But NOT EXISTS has the strong
advantage of letting you say what you mean.

The last time I tried this (on a fairly complex query as well) on SQL
2005, the execution plan of using a NOT EXISTS and LEFT OUTER JOIN to
filter rows was identical. Could it be that the query optimiser
actually understands what we are trying to achieve and derives the
best method to do so regardless of syntax these days? More likely it
was simply a quirk of the query / tables / indexing I guess...

J
.



Relevant Pages

  • Re: Upgrade Access Query - Dynamic Join
    ... AMB, thanks for the response and apologies for posting insufficient details: ... The original query which selects records based on date is then used within ... >> The current access query first derives an Order Number from a reference ...
    (microsoft.public.sqlserver.programming)
  • Re: Upgrade Access Query - Dynamic Join
    ... AMB, thanks again for your assistance however I am not sure I have expressed ... Could I create a view that derives the order number using a UDF then link ... > union all ... >> The original query which selects records based on date is then used ...
    (microsoft.public.sqlserver.programming)
  • If query returns nothing
    ... I have a popup form that has its module set to true, ... form is derived from a query very simple so far. ... is a function that derives its info from a written Public function in a ...
    (microsoft.public.access.queries)
  • Finding win32_DiskDrive
    ... do a query on Win32_IDEControllerDevice followed by GetObject to get ... "The Win32_DiskDrive class is derived from CIM_DiskDrive which derives ... assume some new query needs to be done ... ...
    (microsoft.public.win32.programmer.wmi)
  • Re: SELECT TOP query - in datatables
    ... SELECT TOP query in C# to filter rows from my datatable? ... (not from my SQL server database). ...
    (microsoft.public.dotnet.languages.csharp)