Sql join with multiple tables - sql

I have a table like this
mNumber DateTime
3005877639 22/03/2017 12:04:55 PM
3459145987 17/04/2017 10:32:00 AM
3334386694 18/04/2017 4:37:10 PM
i have ownership information of mNumber in 3 different tables i.e table1, table2 and table3 such that table1 has ownership information of 3005877639, table2 has 3459145987 and table 3 has information about 3334386694.
Now i want to retrieve ownership information of each number and join it with the above table. For some reason i cant combine the ownership information tables. Any help will be appreciated.

Try like below:
select * from table1 t1 inner join table2 t2 on t1.mNumber=t2.mNumber_column_name join table3 t3 on t1.mNumber=t3.mNumber_column_name where 1 //if you want you can more condition
if you want to know more about join check here

Ok, I got it. Just like xQbert commented, i simply "union all" all 3 tables and used that as right-table in Left outer join i.e.
Select a.*, b.Name,b.address from TableX as a Left Outer Join
(Select * fom Table1 union all Select * from Table2
union all Select * from Table3) as b on a.mNumber = b.mNumber
and it worked.
it is also worth mentioning that nested join doesn't work in this scenario.
Thanks to all who replied.

Related

What is the simplest way to join multiple tables in SQL?

I have been working as a data analyst for about 4 months now and the above is a very real question for me. The most recent way I've been taught to join is with the left join with the following example.
left join table1
on
table2.id = table1.id
left join table2
on
table3.table_id = table2.table_id
left join table4
on
table1.tablekey_id = table4.tablekey_id
Looking for the most efficient way to connect multiple tables to save time, if possible.
Thanks in Advance!
You could certainly use a shorter alias for the table names and table fields.
Example:
Table 1 = alias t1
tablekey_id = tkid
So then it would t1.tkid instead of having to type out table1.tablekey_id.
If it is only two tables of same columns and same datatypes, you can use union concept to join the tables.
You can also join tables like this:
Tables: TableA, TableB
SELECT
column1,
column2,
column3
FROM TableA, TableB
WHERE TableA.id = TableB.id;

How to select records that do not exist in two (or more) tables

I have 3 tables of accounts that all contain the same fields. Table1 contains all accounts while Table2 and Table3 contain subsets of the accounts. I'm trying to select records in Table1 that do no exist in Table2 or Table3.
Let's say the table layout is like this and is the same for all 3 tables:
|AcctNum|Name|State|
I know how to do this if it was just Table1 and Table2, using a left join and Is Null, but the 3rd table is throwing me. Is this possible to do in one query? Can you combine left joins? I should point out I'm using Access 2010.
Yes you can combine left joins and with the odd syntax Access uses the query should look like this:
SELECT T1.AcctNum
FROM (Table1 AS T1 LEFT JOIN Table2 AS T2 ON T1.AcctNum = T2.AcctNum)
LEFT JOIN Table3 AS T3 ON T1.AcctNum = T3.AcctNum
WHERE (((T2.AcctNum) Is Null) AND ((T3.AcctNum) Is Null));
You can use Access to create a view called TableCombined that is a union of both Table2 and Table3.
At that point, you can use your left join and Is Null query and join TableCombined to Table1.
Hope this helps!
You can also do a NOT EXISTS statement which makes sense logically for what you are trying to achieve.
For example:
SELECT ACCTNUM
FROM TABLE1
WHERE NOT EXISTS (SELECT TABLE2.ACCTNUM FROM TABLE2 INNER JOIN TABLE3 WHERE TABLE2.ACCTNUM IS NULL AND TABLE3.ACCTNUM IS NULL)

SQL - join multiple tables, with one little catch

My apologies in advance if this particular question has already been asked and answered ... there so many different particular ways the JOIN command is used that it can be difficult to find the exact answer to a given problem. I'm a SQL novice, so ... if the solution exists, feel free to point me to it.
I'm trying to join 3 different tables, and I believe what I want is equivalent to a FULL OUTER JOIN (not supported by MySQL, as I understand) on all 3 tables. Consider a Venn diagram with 3 circles; I want the full union of all 3 circles, including the full intersection, all three pair-wise joins (where one table returns NULLs), and all three single instances (where two tables return NULLs). I believe what I've got here will work, but it's brute-force and I'm sure there is a more efficient way. I'm also a bit concerned with my use of WHERE NOT EXISTS, so please correct me if necessary. Here's the gist of my code:
// Intersection of all three tables
SELECT [table1.cols], [table2.cols], [table3.cols]
FROM table1
INNER JOIN table2
ON table1.col1 = table2.col1
INNER JOIN table3
ON table1.col1 = table3.col1
UNION ALL
// Intersection of tables one and two
SELECT [table1.cols], [table2.cols], [NULLS]
FROM table1
INNER JOIN table2
ON table1.col1 = table2.col1
WHERE NOT EXISTS (table1.col1 = table3.col1)
UNION ALL
// Intersection of tables two and three
SELECT [NULLS], [table2.cols], [table3.cols]
FROM table2
INNER JOIN table3
ON table2.col1 = table3.col1
WHERE NOT EXISTS (table2.col1 = table1.col1)
UNION ALL
// Intersection of tables three and one
SELECT [table1.cols], [NULLS], [table3.cols]
FROM table3
INNER JOIN table1
ON table3.col1 = table1.col1
WHERE NOT EXISTS (table3.col1 = table2.col1)
UNION ALL
// Only in table one
SELECT [table1.cols], [NULLS], [NULLS]
FROM table1
WHERE NOT EXISTS ((table1.col1 = table2.col1))
AND NOT EXISTS ((table1.col1 = table3.col1))
UNION ALL
// Only in table two
SELECT [NULLS], [table2.cols], [NULLS]
FROM table2
WHERE NOT EXISTS ((table2.col1 = table1.col1))
AND NOT EXISTS ((table2.col1 = table3.col1))
UNION ALL
// Only in table three
SELECT [NULLS], [NULLS], [table3.cols]
FROM table3
WHERE (NOT EXISTS (table3.col1 = table1.col1))
AND (NOT EXISTS (table3.col1 = table2.col1))
TIA for your help, and your grace. :)
to simulate your full outer join, I would pre-query just the UNIQUE IDs you are EXPECTING, then LEFT JOIN to each other...
select
AllPossibleKeys.CommonID,
T1a.*,
T2a.*,
T3a.*
from
( select distinct T1.col1 as CommonID
from table1 T1
UNION
select distinct T2.col1 as CommonID
from table2 T2
UNION
select distinct T3.col1 as CommonID
from table1 T3 ) as AllPossibleKeys
LEFT JOIN table1 T1a
on AllPossibleKeys.CommonID = T1a.col1
LEFT JOIN table2 T2a
on AllPossibleKeys.CommonID = T2a.col1
LEFT JOIN table3 T3a
on AllPossibleKeys.CommonID = T3a.col1

Are "from Table1 left join Table2" and "from Table2 right join Table1" interchangeable?

For example, there are two tables:
create table Table1 (id int, Name varchar (10))
create table Table2 (id int, Name varchar (10))
Table1 data as follows:
Id Name
-------------
1 A
2 B
Table2 data as follows:
Id Name
-------------
1 A
2 B
3 C
If I execute both below mentioned SQL statements, both outputs will be the same:
select *
from Table1
left join Table2 on Table1.id = Table2.id
select *
from Table2
right join Table1 on Table1.id = Table2.id
Please explain the difference between left and right join in the above SQL statements.
Select * from Table1 left join Table2 ...
and
Select * from Table2 right join Table1 ...
are indeed completely interchangeable. Try however Table2 left join Table1 (or its identical pair, Table1 right join Table2) to see a difference. This query should give you more rows, since Table2 contains a row with an id which is not present in Table1.
Table from which you are taking data is 'LEFT'.
Table you are joining is 'RIGHT'.
LEFT JOIN: Take all items from left table AND (only) matching items from right table.
RIGHT JOIN: Take all items from right table AND (only) matching items from left table.
So:
Select * from Table1 left join Table2 on Table1.id = Table2.id
gives:
Id Name
-------------
1 A
2 B
but:
Select * from Table1 right join Table2 on Table1.id = Table2.id
gives:
Id Name
-------------
1 A
2 B
3 C
you were right joining table with less rows on table with more rows
AND
again, left joining table with less rows on table with more rows
Try:
If Table1.Rows.Count > Table2.Rows.Count Then
' Left Join
Else
' Right Join
End If
You seem to be asking, "If I can rewrite a RIGHT OUTER JOIN using LEFT OUTER JOIN syntax then why have a RIGHT OUTER JOIN syntax at all?" I think the answer to this question is, because the designers of the language didn't want to place such a restriction on users (and I think they would have been criticized if they did), which would force users to change the order of tables in the FROM clause in some circumstances when merely changing the join type.
select fields
from tableA --left
left join tableB --right
on tableA.key = tableB.key
The table in the from in this example tableA, is on the left side of relation.
tableA <- tableB
[left]------[right]
So if you want to take all rows from the left table (tableA), even if there are no matches in the right table (tableB), you'll use the "left join".
And if you want to take all rows from the right table (tableB), even if there are no matches in the left table (tableA), you will use the right join.
Thus, the following query is equivalent to that used above.
select fields
from tableB
right join tableA on tableB.key = tableA.key
Your two statements are equivalent.
Most people only use LEFT JOIN since it seems more intuitive, and it's universal syntax - I don't think all RDBMS support RIGHT JOIN.
I feel we may require AND condition in where clause of last figure of Outer Excluding JOIN so that we get the desired result of A Union B Minus A Interaction B.
I feel query needs to be updated to
SELECT <select_list>
FROM Table_A A
FULL OUTER JOIN Table_B B
ON A.Key = B.Key
WHERE A.Key IS NULL AND B.Key IS NULL
If we use OR , then we will get all the results of A Union B
select *
from Table1
left join Table2 on Table1.id = Table2.id
In the first query Left join compares left-sided table table1 to right-sided table table2.
In Which all the properties of table1 will be shown, whereas in table2 only those properties will be shown in which condition get true.
select *
from Table2
right join Table1 on Table1.id = Table2.id
In the first query Right join compares right-sided table table1 to left-sided table table2.
In Which all the properties of table1 will be shown, whereas in table2 only those properties will be shown in which condition get true.
Both queries will give the same result because the order of table declaration in query are different like you are declaring table1 and table2 in left and right respectively in first left join query, and also declaring table1 and table2 in right and left respectively in second right join query.
This is the reason why you are getting the same result in both queries. So if you want different result then execute this two queries respectively,
select *
from Table1
left join Table2 on Table1.id = Table2.id
select *
from Table1
right join Table2 on Table1.id = Table2.id
Select * from Table1 t1 Left Join Table2 t2 on t1.id=t2.id
By definition: Left Join selects all columns mentioned with the "select" keyword from Table 1 and the columns from Table 2 which matches the criteria after the "on" keyword.
Similarly,By definition: Right Join selects all columns mentioned with the "select" keyword from Table 2 and the columns from Table 1 which matches the criteria after the "on" keyword.
Referring to your question, id's in both the tables are compared with all the columns needed to be thrown in the output. So, ids 1 and 2 are common in the both the tables and as a result in the result you will have four columns with id and name columns from first and second tables in order.
*select *
from Table1
left join Table2 on Table1.id = Table2.id
The above expression,it takes all the records (rows) from table 1 and columns, with matching id's from table 1 and table 2, from table 2.
select *
from Table2
right join Table1 on Table1.id = Table2.id**
Similarly from the above expression,it takes all the records (rows) from table 1 and columns, with matching id's from table 1 and table 2, from table 2. (remember, this is a right join so all the columns from table2 and not from table1 will be considered).

Is it possible to restrict the results of an outer join?

I've got a scenario where I need to do a join across three tables.
table #1 is a list of users
table #2 contains users who have trait A
table #3 contains users who have trait B
If I want to find all the users who have trait A or trait B (in one simple sql) I think I'm stuck.
If I do a regular join, the people who don't have trait A won't show up in the result set to see if they have trait B (and vice versa).
But if I do an outer join from table 1 to tables 2 and 3, I get all the rows in table 1 regardless of the rest of my where clause specifying a requirement against tables 2 or 3.
Before you come up with multiple sqls and temp tables and whatnot, this program is far more complex, this is just the simple case. It dynamically creates the sql based on lots of external factors, so I'm trying to make it work in one sql.
I expect there are combinations of in or exists that will work, but I was hoping for some thing simple.
But basically the outer join will always yield all results from table 1, yes?
SELECT *
FROM table1
LEFT OUTER
JOIN table2
ON ...
LEFT OUTER
JOIN table3
ON ...
WHERE NOT (table2.pk IS NULL AND table3.pk IS NULL)
or if you want to be sneaky:
WHERE COALESCE(table2.pk, table3.pk) IS NOT NULL
but for you case, i simply suggest:
SELECT *
FROM table1
WHERE table1.pk IN (SELECT fk FROM table2)
OR table1.pk IN (SELECT fk FROM table3)
or the possibly more efficient:
SELECT *
FROM table1
WHERE table1.pk IN (SELECT fk FROM table2 UNION (SELECT fk FROM table3)
If you really just want the list of users that have one trait or the other, then:
SELECT userid FROM users
WHERE userid IN (SELECT userid FROM trait_a UNION SELECT userid FROM trait_b)
Regarding outerjoin specifically, longneck's answer looks like what I was in the midst of writing.
I think you could do a UNION here.
May I suggest:
SELECT columnList FROM Table1 WHERE UserID IN (SELECT UserID FROM Table2)
UNION
SELECT columnList FROM Table1 WHERE UserID IN (SELECT UserID FROM Table3)
Would something like this work? Keep in mind depending on the size of the tables left outer joins can be very expensive with regards to performance.
Select *
from table1
where userid in (Select t.userid
From table1 t
left outer join table2 t2 on t1.userid=t2.userid and t2.AttributeA is not null
left outer join table3 t3 on t1.userid=t3.userid and t3.AttributeB is not null
group by t.userid)
If all you want is the ids of the users then
SELECT UserId From Table2
UNION
SELECT UserId From Table3
is totally sufficient.
If you want some more infos from Table1 on these users, you can join the upper SQL to Table 1:
SELECT <list of columns from Table1>
FROM Table1 Join (
SELECT UserId From Table2
UNION
SELECT UserId From Table3) User on Table1.UserID = Users.UserID