Joins in SQL for retriving data from two tables - sql

There are two tables A and B. You are retreiving data from both tables where all rows from B table and only matching rows from A table should be displayed. Which of the following types of joins will you apply between A and B tables?
- Inner join
- Left outer join
- Right outer join
- Self join

Use left outer hoin or right outer join.
For example, the following satisfy your requirement.
select * from tableB
Left outer join tableA
on tableB.ID= tableA.ID
Or
select * from tableA
Right outer join tableB
on tableA.ID= tableB.ID
Better way to understand:

Easy, I would go with (B).
SELECT * FROM B x
LEFT JOIN A y
on x.someColName = y.someColname
EDIT: can also use Right join
SELECT * FROM A x
RIGHT OUTER JOIN B y
on x.someColName = y.someColname

This looks like homework, but it's dead simple enough that I'll just say that you're asking for B LEFT JOIN A.

Join Left
http://www.w3schools.com/Sql/sql_join_left.asp

Related

What's the SQL Equivalent of the SPSS Partial Outer Join?

I'm working on a project to convert an old SPSS system to Oracle. The current code makes heavy use of partial outer joins, and I'm not sure what the best way to replicate that in SQL would be.
Here's an example of a typical query:
SELECT
TA.A
TB.B
TC.C
FROM
TABLE_A TA
TABLE_B TB
TABLE_C TC
PARTIAL OUTER JOIN
TA.FIELD = TB.FIELD
AND TB.FIELD = TC.FIELD
From what I've read in the IBM Knowledge Base, a partial outer join is essentially a left/right outer join that merges multiple tables. IBM provides the following Venn diagram:
It seems like the best way to replicate this would be to do a full outer join between tables A and C, and then left outer joins from A to C and B to C.
Is that correct? Is there a better solution?
Thanks!
I asked around the office and one of the senior devs pointed me to this manual which defines a partial outer join as
Returns all records from the primary file (first file
in the FROM clause) whether or not there is a match in the secondary file(s)
In other words, take the first table in the FROM clause and do a left outer join to all the other tables.
So the original query:
SELECT
TA.A
TB.B
TC.C
FROM
TABLE_A TA
TABLE_B TB
TABLE_C TC
PARTIAL OUTER JOIN
TA.FIELD = TB.FIELD
AND TB.FIELD = TC.FIELD
Should become this:
SELECT
TA.A
TB.B
TC.C
FROM
TABLE_A TA LEFT OUTER JOIN TABLE_B TB ON TA.FIELD = TB.FIELD
LEFT OUTER JOIN TABLE_C TC ON TA.FIELD = TC.FIELD
You can do this in SQL using:
SELECT
TA.A,
TB.B,
TC.B
FROM
TABLE_A TA
FULL OUTER JOIN
TABLE_B TB
ON (TA.FIELD = TB.FIELD)
LEFT OUTER JOIN
TABLE_C TC
ON (TA.FIELD = TC.FIELD)
OR (TB.FIELD = TC.FIELD);
Every record in tables A and B will return, even if they do not match. Then only records from table C that match one in A or B will return.
The documentation is not at all clear. I'm not sure what SPSS does when there are rows in A and B that match different rows in C. One possibility is
select a.a, b.b, c.c
from a full join
b
on a.? = b.? left join
c
on a.? = c.? or b.? = c.?;

left join with three tables

Can anyone explain what will happen in following scenario?
SELECT *
FROM A,
B
LEFT JOIN C
ON B.FIELD1=C.FIELD1
WHERE A.FIELD1='SOME VALUE'
Here table A and table B are not joined with any condition. So my doubt is what kind of join will be applied between A and B?
A cross join (cartesian product, if you prefer) will be applied between the results of A and B left join C: each row in the first set will be tied to each row in the second set.
A cross join applies, If you don't used a join condition, get irrelavent results also.
Please try it
SELECT * FROM A INNER JOIN B ON A.IDCOLUMND=B.IDCOLUMN LEFT JOIN C ON B.FIELD1=C.FIELD1

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)

Joining multiple tables in SQL

Can sombody Explains me about joins?
Inner join selects common data based on where condition.
Left outer join selects all data from left irrespective of common but takes common data from right table and vice versa for Right outer.
I know the basics but question stays when it comes to join for than 5, 8, 10 tables.
Suppose I have 10 tables to join. If I have inner join with the first 5 tables and now try to apply a left join with the 6th table, now how the query will work?
I mean to say now the result set of first 5 tables will be taken as left table and the 6th one will be considerded as Right table? Or only Fifth table will be considered as left and 6th as right? Please help me regarding this.
When joining multiple tables the output of each join logically forms a virtual table that goes into the next join.
So in the example in your question the composite result of joining the first 5 tables would be treated as the left hand table.
See Itzik Ben-Gan's Logical Query Processing Poster for more about this.
The virtual tables involved in the joins can be controlled by positioning the ON clause. For example
SELECT *
FROM T1
INNER JOIN T2
ON T2.C = T1.C
INNER JOIN T3
LEFT JOIN T4
ON T4.C = T3.C
ON T3.C = T2.C
is equivalent to (T1 Inner Join T2) Inner Join (T3 Left Join T4)
It's helpful to think of JOIN's in sequence, so the former is correct.
SELECT *
FROM a
INNER JOIN b ON b.a = a.id
INNER JOIN c ON c.b = b.id
LEFT JOIN d ON d.c = c.id
LEFT JOIN e ON e.d = d.id
Would be all the fields from a and b and c where all the ON criteria match, plus the values from d where its criteria match plus all the contents of e where all its criteria match.
I know RIGHT JOIN is perfectly acceptable, but I've found in my experience that it's unnecessary - I almost always just join things from left to right.
> Simple INNER JOIN VIEW code...
CREATE VIEW room_view
AS SELECT a.*,b.*
FROM j4_booking a INNER JOIN j4_scheduling b
on a.room_id = b.room_id;
You can apply join like this..
select a.*,b.*,c.*,d.*,e.*
from [DatabaseName].[Table_a] a
INNER JOIN [DatabaseName].[Table_b] b ON a.id = b.id
INNER JOIN [DatabaseName].[Table_c] c ON b.id=c.id
INNER JOIN [DatabaseName].[Table_d] d on c.id=d.id
INNER JOIN [DatabaseName].[Table_e] e on d.id=e.id where a.con=5 and
b.con=6
Here, at place of a.* and in where condition, you can show column(filed) which you like and according condition in where condition. You can insert more table and database as per your choice. But mind that you need to mention database name and alias if you work in different database.
Just tried the following from the Example DataBase given in W3School. Worked Fine for me.
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate, Products.ProductName, Products.ProductID
FROM Orders
INNER JOIN Products
INNER JOIN Customers
ON Orders.CustomerID=Customers.CustomerID;
Join used to combine rows from two or more tables, based on a related column between them. This example from Adventure works:
SELECT a.[EmailAddress],b.[FirstName],b.[LastName],c.[PhoneNumber],d.[Name]
FROM [Person].[EmailAddress] a
INNER JOIN [Person].[Person] b
ON a.BusinessEntityID = b.BusinessEntityID
INNER JOIN [Person].[PersonPhone] c
ON b.BusinessEntityID = c.BusinessEntityID
INNER JOIN [Person].[PhoneNumberType] d
ON c.phoneNumberTypeID = d.phoneNumberTypeID

INNER and LEFT OUTER join help

Say I have 3 tables. TableA, TableB, TableC
I need to operate the recordset that is available after a INNER JOIN.
Set 1 -> TableA INNER JOIN TableB
Set 2 -> TableC INNER JOIN TableB
I need the Set 1 irrespective of if Set 2 is empty or not (LEFT OUTER JOIN) comes to mind.
So essentially, I am trying to write a query and have come this far
SELECT *
FROM TableA
INNER JOIN TableB ON ...
LEFT OUTER JOIN (TableC INNER JOIN TableB)
How would I write in SQL Server?
EDIT: In reality, what I am trying to do is to join multiple tables. How would your response change if I need to join multiple tables ex: OUTER JOIN OF (INNER JOIN of TableA and TableB) and (INNER JOIN OF TableC and TableD) NOTE: There is a new TableD in the equation
SELECT * FROM TableA
INNER JOIN TableB ON TableB.id = TableA.id
LEFT JOIN TABLEC ON TABLEC.id = TABLEB.id
I Don't know what columns you are trying to use but it is just that easy
Edit:
Looking at your edit it seems that you are confused about what Joins actually do. In the example I have written above you will recieve the following results.
Columns -> You will get all of the columns for TableA,TableB and TableC
Rows-> You will start off with all of the rows from tableA. Next you will remove all rows from TableA that do not have a matching "id" in Table B.(You will have duplicates if it is not a 1:1 relationship between TableA and TableB).
Now if you take the results from above you will match any records from TableC that match the TableB.id column. Any rows from above that do not have a matching TableC record will get a null value for all of the columns from TableC in the results.
ADVICE- I am betting that only part of this made sense to you but my advice is that you start writing some queries, predict the results and then see if your predictions are correct to see if you understand what it is doing.
What you want isn't a JOIN but a UNION.
SELECT * FROM TableA INNER JOIN TableB ON ...
UNION
SELECT * FROM TableC INNER JOIN TableD ON ...
You can actually add an ordering to your joins just like in a math equation where you might do this: (5 + 4) * (3 + 1).
Given the second part of your question, give this a try:
SELECT
<your columns>
FROM
(TableA INNER JOIN Table B ON <join criteria for A to B>)
LEFT OUTER JOIN
(TableC INNER JOIN Table D ON <join criteria for C to D>) ON
<join criteria for AxB to CxD>
Select * from ((((TableA a inner join TableB b on a.id = b.id)
left outer join TableC c on b.id = c.id)
full outer join TableD d on c.id = d.id)
right outer join TableE e on e.id = d.id)
/* etc, etc... */
You can lose the brackets if you want.
try this..
SELECT *
FROM TableA a
INNER JOIN TableB b ON a.id=b.id
LEFT OUTER JOIN (SELECT *
FROM TableC c
INNER JOIN TableD d on c.id=d.id
) dt on b.id=dt.id
You didn't give your join conditions or explain how the tables are intended to be related, so it's not obvious how this might be simplified.
SELECT a.a_id, b1.b_id b1_id, b2_id, bc.c_id
FROM TableA a JOIN TableB b1 on a.b_id = b1.b_id
LEFT JOIN (SELECT c.c_id, b2.b_id b2_id
FROM TableC c JOIN TableB b2 ON c.b_id = b2.b_id
) bc ON bc.c_id = a.c_id;
Looking at your latest edit, you can do something along the lines of:
SELECT <columns>
FROM (SELECT <columns> FROM TableA JOIN TableB ON <A-B join conditions>)
LEFT JOIN
(SELECT <columns> FROM TableC JOIN TableD ON <C-D join conditions>)
ON <AB-CD join conditions>
Although you don't actually need the inner projections, and can do:
SELECT <columns>
FROM (TableA a JOIN TableB b ON <A-B join conditions>)
LEFT JOIN
(TableC c JOIN TableD d ON <C-D join conditions>)
ON <AB-CD join conditions>
Where the AB-CD join conditions are written in terms of columns of a, b, c, d etc directly.
Since you're using Sql Server, why not create views that help you? Stuffing everything in a gigantic Sql statement can become hard to read. An example view might look like:
create view AandB
as
select *
from A
inner join B on B.aid = A.aid
And the same for CandD. Then you can retrieve the optional join with simple Sql:
select *
from AndB
left outer join CandD on AndB.cid = CandD.cid
If you're interested in rows from both sets, you can do a full join:
select *
from AndB
full outer join CandD on AndB.cid = CandD.cid
Assuming I Understand your question, I think this is what you're asking for:
SELECT *
FROM TableA INNER JOIN TableB on TableA.JoinColumn = TableB.JoinColumn
LEFT OUTER JOIN TableC on TableB.JoinColum = TableC.JoinColumn
INNER JOIN TableD on TableC.JoinColumn = TableD.JoinColumn
Note that the JoinColumn used to join A & B doesn't necesarilly have to be the same column as the one used to join B & C, and so on for C & D.
SELECT *
FROM TableA A
INNER JOIN TableB B ON B.?? = A.?? AND ...
LEFT JOIN TableC C ON C.?? = B.?? AND ...
LEFT JOIN TableB B2 ON B2.?? = C.?? AND ...
LEFT JOIN TableD D ON D.?? = C.?? AND ...
So here's the thing: logically, joins aren't actually between specific tables, they are between a table and the rest of the "set" (of joins and tables). So while you know that there is a 1-to-1 relationship between C and B2 or between C and D, you can't INNER JOIN to C because C could be null from it's LEFT JOIN to B, which will eliminate those rows, effectively undoing your LEFT join.
So basically, any joins to a table that's LEFT outer joined must also be LEFT outer joined. Does this make sense?