I will like to ask which will be the better option and why if I intend to LEFT JOIN a few tables? Provide an example below.
Option 1:
SELECT * FROM TABLEA a
LEFT JOIN TABLEB b
ON a.id=b.id
LEFT JOIN TABLEC c
ON a.id=c.id
LEFT JOIN TABLED d
ON a.id=d.id
Option 2(CTE):
WITH tablea_b as (
SELECT * FROM TABLEA a
LEFT JOIN TABLEB b
ON a.id=b.id)
, tablea_b_c as (
SELECT * FROM tablea_b a
LEFT JOIN TABLEC c
ON a.id=c.id)
, tablea_b_c_d as (
SELECT * FROM tablea_b_c a
LEFT JOIN TABLED d
ON a.id = d.id) SELECT * FROM tablea_b_c_d
Basically the differences is i left join part by part at option 2 whereas at option 1 i do it in one go. Are there any differences in terms of efficiency?
Related
With this kind of data
create table table_a as select 1 as id1;
insert into table_a values (2),(3);
create table table_b as select 1 as id1, 'a' as id2;
insert into table_b values (1,'b');
create table table_c as select 'a' as id2;
I have the following kind of join in Impala sql:
select *
from table_a as a
left join table_b as b
on b.id1 = a.id1
left join table_c as c
on c.id2 = b.id2
yielding this result
"id1","id1","id2","id2"
1,1,b,
1,1,a,a
2,,,
3,,,
I would like the second join to be inner join instead of left join:
select *
from table_a as a
left join table_b as b
on b.id1 = a.id1
join table_c as c /* <- How to process this join first without using inner queries? */
on c.id2 = b.id2
and get this result:
"id1","id1","id2","id2"
1,1,a,a
2,,,
3,,,
Thus, I would like the inner join of table_b and table_c to take place first and only after to do the left join between table_a and (table_b inner joined to table_b).
Is possible to determine the join order in such manner without using inner queries?
With the help from #jarlh, I realized left-to-right processing of joins and then found that it is possible to use RIGHT joins:
select *
from table_c as c
join table_b as b
on b.id2 = c.id2
right join table_a as a
on a.id1 = b.id1;
in order to get the desired result:
"id2","id1","id2","id1"
a,1,a,1
,,,2
,,,3
I have two tables Say A and B. A is master table and B is child table, from which I need values as below.
select A.Id, A.Name, B.Path from A,B where A.Id=B.Id
Now, I want to add column of 3rd table which is child of table 'B', say C i.e. C.File.
The value of C.File will be null if C.SubId=B.SubId is false else will return value when condition becomes true.
This is the exact definition of a left join:
SELECT a.id, b.name, b.path, c.file
FROM a
JOIN b ON a.id = b.id
LEFT JOIN c ON b.subid = c.subid
You need to LEFT JOIN your third table from what I can gather.
SELECT A.Id, A.Name, B.Path, C.file
FROM tableA a
INNER JOIN tableB b ON a.id = b.id
LEFT JOIN tableC c ON b.subid = c.subid
Simply Join all the three tables using INNER JOIN
select A.Id, A.Name, B.Path ,C.File
FROM A
INNER JOIN B
ON A.Id=B.Id
INNER JOIN C
ON C.SubId=B.SubId
I have a query which involves a left join followed by a join. I want to make sure the left join is done first. The left join comes before the join nin my query, is this enough? This is how the join looks like
select * from
(select *....) A
left join
(select *...) B
on A.a = B.a
left join
C
on A.f = C.f
I cannot see the JOIN in your code, only two LEFT JOIN statements.
However, if you have something like this:
select * from
(select *....) A
left join
(select *...) B
on A.a = B.a
join
C
on A.f = C.f
and you want to make sure the LEFT JOIN is executed first, you can move this LEFT JOIN to a sub-query:
select *
from (
select * from (
(select *....) A
left join
(select *...) B
on A.a = B.a
)
) D
join
C
on D.f = C.f
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 been having a hard time googling an answer for this, but....
can someone explain to me the difference between putting the ON condition of a JOIN with the the JOIN itself vs putting the ON at the end of all the other JOINs.
here is an example http://sqlfiddle.com/#!3/e0a0f/3
CREATE TABLE TableA (Email VARCHAR(100), SomeNameA VARCHAR(100))
CREATE TABLE Tableb (Email VARCHAR(100), SomeNameB VARCHAR(100))
CREATE TABLE Tablec (Email VARCHAR(100), SomeNameC VARCHAR(100))
INSERT INTO TableA SELECT 'joe#test.com', 'JoeA'
INSERT INTO TableA SELECT 'jan#test.com', 'JaneA'
INSERT INTO TableA SELECT 'dave#test.com', 'DaveA'
INSERT INTO TableB SELECT 'joe#test.com', 'JoeB'
INSERT INTO TableB SELECT 'dave#test.com', 'DaveB'
INSERT INTO TableC SELECT 'joe#test.com', 'JoeC'
INSERT INTO TableC SELECT 'dave#test.com', 'DaveC'
SELECT TOP 2 a.*,
b.*,
c.*
FROM TableA a
LEFT OUTER JOIN TableB b
ON a.email = b.email
INNER JOIN TableC c
ON c.Email = b.email;
SELECT TOP 2 a.*,
b.*,
c.*
FROM TableA a
LEFT OUTER JOIN TableB b
INNER JOIN TableC c
ON c.Email = b.email
ON a.email = b.email;
I don't understand why these two SELECT statements produce different results.
What matters is orders of joins. Treat your expressions as if every join produced temporary "virtual" table.
So when you write
FROM TableA a
LEFT OUTER JOIN TableB b ON a.email = b.email
INNER JOIN TableC c ON c.Email = b.email ;
then order is as follows:
TableA is left joined to TableB producing temporary relation V1
V1 is inner joined to TableC.
Meanhwile when you write:
FROM TableA a
LEFT OUTER JOIN TableB b
INNER JOIN TableC c ON c.Email = b.email ON a.email = b.email;
then order is as follows:
TableB is inner joined to TableC producing temporary relation V1.
TableA is left joined to V1.
Thus results are different. It is generally recommended to use parenthesis in such situations to improve readability of the query:
FROM TableA a
LEFT OUTER JOIN
(TableB b INNER JOIN TableC c ON c.Email = b.email)
ON a.email = b.email;
In your second example, the part ON a.email = b.email belongs to the LEFT JOIN.
If written like this, it means the following:
INNER JOIN TableC with TableB and LEFT OUTER JOIN the result with TableA.
The result will be all rows from TableA joined with those rows from TableB that also have an entry in TableC.
The first example means the following:
LEFT OUTER JOIN TableB with TableA and INNER JOIN TableC with the result. This is equivalent to using an INNER JOIN for TableB.
Explanation: When you LEFT OUTER JOIN TableA with TableB you will get all rows from TableA and for matching rows in TableB you will get that data, too. In your result set you will have rows with b.email = NULL and this will now be INNER JOINed with TableC. As long as there is no entry in TableC with email = NULL you will get the results you observed.