Real time usage and difference between left outer join and right outer join [duplicate] - sql

This question already has answers here:
Does "Right Outer Join" have any useful purpose?
(9 answers)
Closed 9 years ago.
If Left outer join is used(select table A left outer join table B), I can have null values in right table(Table B) when data is not matching with left table(Table A). If I change the select query order (select table B left outer join table A), now I can have null data in Table A. So, same operation can be performed by using left outer join. So, what is the use of right outer join?
Please help me to get solution on this.

left outer join and right outer join are, in a sense, redundant. You can write a query using only one of them.
They are both provided for the same reason that < and > are both provided. Sometimes one or the other makes more sense for a given logical operation.
As for me, I strive to write queries using only join and left outer join. The left outer join makes more sense to me, because it says "keep all the rows in the first table, along with matching rows in other tables". This doesn't mean that right outer join is wrong, just that different people understand things in different ways.

The difference is simple – in a left outer join, all of the rows from the “left” table will be displayed, regardless of whether there are any matching columns in the “right” table. In a right outer join, all of the rows from the “right” table will be displayed, regardless of whether there are any matching columns in the “left” table. Hopefully the example that we gave above help clarified this as well.
Should I use a right outer join or a left outer join?
Actually, it doesn’t matter. The right outer join does not add any functionality that the left outer join didn’t already have, and vice versa. All you would have to do to get the same results from a right outer join and a left outer join is switch the order in which the tables appear in the SQL statement.

SELECT * from TableA LEFT JOIN TableB
Same as
SELECT * from TableB RIGHT JOIN TableA
SELECT * FROM TableA LEFT JOIN TableB
All rows in TableA and matching rows in TableB. If no matching row is found in TableB then all the columns of TableB will be replaced by null
Example:
TableA rows
1
2
3
Table B rows
2
3
4
TableA LEFT JOIN TableB will give the following tuples:
(1 NULL)
(2 2)
(3 3)
TableA RIGHT JOIN TableB will give the following tuples:
(2 2)
(3 3)
(NULL 4)
TableA OUTER JOIN TableB the following tuples:
(1 NULL)
(2 2)
(3 3)
(NULL 4)

Related

Why an 'ON' clause is required in a left outer join

As far as I understand in a left outer join between two tables (say a & b) all the rows of the table on the left side of the join are retrieved regardless of the values in the rows on the right table. Then why do we need an 'ON' clause specifying a condition, something like this:
select * from a LEFT OUTER JOIN b on a.some_column1 = b.some_column2;
Why is there a need for the statement "a.some_column1 = b.some_column2".
A left join would return all the rows from table a, and for each row the matching row in table b, if it exists - if it doesn't, nulls would be returned instead of b's columns. The on clause defines how this matching is done.
An on clause is required since you are "joining", and you need to tell which columns you want to join by. Otherwise you would use traditional from without any where condition to all possible row combinations. But you wanted a join, right?
Yeah, that's pretty much it is.
As far as I understand in a left outer join between two tables (say a & b) all the rows of the table on the left side of the join are retrieved regardless of the values in the rows on the right table.
That is correct in the sense that it says something about what left join on returns, but it isn't a definition of what it returns. left join on returns inner join on rows plus (union all) unmatched left table rows extended by nulls.
inner join on returns the rows of cross join that satisfy the on condition--which could be any condition on the columns. cross join returns every combination of a row from the left table & a row from the right table.
What do you expect outer join without on to mean? In standard SQL outer & inner join have to have an on. inner join on a true condition is the same as cross join. Which has no unmatched left table rows. So if you want outer join with no on to mean outer join on a true condition then, since there are no unmatched rows in the inner join on that condition, the result is also just cross join. (MySQL allows inner join to be used without an on, but it just means cross join.)

what is the use of right join exactly as we can get the same result from left join [duplicate]

This question already has answers here:
When or why would you use a right outer join instead of left?
(11 answers)
Closed 6 years ago.
I want to know that what is the use of right join exactly as we can get the same result from left join by interchanging the tables.
So let's take an example here -
Suppose i need to join two tables TAB_A and TAB_B with right join as below to get the result -
SELECT * FROM TAB_A RIGHT JOIN TAB_B
but i can also get the same result of query by using left join also instead of right join as below
SELECT * FROM TAB_B LEFT JOIN TAB_A
So my question is what is the purpose of right join in sql exactly, is there any performance related comparision or anything that can not be done by left join?
Suppose you need to do a double join like this:
SELECT *
FROM table1
LEFT JOIN table2 ON table1.name = table2.name1
RIGHT JOIN table3 ON table2.position = table3.job;
that is when the RIGHT JOIN is useful...
Regards
In case of only tow table you can switch the tables to get the same result from right and left join. But if you are having more than two table and you need to put left join on few tables right join on few tables so in that case you need both the joins.
As stated in http://dev.mysql.com/doc/refman/5.7/en/outer-join-simplification.html
At the parser stage, queries with right outer joins operations are converted to equivalent queries containing only left join operations.
Using RIGHT JOIN will cause parser conversions, but it should be negligible in pracitce.

Can RIGHT OUTER could be written as a LEFT OUTER by just flipping the tables [duplicate]

This question already has answers here:
When or why would you use a right outer join instead of left?
(11 answers)
Closed 8 years ago.
Can we use right join instead of left join and vice versa by just flipping the table position in the query.If so then why we need both joins?
Yes. Following query have same result:
SELECT *
FROM table1
LEFT JOIN table2 ON table1.column1 = table2.column2
SELECT *
FROM table2
RIGHT JOIN table1 ON table1.column1 = table2.column2
If a developer wants all records from #t1 and only matching records from #t2, then he can write the query as
Select * from #t1 left join #t2 or Select * from #t2 right join #t1
Both are same. They give same results.
First, there are more than just these 2 types of joins. There is also a FULL OUTER JOIN, in which case non-matching rows from both sides are included in the result. Additionally, an INNER JOIN (matching rows only) and CROSS JOIN (cartesian product) are available.
In a simple query like the examples in this thread, one can use LEFT or RIGHT and reverse the tables to return the desired results. But as additional tables are added to the query, it may be easier to add those with different join type rather than refactoring the entire query to achive the same semantics.
Yes, the only difference is the order in which you write the joined tables, you can rewrite LEFT into RIGHT and vice versa. So strictly speaking, we do not need to have both joins.
There is a discussion on why people use RIGHT joins.

What means "table A left outer join table B ON TRUE"?

I know conditions are used in table joining. But I met a specific situation and the SQL codes writes like "Table A join table B ON TRUE"
What will happen based on the "ON TRUE" condition? Is that just a total cross join without any condition selection?
Actually, the original expression is like:
Table A LEFT outer join table B on TRUE
Let's say A has m rows and B has n rows. Is there any conflict between "left outer join" and "on true"? Because it seems "on true" results a cross join.
From what I guess, the result will be m*n rows. So, it has no need to write "left outer join", just a "join" will give the same output, right?
Yes. That's the same thing as a CROSS JOIN.
In MySQL, we can omit the [optional] CROSS keyword. We can also omit the ON clause.
The condition in the ON clause is evaluated as a boolean, so we could also jave written something like ON 1=1.
UPDATE:
(The question was edited, to add another question about a LEFT [OUTER] JOIN b which is different than the original construct: a JOIN b)
The "LEFT [OUTER] JOIN" is slightly different, in that rows from the table on the left side will be returned even when there are no matching rows found in the table on the right side.
As noted, a CROSS JOIN between tables a (containing m rows) and table b containing n rows, absent any other predicates, will produce a resultset of m x n rows.
The LEFT [OUTER] JOIN will produce a different resultset in the special case where table b contains 0 rows.
CREATE TABLE a (i INT);
CREATE TABLE b (i INT);
INSERT INTO a VALUES (1),(2),(3);
SELECT a.i, b.i FROM a LEFT JOIN b ON TRUE ;
Note that the LEFT JOIN will returns rows from table a (a total of m rows) even when table b contains 0 rows.
A cross join produces a cartesian product between the two tables, returning all possible combinations of all rows. It has no on clause because you're just joining everything to everything.
Cross join does not combine the rows, if you have 100 rows in each table with 1 to 1 match, you get 10.000 results, Innerjoin will only return 100 rows in the same situation.
These 2 examples will return the same result:
Cross join
select * from table1 cross join table2 where table1.id = table2.fk_id
Inner join
select * from table1 join table2 on table1.id = table2.fk_id
Use the last method
The join syntax's general form:
SELECT *
FROM table_a
JOIN table_b ON condition
The condition is used to tell the database how to match rows from table_a to table_b, and would usually look like table_a.some_id = table_b.some_id.
If you just specify true, you will match every row from table_a with every row of table_b, so if table_a contains n rows and table_b contains m rows the result would have m*n rows.
Most(?) modern databases have a cleaner syntax for this, though:
SELECT *
FROM table_a
CROSS JOIN table_b
The difference between the pure cross join and left join (where the condition is forced to be always true, as when using ON TRUE) is that the result set for the left join will also have rows where the left table's rows appear next to a bunch of NULLs where the right table's columns would have been.

What is difference between INNER join and OUTER join [duplicate]

This question already has answers here:
What is the difference between "INNER JOIN" and "OUTER JOIN"?
(28 answers)
Closed 9 years ago.
Difference between inner and outer join. i am using two table and want to fetch data from both table so which type join we should use owning of that we can solve our problem
This is the best and simplest way to understand joins:
Credits go to the writer of this article HERE
Inner join - An inner join using either of the equivalent queries gives the intersection of the two tables, i.e. the two rows they have in common.
Left outer join -
A left outer join will give all rows in A, plus any common rows in B.
Full outer join -
A full outer join will give you the union of A and B, i.e. All the rows in A and all the rows in B. If something in A doesn't have a corresponding datum in B, then the B portion is null, and vice versa.
check this
INNER JOIN: Returns all rows when there is at least one match in BOTH tables
LEFT JOIN: Return all rows from the left table, and the matched rows from the right table
RIGHT JOIN: Return all rows from the right table, and the matched rows from the left table
FULL JOIN: Return all rows when there is a match in ONE of the tables
Inner join matches tables on keys, but outer join matches keys just for one side.
For example when you use left outer join the query brings the whole left side table and matches the right side to the left table primary key and where there is not matched places null.