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

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.

Related

Right join or which type of join?

I have 4 tables.
1 with just sitenames.
3 tables, which contains sitenames and the ammount of hits on them for different user types.
I need to make a report on the number of hits on thoose sites for each user type like this
Site Userype1hits Userype2hits Userype3hits
so in the select part I neeed to crosscheck with a table called noanswer
like
select *
from table
where site in (select site from noanswer)
So from what I understand I need to use join, and in this case right join?
How do I do with join in this query?
You would use left join:
select sn.*, t1.cnt, t2.cnt, t3.cnt
from sitenames sn left join
table1 t1
on t1.name = sn.name left join
table2 t2
on t2.name = sn.name left join
table3 t3
on t3.name = sn.name;
Your question is vague on the field names.
A left join keeps all rows in the first table and matches rows in the subsequent tables. It is much more commonly used than right join, probably because it is easier to read the logic thinking "I'll keep all of these rows". A series of right joins actually keeps all rows in the last table, so you have to wait to see which rows stay.
I did like this, googled some further as I did not get it to work even with the answer here, joins are totaly new to me. The internalusers, external and so on is just fake to post what I did here as I cant use the real names.
select s.Site,s2.Total_hits as 'Internalusers',s3.Total_Hits as 'ExternalUsers',
s4.Total_hits as 'Comapany2users' from Database.Table as S
left outer join HITAnalyze.dbo.Internal as s2 on s2.site = s.Site
left outer join HITAnalyze.dbo.External1 as s3 on s3.site = s.Site
left outer join HITAnalyze.dbo.Company2 as s4 on s4.site = s.Site

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

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

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)

What is the difference between LEFT OUTER JOIN and LEFT JOIN? Result is the same [duplicate]

What is the difference between LEFT JOIN and LEFT OUTER JOIN?
As per the documentation: FROM (Transact-SQL):
<join_type> ::=
[ { INNER | { { LEFT | RIGHT | FULL } [ OUTER ] } } [ <join_hint> ] ]
JOIN
The keyword OUTER is marked as optional (enclosed in square brackets). In this specific case, whether you specify OUTER or not makes no difference. Note that while the other elements of the join clause is also marked as optional, leaving them out will make a difference.
For instance, the entire type-part of the JOIN clause is optional, in which case the default is INNER if you just specify JOIN. In other words, this is legal:
SELECT *
FROM A JOIN B ON A.X = B.Y
Here's a list of equivalent syntaxes:
A LEFT JOIN B A LEFT OUTER JOIN B
A RIGHT JOIN B A RIGHT OUTER JOIN B
A FULL JOIN B A FULL OUTER JOIN B
A INNER JOIN B A JOIN B
Also take a look at the answer I left on this other SO question: SQL left join vs multiple tables on FROM line?.
To answer your question there is no difference between LEFT JOIN
and LEFT OUTER JOIN, they are exactly same that said...
At the top level there are mainly 3 types of joins:
INNER
OUTER
CROSS
INNER JOIN - fetches data if present in both the tables.
OUTER JOIN are of 3 types:
LEFT OUTER JOIN - fetches data if present in the left table.
RIGHT OUTER JOIN - fetches data if present in the right table.
FULL OUTER JOIN - fetches data if present in either of the two tables.
CROSS JOIN, as the name suggests, does [n X m] that joins everything to everything.
Similar to scenario where we simply lists the tables for joining (in the FROM clause of the SELECT statement), using commas to separate them.
Points to be noted:
If you just mention JOIN then by default it is a INNER JOIN.
An OUTER join has to be LEFT | RIGHT | FULL you can not simply say OUTER JOIN.
You can drop OUTER keyword and just say LEFT JOIN or RIGHT JOIN or FULL JOIN.
For those who want to visualise these in a better way, please go to this link:
A Visual Explanation of SQL Joins
What is the difference between left join and left outer join?
Nothing. LEFT JOIN and LEFT OUTER JOIN are equivalent.
Left Join and Left Outer Join are one and the same. The former is the shorthand for the latter. The same can be said about the Right Join and Right Outer Join relationship. The demonstration will illustrate the equality. Working examples of each query have been provided via SQL Fiddle. This tool will allow for hands on manipulation of the query.
Given
Left Join and Left Outer Join
Results
Right Join and Right Outer Join
Results
I'm a PostgreSQL DBA, as far as I could understand the difference between outer or not outer joins difference is a topic that has considerable discussion all around the internet. Until today I never saw a difference between those two; So I went further and I try to find the difference between those.
At the end I read the whole documentation about it and I found the answer for this,
So if you look on documentation (at least in PostgreSQL) you can find this phrase:
"The words INNER and OUTER are optional in all forms. INNER is the default; LEFT, RIGHT, and FULL imply an outer join."
In another words,
LEFT JOIN and LEFT OUTER JOIN ARE THE SAME
RIGHT JOIN and RIGHT OUTER JOIN ARE THE SAME
I hope it can be a contribute for those who are still trying to find the answer.
I find it easier to think of Joins in the following order:
CROSS JOIN - a Cartesian product of both tables. ALL joins begin here
INNER JOIN - a CROSS JOIN with a filter added.
OUTER JOIN - an INNER JOIN with missing elements (from either LEFT or RIGHT table)
added afterward.
Until I figured out this (relatively) simple model, JOINS were always a bit more of a black art. Now they make perfect sense.
Hope this helps more than it confuses.
To answer your question
In Sql Server joins syntax OUTER is optional
It is mentioned in msdn article : https://msdn.microsoft.com/en-us/library/ms177634(v=sql.130).aspx
So following list shows join equivalent syntaxes with and without OUTER
LEFT OUTER JOIN => LEFT JOIN
RIGHT OUTER JOIN => RIGHT JOIN
FULL OUTER JOIN => FULL JOIN
Other equivalent syntaxes
INNER JOIN => JOIN
CROSS JOIN => ,
Strongly Recommend Dotnet Mob Artice : Joins in Sql Server
Why are LEFT/RIGHT and LEFT OUTER/RIGHT OUTER the same? Let's explain why this vocabulary.
Understand that LEFT and RIGHT joins are specific cases of the OUTER join, and therefore couldn't be anything else than OUTER LEFT/OUTER RIGHT. The OUTER join is also called FULL OUTER as opposed to LEFT and RIGHT joins that are PARTIAL results of the OUTER join. Indeed:
Table A | Table B Table A | Table B Table A | Table B Table A | Table B
1 | 5 1 | 1 1 | 1 1 | 1
2 | 1 2 | 2 2 | 2 2 | 2
3 | 6 3 | null 3 | null - | -
4 | 2 4 | null 4 | null - | -
null | 5 - | - null | 5
null | 6 - | - null | 6
OUTER JOIN (FULL) LEFT OUTER (partial) RIGHT OUTER (partial)
It is now clear why those operations have aliases, as well as it is clear only 3 cases exist: INNER, OUTER, CROSS. With two sub-cases for the OUTER.
The vocabulary, the way teachers explain this, as well as some answers above, often make it looks like there are lots of different types of join. But it's actually very simple.
There are only 3 joins:
A) Cross Join = Cartesian (E.g: Table A, Table B)
B) Inner Join = JOIN (E.g: Table A Join/Inner Join Table
B)
C) Outer join:
There are three type of outer join
Left Outer Join = Left Join
Right Outer Join = Right Join
Full Outer Join = Full Join
There are mainly three types of JOIN
Inner: fetches data, that are present in both tables
Only JOIN means INNER JOIN
Outer: are of three types
LEFT OUTER - - fetches data present only in left table & matching condition
RIGHT OUTER - - fetches data present only in right table & matching condition
FULL OUTER - - fetches data present any or both table
(LEFT or RIGHT or FULL) OUTER JOIN can be written w/o writing "OUTER"
Cross Join: joins everything to everything
Syntactic sugar, makes it more obvious to the casual reader that the join isn't an inner one.
Just in the context of this question, I want to post the 2 'APPLY' operators as well:
JOINS:
INNER JOIN = JOIN
OUTER JOIN
LEFT OUTER JOIN = LEFT JOIN
RIGHT OUTER JOIN = RIGHT JOIN
FULL OUTER JOIN = FULL JOIN
CROSS JOIN
SELF-JOIN: This is not exactly a separate type of join. This is basically joining a table to itself using one of the above joins. But I felt it is worth mentioning in the context JOIN discussions as you will hear this term from many in the SQL Developer community.
APPLY:
CROSS APPLY -- Similar to INNER JOIN (But has added advantage of being able to compute something in the Right table for each row of the Left table and would return only the matching rows)
OUTER APPLY -- Similar to LEFT OUTER JOIN (But has added advantage of being able to compute something in the Right table for each row of the Left table and would return all the rows from the Left table irrespective of a match on the Right table)
https://www.mssqltips.com/sqlservertip/1958/sql-server-cross-apply-and-outer-apply/
https://sqlhints.com/2016/10/23/outer-apply-in-sql-server/
Real life example, when to use OUTER / CROSS APPLY in SQL
I find APPLY operator very beneficial as they give better performance than having to do the same computation in a subquery. They are also replacement of many Analytical functions in older versions of SQL Server. That is why I believe that after being comfortable with JOINS, one SQL developer should try to learn the APPLY operators next.