This question already has answers here:
Oracle "(+)" Operator
(4 answers)
Oracle join operator
(1 answer)
Oracle-Conveting SQL to ANSI SQL
(1 answer)
Conversion of Oracle join to Ansi join
(1 answer)
Closed 5 years ago.
I have Oracle sql code that uses the old and not recommended (+) notation. I have a basic understanding of how to convert but am struggling with multiple tables.
For exmaple
select [list of columns]
from table1 t1,
table2 t2,
table3 t3,
table4 t4,
table5 t5
where t2.col1 = t1.col1
and t2.col2 = t3.col2
and t4.col1(+) = t2.col3
and t5.col1(+) = t2.col4
and t5.col2(+) = t2.col5
Hope this makes sense. I believe this is slightly different from other similar questions as they did not cover multiple joins in the same query
t4.col1(+) = t2.col3
would convert to
RIGHT OUTER JOIN table2 t2 on t4.col1 = t2.col3
etc.
but since table2 should be joined to table1 first, it would be cleaner to use the more common LEFT join and flip the tables:
from table1 t1,
INNER JOIN table2 t2 ON t2.col1 = t1.col1
LEFT JOIN table3 t3 ON t2.col2 = t3.col2
LEFT JOIN table4 t4 ON t2.col3 = t4.col1
LEFT JOIN table5 t5 ON t2.col4 = t5.col1
and t2.col5 = t5.col2
Just move each join condition from the WHERE clause into the corresponding JOIN:
select [list of columns]
from table1 t1
join table2 t2
on t2.col1 = t1.col1
join table3 t3
on t3.col2 = t2.col2
left join table4 t4
on t4.col1 = t2.col3
left join table5 t5
on t5.col1 = t2.col4
and t5.col2 = t2.col5
I never use the optional (i.e. redundant) inner and outer keywords, or right join (since you can always just reverse it to make a normal left join and avoid a lot of confusion).
Related
This question already has answers here:
Problems with INNER JOIN and LEFT/RIGHT OUTER JOIN
(5 answers)
Closed 2 years ago.
I have 3 tables: T1, T2, T3
I need to have all rows from T1 in result and if there are matching records in T2 and T3, then output them also.
So my query looks like
SELECT *
FROM T1
LEFT JOIN T2
ON T1.Id = T2.Id
INNER JOIN T3
ON T2.SecondId = T3.Id
But in result, I receive only records that have some records in T3.
Is it the same behavior as I would write
SELECT *
FROM T1
LEFT JOIN T2
ON T1.Id = T2.Id
WHERE T2.Value = 4
where LEFT JOIN behaves like INNER JOIN because of WHERE clause?
The syntax for what you probably want is:
SELECT *
FROM T1 LEFT JOIN
T2
ON T1.Id = T2.Id LEFT JOIN
T3
ON T2.SecondId = T3.Id
Without the second LEFT JOIN, the INNER JOIN filters out non-matches. That is because T2.SecondId will be NULL for those non-matches. This is the right approach; checking explicitly for NULLs has some edge cases that don't work.
For the second query:
SELECT *
FROM T1 LEFT JOIN
T2
ON T1.Id = T2.Id AND T2.Value = 4
I have two sql in netezza:
1.
SELECT T1.Col1, T2.Col2 FROM TableA T1 JOIN TableB T2 ON T1.Col3 = T2.Col3
2.
SELECT T1.Col1, T2.Col2 FROM TableA T1 FULL OUTER JOIN TableB T2 ON T1.Col3 = T2.Col3
I assume that 2 should return more or equal results as 1. However, results from 1 is more than 2.
Could someone help me understand why?
I am new to Hibernate. And here I successfully used SQL query as is using session.createSQLQuery():
SELECT t2.col1, t2.col2, t2.col3, t5.col4, t1.col5, t4.col6, t4.col7,
DECODE(t1.col8,null,t1.col9,t1.col8), t1.col10, t1.col11, t8.col12
FROM table1 t1
JOIN table2 t2
ON t1.xyz = t2.xyz
JOIN table3 t3
ON t2.col3 = t3.col3
LEFT JOIN view1 t4
ON t1.abc = t4.abc1
LEFT JOIN view2 t5
ON t1.abc10 = t5.abc2 AND t5.xyz1 = 1
JOIN table6 t6
ON t2.abc8 = t6.abc9
JOIN table7 t7
ON t6.xyz2 = t7.xyz2
LEFT JOIN table8 t8
ON t2.col1 = t8.abc3 AND t8.abc5 = 'XYZ' AND t8.abc6 = 1234
WHERE t2.DISPLAY = 'true'
AND t2.abc4 = 0
AND t6.abc7 = 0
AND t2.col2 = 0
So I don't have all those entities Java objects. And no mapping xml file. But this query does not work when I use "SELECT COUNT(*) FROM " It is giving me an error "unexpected token ON". So how to fix select COUNT(*)? Thank you.
You need a group by to do COUNT(*).
group by the values you select and you should have your count ;)
I am trying to do an INNER JOIN and LEFT JOIN in the same query in MS ACCESS and here is my query
SELECT T2.Col1, T2.Col2, T2.Col3, TB.Col1
FROM (T2
INNER JOIN TB ON
TB.Col1 = T2.Col1 AND TB.Col2 = T2.Col2)
LEFT JOIN T1
ON (T1.Col1 = TB.Col1) AND (T1.Col2 = T2.Col2)
WHERE T1.Col1 IS NULL OR T1.Col2 IS NULL
But at (T1.Col1 = TB.Col1)` it says JOIN Expression not supported. Can some one help me with this.
I don't want to create an inner query and then create another left query with that seperately
An earlier answer, since deleted, recommended you remove all the parentheses from your query. However Access requires parentheses in the FROM clause when it includes more than one join.
Since the problem is with the joins, start with a simpler query which focuses on them only. See whether this query runs without error.
SELECT *
FROM
(T2
INNER JOIN TB
ON T2.Col1 = TB.Col1 AND T2.Col2 = TB.Col2)
LEFT JOIN T1
ON T2.Col1 = T1.Col1 AND T2.Col2 = T1.Col2
Once you get the joins correct, replace * with your field names and add your WHERE clause.
I have a query in the following format
select
*
from
Table1 t1
inner join Table2 t2
inner join Table3 t3 on t2.ID = t3.ID
on t3.ID = t1.ID
What I do know:
Not providing the last on condition results in an error.
Additionally changing the first join condition from on t2.ID = t3.ID to on t1.ID = t2.ID results in an error that t1.ID could not be bound.
Obviously the above examples are arbitrary and may not actually produce a practically useful result. However, an explanation of what providing the on later is actually doing would be great.
Thanks
EDIT
I'm not trying to change the question to something that works but to understand what MSSQL is doing when I provide it.
You can use the format you specified (presuming the correct table aliases), if you use parenthesis.
Select ... -- never use Select *
From (Table1 As T1
Join Table2 As T2
On T2.ID = T1.ID)
Join Table3 As T3
On T3.ID = T1.ID
However, with equi-joins (inner joins) it really makes no difference and it is easier to read if you do not use parenthesis. However, this format is very useful with outer joins. Take the following two examples:
Example 1
Select ...
From Table1 As T1
Left Join Table2 As T2
On T2.T1_ID = T1.ID
Join Table3 As T3
On T3.T2_ID = T2.ID
Example 2
Select ...
From Table1 As T1
Left Join (Table2 As T2
Join Table3 As T3
On T3.T2_ID = T2.ID)
On T2.T1_ID = T1.ID
Suppose in this situation, that T3.T2_ID is a non-nullable foreign key to Table2. In Example1, the Inner Join to Table3 will effectively filter out rows that would have been null because the given T2.T2_ID does not exist in Table1. However, in the second example, the join between Table2 and Table3 is done before the Left Join to Table1 is processed. Thus, we'll get the same rows from Table1 and Table2 as:
Example 3
Select ...
From Table1 As T1
Left Join Table2 As T2
On T2.T1_ID = T1.ID
Assuming you meant t1 rather than t, then your query:
select
*
from
Table1 t1
inner join Table2 t2
inner join Table3 t3 on t2.ID = t3.ID
on t3.ID = t1.ID
...can be made rather more clear by the addition of the brackets it doesn't really need:
select
*
from
Table1 t1
inner join
(Table2 t2 inner join Table3 t3 on t2.ID = t3.ID) on t3.ID = t1.ID
Effectively, you're explicitly saying "join t2 to t3, then join t1 to that."
Does that help?
First off--you don't define what t is
Table1 is aliased t1
Table2 is aliased t2
Table3 is aliased t3
But there is no plain t.
Second, you are not doing a join of t1 to t2, but of t1 to t3 and then t3 to t2. That will work. If there is a relation between t1 and t2 (t1.ID=t2.ID) then that "on" statement should directly follow the inner join statement for t2:
select
*
from
Table1 t1
inner join Table3 t3 on t1.ID = t3.ID
inner join Table2 t2 on t3.ID = t2.ID
UPDATE (based on your update)
are t1.ID, t2.ID, and t3.ID all the same data type?