[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

SQL Server SQL syntax =* and right joins

Posted on 2016-07-25
11
Medium Priority
?
36 Views
Last Modified: 2016-07-26
Hi Folks.
I am struggling to understand the join conversion process, and how to nest right joins.

do the following have identical results?

thank you.

from APOPEN O,APPayments P, APNewPayables N  WHERE O.ApplytoId =* P.ApplytoId and N.voucherid =* P.applytoid

from (APOPEN RIGHT JOIN APPayments ON AOPEN.ApplytoId = APPayment.ApplytoId) RIGHT JOIN APNewPayables ON APNewPayables.voucherid = APPayment.applytoid
0
Comment
Question by:COACHMAN99
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
11 Comments
 
LVL 27

Expert Comment

by:Chris Luttrell
ID: 41728700
Close, SQL Server uses the ANSI JOIN syntax like you have in the second line. The =* is signifying RIGHT OUTER JOIN.  You don't have to have the Parenthesis either.
from APOPEN O RIGHT OUTER JOIN APPayments P ON O.ApplytoId = P.ApplytoId RIGHT OUTER JOIN APNewPayables N ON  N.voucherid = P.applytoid 

Open in new window

0
 
LVL 7

Author Comment

by:COACHMAN99
ID: 41728729
Hi Chris
your text is jumping around so I cant read it.
Is right outer not the same as right? and
Do my two statements generate identical results?
thanks
0
 
LVL 7

Author Comment

by:COACHMAN99
ID: 41728732
and can one not just use the table-name instead of the alias?
My objective is to use the second text/syntax in place of the first. I just need to know if the results (as I have phrased them) would be identical .

thanks
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 27

Accepted Solution

by:
Chris Luttrell earned 2000 total points
ID: 41728735
Yes to both of you questions.  https://msdn.microsoft.com/en-us/library/ms177634(v=sql.130).aspx is the link to the T-SQL rules on FROM.
So OUTER is "OPTIONAL" but I am a fan of following the standards because it will transfer between databases better and you don't have to worry about old code breaking because they suddenly start enforcing the ANSI standards.  I also like table aliases over full table names, just a preference there.
0
 
LVL 27

Expert Comment

by:Chris Luttrell
ID: 41728738
I should know about old code because I have been working with various database versions and vendors for 30 years now and have plenty of my own out there. :-)
0
 
LVL 7

Author Comment

by:COACHMAN99
ID: 41728739
Excellent thanks! so my syntax is identical? I was concerned that the second RIGHT join was the wrong way around.
cheers.
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 41729007
There is an unwritten convention: avoid right joins

Please don't get me wrong here. You are free to use any join you want at any time and the standards do allow you to do it. But many of us who write and maintain mountains of SQL prefer to avoid right joins completely making query logic much simpler to maintain.

Your query could be reversed in direction
FROM APNewPayables N
LEFT JOIN APPayments P ON  N.voucherid = P.applytoid 
LEFT JOIN APOPEN O ON P.ApplytoId = O.ApplytoId 

Open in new window

1
 
LVL 7

Author Comment

by:COACHMAN99
ID: 41729658
Thanks Paul, makes sense.
So there are no efficiency issues, just 'trickier' to understand?
0
 
LVL 27

Expert Comment

by:Chris Luttrell
ID: 41729662
Good point Paul.  I agree, I just did not want to push the issue.  Yes, they work the same, just trickier to think about the logic flow.
0
 
LVL 7

Author Comment

by:COACHMAN99
ID: 41729676
Great to have consensus  :-)
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 41730187
Right joins are indeed much trickier to follow, and they alter the precedence of tables, and in large multi-table from clauses it can be very hard to understand the author's intentions if precedence flips about like a trout on a river bank.

There should not be performance issues as it is actually the optimizer that determines the actual execution path.
1

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question