Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

use "left join" or "not exists" ?

 
   Database Help (Home) -> Programming RSS
Next:  Query between two tables and get differences  
Author Message
Stephen Ahn

External


Since: Mar 14, 2007
Posts: 6



(Msg. 1) Posted: Thu Nov 16, 2006 11:59 am
Post subject: use "left join" or "not exists" ?
Archived from groups: microsoft>public>sqlserver>programming (more info?)

Using SQL Server 2000,

Example :
==
create table jtest1 (id int primary key, col1 varchar(10))
create table jtest2 (id2 int primary key, col2 varchar(10))

insert jtest1 values (1, 'hello')
insert jtest1 values (2, 'two')

insert jtest2 values (1, 'world')
==

Now, I think the following two queries produce the same end result :

1) select j1.* from jtest1 j1 left join jtest2 on id = id2 where id2 is
null

2) select j1.* from jtest1 j1 where not exists(select * from jtest2 j2
where j1.id = j2.id2)

My preference in this case is number 2, since it "feels like" it should be
more efficient.
Is this the case ?

TIA,
Stephen

 >> Stay informed about: use ""left join"" or ""not exists"" ? 
Back to top
Login to vote
Tibor Karaszi

External


Since: Jan 29, 2004
Posts: 1559



(Msg. 2) Posted: Thu Nov 16, 2006 11:59 am
Post subject: Re: use "left join" or "not exists" ? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Most probably the EXIST will result in same execution plan as the left join. I prefer EXISTS, as it
tend to perform predictable and I think it is more intuitive.

--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/


"Stephen Ahn" <noaddress> wrote in message
> Using SQL Server 2000,
>
> Example :
> ==
> create table jtest1 (id int primary key, col1 varchar(10))
> create table jtest2 (id2 int primary key, col2 varchar(10))
>
> insert jtest1 values (1, 'hello')
> insert jtest1 values (2, 'two')
>
> insert jtest2 values (1, 'world')
> ==
>
> Now, I think the following two queries produce the same end result :
>
> 1) select j1.* from jtest1 j1 left join jtest2 on id = id2 where id2 is null
>
> 2) select j1.* from jtest1 j1 where not exists(select * from jtest2 j2 where j1.id = j2.id2)
>
> My preference in this case is number 2, since it "feels like" it should be more efficient.
> Is this the case ?
>
> TIA,
> Stephen
>
>

 >> Stay informed about: use ""left join"" or ""not exists"" ? 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
left join/inner join/outer join with repeat values - hi david, thanks for the reply. basically what i need this for is: there are two tables: table1 is a master table, table2 is a detail table. table1's primary_id is present in details_id of table2 table1 has only 1 record table2 could have many..

Left join is equal to inner join? - Hi All I try to calculate the result by using left join but it still returns the output same as using inner join. ************************************ Table1(T1) Status_id Score_id SumOfAmount Chief Excellent 1 Chief Fair 1 Chief ....

Left join - On a Sql Server 2005, suppose the query SELECT ...... FROM A LEFT JOIN B ON B.Col1 = A.Col2 LEFT JOIN C ON (C.Col3 = A.Col4 OR C.Col5 = B.Col6) If the left join on B results in 1 or more rows, then I get all the rows from C: the rows that apply to the....

LEFT JOIN question - Hi, I have table A and I create a left join to table B: SELECT * from tableA a LEFT JOIN tableB b ON a.id=b.id... and I get all the records from table A, that is 100 records for example. If I create join to table C: SELECT * from tableA a LEFT JOIN....

LEFT OUTER JOIN problem - Hello, I have 2 tables , tblTOSub , and tblTrScanSub with this schema , the 2 identities are the PKeys. CREATE TABLE [dbo].[tblTOSub]( [TOSubID] [int] IDENTITY(1,1) NOT NULL, [TOID] [int] NOT NULL, [ItemID] [int] NOT NULL, [Qty] [int] NOT NULL, ..
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]