SELECT tableA.id, tableB.id FROM tableA, tableB WHERE tableA.id *= tableB.id. this query is sql server 2005 and i need sql server 2016? - sql-server-2005

SELECT tableA.id, tableB.id FROM tableA, tableB WHERE tableA.id *= tableB.id.
This query is sql server 2005 and I need sql server 2016. I need to do a LEFT or RIGHT OUTER JOIN and for that, need to use the "new" syntax.

I need to do a LEFT or RIGHT OUTER JOIN and for that, need to use the
"new" syntax.
try this
SELECT A.id, B.id
FROM tableA A
LEFT OUTER JOIN tableB B ON A.id *= B.id

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.

Left Join With Regular Joins and Results

Question with left join. I am trying to LEFT JOIN a table that requires other tables to be joined on the initial left joined table. So..
SELECT * FROM tableA
LEFT JOIN tableB
ON tableB.id=tableA.id
JOIN tableC
ON tableC.id=tableB.id
The problem is if I don't left join table C I get no results, and if do left join I get too many results.
What kind of joins should I be using where if tableB join is null, tableC joins will also be null?
What about a subquery ?
SELECT * FROM tableA
LEFT JOIN (SELECT tableB.id FROM tableB
JOIN tableC
ON tableC.id=tableB.id) tableZ
ON tableZ.id=tableA.id
I don't left join table C I get no results, and if do left join I get
too many results
You need to determine what is your driving table and data. In this case, it seems like table A is the driving table and the join from B to C also could be a left join, meaning data from C could be returned even if no matching exists in B.
SELECT * FROM tableA
LEFT JOIN tableB
ON tableB.id=tableA.id
LEFT JOIN tableC
ON tableC.id=tableB.id
if do left join I get too many results
Can you post some sample data to show what you mean by this?
I think you might want this logic:
SELECT *
FROM tableA LEFT JOIN
(tableB JOIN
tableC
ON tableC.id = tableB.id
)
ON tableB.id = tableA.id ;
Normally, with LEFT JOIN you want to chain them, but there are some exceptions.

How to add conditional left or inner join based on parameter in SQL?

I am trying to add conditional left or inner join based on parameter in my select statement.
For example :
I have TableA and TableB, and I have a parameter called #test,
I wanna do something like this :
SELECT * FROM TableA
IF(#test ='')
INNER JOIN TableB
ELSE
LEFT OUTER JOIN TableB
IS that possible in SQL Server 2012 ?
You cannot change the kind of the JOIN based on a parameter, but you can filter out the rows with NULLs in an outer join.
Suppose that TableB is joined to TableA on b.a_id = a.id. Then you could write this query:
SELECT * FROM TableA a
LEFT OUTER JOIN TableB b
ON b.a_id = a.id
WHERE #test != '' OR b.a_id IS NOT NULL
Not exactly (except using dynamic SQL), but you can do this in the where clause:
SELECT *
FROM TableA LEFT OUTER JOIN
TableB
ON . . .
WHERE (#test = '' and TableB.id is not null) or (#test <> '')

SELECT * FROM tableA, tableB WHERE Conditions [+]

I have the following query
SELECT *
FROM tableA, tableB
WHERE Conditions [+]
What does this keyword Conditions[+] Stands for?
How this query behaves as a outer join?
That is old Oracle Join syntax.
SELECT *
FROM tableA, tableB
WHERE Conditions [+] -- this should be tableA (+) = tableB
The positioning of the + sign denotes the JOIN syntax.
If you query was:
SELECT *
FROM tableA, tableB
WHERE tableA.id (+) = tableB.Id
Then it would be showing a RIGHT OUTER JOIN so the equivalent is:
SELECT *
FROM tableA
RIGHT OUTER JOIN tableB
ON tableB.id = tableA.Id
If the + sign was on the other side then it would be a LEFT OUTER JOIN
SELECT *
FROM tableA, tableB
WHERE tableA.id = tableB.Id (+)
is equivalent to
SELECT *
FROM tableA
LEFT OUTER JOIN tableB
ON tableA.id = tableB.Id
I would advise using standard join syntax though.
If you do not specify a + sign then it will be interpreted as an INNER JOIN
SELECT *
FROM tableA, tableB
WHERE tableA = tableB
it's equivalent is:
SELECT *
FROM tableA
INNER JOIN tableB
ON tableA.id = tableB.id
A FULL OUTER JOIN would be written using two SELECT statements and a UNION:
SELECT *
FROM tableA, tableB
WHERE tableA.id = tableB.Id (+)
UNION
SELECT *
FROM tableA, tableB
WHERE tableA.id (+) = tableB.Id
It's equivalent is:
SELECT *
FROM tableA
FULL OUTER JOIN tableB
ON tableA.id = tableB.id
Here is a tutorial that explains a lot of these:
Old Outer Join Syntax
It is not important how that behaves. You should use the standard syntax for outer joins:
select *
from tableA left outer join
tableB
on . . .
The "(+)" syntax was introduced by Oracle before the standard syntax, and it is highly out of date.
'Conditions' here just means what you're using to filter all this data.
LIke here's an example:
SELECT *
FROM tableA, tableB
WHERE Name like '%Bob%'
would return names that have "Bob" anywhere inside.
About outer joins, actually you'd use that in the FROM clause:
So maybe
SELECT *
FROM tableA ta
OUTER JOIN tableB tb
ON ta.name = tb.name
WHERE ta.age <> 10
and there where here is optional, by the way
I hate to just copy & paste an answer, but this sort of thing can be found pretty easily if you do a little searching...
An outer join returns rows for one table, even when there are no
matching rows in the other. You specify an outer join in Oracle by
placing a plus sign (+) in parentheses following the column names from
the optional table in your WHERE clause. For example:
SELECT ut.table_name, uc.constraint_name
FROM user_tables ut, user_constraints uc
WHERE ut.table_name = uc.table_name(+);
The (+) after uc.table_name makes the user_constraint table optional.
The query returns all tables, and where there are no corresponding
constraint records, Oracle supplies a null in the constraint name
column.

*= 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.