Right join vs left join, which table is left vs right? - sql

Given
select *
from a
left join b
on a.id = b.id
is table a left and table b right?
Would that be equivalent to
Select *
from a
right join b
on b.id = a.id
because I switched left and right while flipping the ON clause? Or is a still left because it came first and b is right because it's the thing we're joining?
Thank you.

No. "left" and "right" refer to the ordering of the tables in the FROM clause. So these are equivalent:
select *
from a left join
b
on a.id = b.id
select *
from b right join
a
on a.id = b.id
These two on clauses do exactly the same thing:
on a.id = b.id
on b.id = a.id
They do not affect the results at all.

Related

Oracle Sql left join return null value on condition.

Hi I have a left join like
Select * from tables a
Left join tableB b on b.id = a.id and b.name ='avc';
This query also return record were b.name is null..
Could someone plz explain the reason.
You need to use an INNER JOIN to avoid nulls.
SELECT * FROM tables a
INNER JOIN tableB b
ON b.id = a.id and b.name ='avc';
Check this for more information about joins.

How to write a transitive sql join in DB2?

I want something like the following.
SELECT fewCols, aColFromNewTbl FROM TABLE_A AS A
LEFT OUTER JOIN TABLE_B AS B ON A.ID = B.ID
LEFT OUTER JOIN TABLE_C AS C ON A.ID = C.ID
INNER JOIN A_NEW_TABLE AS NEWTBL ON NEWTBL.ID = B.ID;
Somehow I'm not able to achieve this functionality. Actually above query is suppose to join A with NEWTBL, but I'm joining it with B, which is already joined with A. For my results I want them to come exclusively from the join of NEWTBL and B. I don't know how I can get desired results?
Probably you need this:
SELECT fewCols, aColFromNewTbl
FROM TABLE_A AS A
LEFT OUTER JOIN TABLE_B AS B
INNER JOIN A_NEW_TABLE AS NEWTBL
ON NEWTBL.ID = B.ID
ON A.ID = B.ID
LEFT OUTER JOIN TABLE_C AS C
ON A.ID = C.ID;

Right join with a where clause

I found a query like this in code:
SELECT *
FROM a
RIGHT JOIN b ON a.id = b.id
WHERE a.id = b.id
Is it basically the same as an inner join on a.id = b.id?
Yes, this is basically the same as an inner join.
The where clause will fail when there are no matches, because the value a.id will be NULL.

Joining many tables together using key from first table for all joins?

I would like to do something like this but can't get it to work:
SELECT A.*,B.*,C.* FROM tableA A LEFT JOIN
B ON A.ID = B.ID
C ON A.ID = C.ID
I.e. I need to use a field from the first table for all joins as tableB and tableC don't
has any fields to link them together.
Can this be done?
you missed a join !
SELECT A.*,B.*,C.* FROM tableA A
LEFT JOIN
B ON A.ID = B.ID
left join
C ON A.ID = C.ID
Try this one -
SELECT *
FROM dbo.tableA A
LEFT JOIN dbo.B ON A.ID = B.ID
LEFT JOIN dbo.C ON A.ID = C.ID

*= in Sybase SQL

I'm maintaining some code that uses a *= operator in a query to a Sybase database and I can't find documentation on it. Does anyone know what *= does? I assume that it is some sort of a join.
select * from a, b where a.id *= b.id
I can't figure out how this is different from:
select * from a, b where a.id = b.id
From http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.dc34982_1500/html/mig_gde/mig_gde160.htm:
Inner and outer tables
The terms outer table and inner table describe the placement of the tables in an outer join:
In a left join, the outer table and inner table are the left and right tables respectively. The outer table and inner table are also referred to as the row-preserving and null-supplying tables, respectively.
In a right join, the outer table and inner table are the right and left tables respectively.
For example, in the queries below, T1 is the outer table and T2 is the inner table:
T1 left join T2
T2 right join T1
Or, using Transact-SQL syntax:
T1 *= T2
T2 =* T1
It means outer join, a simple = means inner join.
*= is LEFT JOIN and =* is RIGHT JOIN.
(or vice versa, I keep forgetting since I'm not using it any more, and Google isn't helpful when searching for *=)
Of course, you should write it this way:
SELECT *
FROM a
LEFT JOIN b ON b.id=a.id
The a,b syntax is evil.
ANSI-82 syntax
select
*
from
a
, b
where
a.id *= b.id
ANSI-92
select
*
from
a
left outer join b
on a.id = b.id
select * from a, b where a.id = b.id
Requires that a row exist in where b.id = a.id in order to return an answer
select * from a, b where a.id *= b.id
Will fill the columns from b with nulls when there wasn't a row in b where b.id = a.id.