If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > outer join shortcut?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-20-07, 15:11
wey97 wey97 is offline
Registered User
 
Join Date: Mar 2003
Posts: 130
Question outer join shortcut?

Is this a shortcut for an outer join and if so, why can't I find any info on it?

Code:
select id, name
from table1, table2
where id *= id_tbl2
instead of

Code:
select tb1.id, tb1.name
from table1 as tb1
outer join table2 as tb2
where tb1.id = tb2.id_tbl2

and what happens in the first example if you try to join more tables?

Last edited by wey97; 11-20-07 at 15:14.
Reply With Quote
  #2 (permalink)  
Old 11-20-07, 15:25
amthomas amthomas is offline
Registered User
 
Join Date: May 2005
Location: San Antonio, Texas
Posts: 134
http://en.wikipedia.org/wiki/Join_(SQL)

the first is an inner join.

the 2nd would show all result (left outer join) for table1 and only combine the result from table2 that matched. Of course I could be wrong on this if it is something MS SQL specific. Does it allow for just 'outer join'?
__________________
Vi veri veniversum vivus vici
By the power of truth, I, a living man, have conquered the universe
Reply With Quote
  #3 (permalink)  
Old 11-20-07, 15:31
wey97 wey97 is offline
Registered User
 
Join Date: Mar 2003
Posts: 130
Quote:
Originally Posted by amthomas
http://en.wikipedia.org/wiki/Join_(SQL)

the first is an inner join.

the 2nd would show all result (left outer join) for table1 and only combine the result from table2 that matched. Of course I could be wrong on this if it is something MS SQL specific. Does it allow for just 'outer join'?
according to sql server, the first is an outer join.

you can verify that by attempting

Code:
select id
from table1
where id *= 1
and the error message is

Code:
Msg 150, Level 15, State 1, Line 5
Both terms of an outer join must contain columns.
Reply With Quote
  #4 (permalink)  
Old 11-20-07, 15:35
tomh53 tomh53 is offline
9th inning DBA
 
Join Date: Jan 2004
Location: In a large office with bad lighting
Posts: 1,036
*= is indeed a left outer join, just as =* is a right outer join. It is a T_SQL extension and was valid up thru S2K. It is no longer available in S2K5.

It is not in the ANSI Standard, so if you want your code to run in SQL Server later than 2000, do not use it!
__________________

-- This is all just a Figment of my Imagination --
Reply With Quote
  #5 (permalink)  
Old 11-20-07, 15:38
amthomas amthomas is offline
Registered User
 
Join Date: May 2005
Location: San Antonio, Texas
Posts: 134
oops, I am sorry. that was something ms sql specific. I should not have answered that. I just didn't see the little * :/

sorry about that. well.. at least I learned something
__________________
Vi veri veniversum vivus vici
By the power of truth, I, a living man, have conquered the universe
Reply With Quote
  #6 (permalink)  
Old 11-20-07, 15:41
wey97 wey97 is offline
Registered User
 
Join Date: Mar 2003
Posts: 130
Quote:
Originally Posted by tomh53
*= is indeed a left outer join, just as =* is a right outer join. It is a T_SQL extension and was valid up thru S2K. It is no longer available in S2K5.

It is not in the ANSI Standard, so if you want your code to run in SQL Server later than 2000, do not use it!
Code I inherited.

I don't think the db admin knew exactly what he was doing, other than taking a shortcut to write less code.
Reply With Quote
  #7 (permalink)  
Old 11-20-07, 16:05
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,726
Its not a shortcut. Its an "old cut". It used to be the standard coding syntax, so far back in the mists of time that only wise men such as Pat Phelan know the origins.
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
Reply With Quote
  #8 (permalink)  
Old 10-07-11, 17:13
sdtacoma sdtacoma is offline
Registered User
 
Join Date: Oct 2011
Posts: 2
Hello,

I realize this post is a little old but...

I have been using the 'old cut' for a long time now without issue. I have recently been trying to convert to the 'ANSI Standard' with mixed results. I am sure it is user-error but I often have problems pulling the correct rows with the 'ANSI Standard' and can get exactly what I need with the 'old cut' way.

Maybe somebody can see what I am doing wrong here. I have tried changing my joins to left outer/inner with no luck.

'Old cut' way, returns the needed 589 records:
select amt.family_id, u.user_type_id, ui.lastname + ', ' + ui.firstname as StuName, sf.sf_file_name
from advisor_student_matches amt, user_info ui, users u, student_files sf
where amt.edcom_id = ui.edcom_id
and ui.edcom_id = u.edcom_id
and ui.edcom_id *= sf.sf_edcom_id
and amt.delete_date is null
and sf.sf_file_type = 'pdf'
order by amt.family_id, u.user_type_id


'ANSI Standard' way, returns 142 records:
select amt.family_id, u.user_type_id, ui.lastname + ', ' + ui.firstname as StuName, sf.sf_file_name
from advisor_student_matches amt
left join user_info ui on amt.edcom_id = ui.edcom_id
left join users u on ui.edcom_id = u.edcom_id
left outer join student_files sf on ui.edcom_id = sf.sf_edcom_id
where amt.delete_date is null
and sf.sf_file_type = 'pdf'
order by amt.family_id, u.user_type_id


Any help would be appreciated.

Thanks
Reply With Quote
  #9 (permalink)  
Old 10-07-11, 22:55
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
change this --
Code:
left outer join student_files sf on ui.edcom_id = sf.sf_edcom_id 
where amt.delete_date is null
and sf.sf_file_type = 'pdf'
to this --
Code:
left outer join student_files sf on ui.edcom_id = sf.sf_edcom_id 
and sf.sf_file_type = 'pdf'
where amt.delete_date is null
you will be surprised at the difference in results


__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #10 (permalink)  
Old 10-10-11, 06:14
jassi.singh jassi.singh is offline
Registered User
 
Join Date: Sep 2011
Posts: 75
Hello,

Its better to use outer join instead of shortcut for performance gain
Reply With Quote
  #11 (permalink)  
Old 10-10-11, 11:28
cindyaz cindyaz is offline
Registered User
 
Join Date: Sep 2011
Posts: 31
Quote:
Originally Posted by jassi.singh View Post
Hello,

Its better to use outer join instead of shortcut for performance gain
Though join shortcuts have been depracated now, can you elaborate how will this help in performance?
Reply With Quote
  #12 (permalink)  
Old 10-10-11, 14:02
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,726
When you include a column from an outer-joined dataset in the WHERE clause, it effectively turns it into an inner join. So, as Rudy showed, you need to include such filters in the outer join itself:
Code:
select	amt.family_id,
	u.user_type_id,
	ui.lastname + ', ' + ui.firstname as StuName,
	sf.sf_file_name
from	advisor_student_matches amt
	left outer join user_info ui on amt.edcom_id = ui.edcom_id
	left outer join users u on ui.edcom_id = u.edcom_id
	left outer join student_files sf
		on ui.edcom_id = sf.sf_edcom_id
		and sf.sf_file_type = 'pdf'
where	amt.delete_date is null
order by amt.family_id,
	u.user_type_id
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
Reply With Quote
  #13 (permalink)  
Old 10-11-11, 04:31
jassi.singh jassi.singh is offline
Registered User
 
Join Date: Sep 2011
Posts: 75
Quote:
Originally Posted by cindyaz View Post
Though join shortcuts have been depracated now, can you elaborate how will this help in performance?
YOu can write both the quries in ssms and then clcik on estimated execution plan button to see the total cost and resource usage
Reply With Quote
  #14 (permalink)  
Old 10-13-11, 15:09
sdtacoma sdtacoma is offline
Registered User
 
Join Date: Oct 2011
Posts: 2
Thumbs up

Sorry, I didn't mean to abandon my posting. I got tasked onto something else here at work.

Your suggestions worked like a charm; I get the records I need. Thank you for the help and teaching me something new.
Reply With Quote
  #15 (permalink)  
Old 10-13-11, 15:44
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by sdtacoma View Post
Your suggestions worked like a charm;
you are of course referring to post #9

thanks very much
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On