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.
Related
The condition is I know the right table does not have all matching records with the left table.
but I am still not able to get data from left table with null from right table
select a.sales, b.profit
from T1 a
left join T2 b on a.id = b.id
where b.category = 'office'
and b.code = '245'
because of the where condition of right table, the right table does not have matching records,
without where condition I got the records.
My question is will left table be affected with where condition of right table although using left join to retain the left table records.
Your WHERE clause forces the query to only return rows which b.category and b.code match the required values and so are non-NULL; this effectively turns your JOIN condition into an INNER JOIN.
You want to put the filters in the join condition:
select a.sales,
b.profit
from T1 a
left join T2 b
on ( a.id = b.id
AND b.category = 'office'
AND b.code = '245')
Or to pre-filter T2 in a sub-query:
select a.sales,
b.profit
from T1 a
left join (
SELECT *
FROM T2
WHERE category = 'office'
AND code = '245'
) b
on a.id = b.id
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.
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.
Working on Oracle: I am attempting to do an inner self join, with a where clause, then take that result and do a left outer join on it:
(select * from table1 A
inner join
select * from table1 B
on A.id = B.id
where
A.id is not null and B.id is not null) C
left outer join
select * from table2 D
on C.id = D.id
Somehow I am syntactically challenged and can't make this work. Can't seem to find the right syntax anywhere.
Just the put the where clause at the end. The database will get it right:
select *
from table1 A
inner join table1 B on A.id = B.id
left join table2 D on D.id = A.id
where A.id is not null
In this case, we can take advantage of the logical transitive property for your id column joins and where clause.
Your second join needs to be joined to a query add a select * from at the beginning
select * from (select * from table1 A
inner join
select * from table1 B
on A.id = B.id
where
A.id is not null and B.id is not null) C
left outer join
select * from table2 D
on C.id = D.id
I have two queries that I thought meant the same thing, but I keep getting different results and I was hoping someone could explain how these are different:
1.
select *
from table1 a
left join table2 b on a.Id = b.Id and a.val = 0
where b.Id is null
2.
select *
from table1 a
left join table2 b on a.Id = b.Id
where b.Id is null
and a.val = 0
The point of the query is to find the rows that are in table1 and val = 0 that are not in table2.
I'm using sql server 2008 as well, but I doubt that this should matter.
When considering left joins think of them as having 3 conceptual stages.
The join filter is applied
The left rows are added back in
the where clause is applied.
You will then see why you get different results.
That also explains why this returns results
select o.*
from sys.objects o
left join sys.objects o2 on o.object_id=o2.object_id and 1=0
And this doesn't.
select o.*
from sys.objects o
left join sys.objects o2 on o.object_id=o2.object_id
where 1=0
SELECT * from TABLE1 t1
WHERE Val = 0
AND NOT EXISTS(SELEct 1 from Table2 t2 Where t1.Id = t2.Id)
If you remove the WHERE clause entirely, using a LEFT OUTER JOIN means that all the rows from the table on the left hand side will appear, even if they don't satisfy the JOIN criteria. For example, no rows satisfy the expression 1 = 0 however this:
SELECT *
FROM table1 AS a
LEFT OUTER JOIN table2 AS b
ON a.Id = b.Id
AND 1 = 0;
still results in all rows in table1 being returned where the id values match. Simply put, that's the way OUTER JOINs work.
The WHERE clause is applied after the JOIN, therefore this
SELECT *
FROM table1 AS a
LEFT OUTER JOIN table2 AS b
ON a.Id = b.Id
WHERE 1 = 0;
will return no rows.