SQL Join multiple tables using where clause - sql

I want to write a sql statement:
Select * FROM table1
inner join table2 AS t2
ON
inner join table3 AS t3
ON
inner join table4 AS t4
ON
Where FK_Client_ID = 4
All tables have the client ID in common. So Not sure what to do on the ON.
Will it be something like ON t2.FK_Client_ID = ...... not sure.
So I just want all the data from those tables that has FK_Client_ID in common.

Select *,
(Select FK_Client_ID from table2 where FK_Client_ID = t1.FK_Client_ID )As TID1,
(Select FK_Client_ID from table3 where FK_Client_ID = t1.FK_Client_ID )As TID2,
(Select FK_Client_ID from table4 where FK_Client_ID = t1.FK_Client_ID )As TID3
FROM table1 t1

Select * FROM table1 t1 inner join table2 t2
ON t1.FK_Client_ID = t2.FK_Client_ID
inner join table3 t3
ON t1.FK_Client_ID = t3.FK_Client_ID
inner join table4 t4
ON t1.FK_Client_ID = t4.FK_Client_ID
Where t1.FK_Client_ID = 4

Try this
Select * FROM table1 t1
inner join table2 AS t2
ON t2.FK_Client_ID = t1.FK_Client_ID
inner join table3 AS t3
ON t3.FK_Client_ID = t1.FK_Client_ID
inner join table4 AS t4
ON t4.FK_Client_ID = t1.FK_Client_ID
Where t1.FK_Client_ID = 4

if your foreign field name is "FK_Client_ID" and primary key in table1 is "Client_ID"
Select * FROM table1
inner join table2 AS t2
ON t2.FK_Client_ID = table1.Client_ID
inner join table3 AS t3
ON t3.FK_Client_ID = table1.Client_ID
inner join table4 AS t4
ON t4.FK_Client_ID = table1.Client_ID
Where table1.Client_ID = 4
Since you are using inner join no matter you join them with table1 or any other of them

Related

I need to combine two tables and then joiing with 10 more tables in SQL

I have select SQL query in vb.net as below to fetch record. Except for table1 & table1history all other conditions are seems to be same. Is there any way to place the filtered date from table1 & table2 into a temp table and perfom multiple join only once against the temp ?
select
colA, colB, ...
from
table1
join
table2 on tabel1.col = table2.col
join
table3 on tabel3.col = table2.col
join
table4 on tabel4.col = table2.col
join
table5 on tabel5.col = table2.col
join
table6 on tabel6.col = table5.col
left join
table7 on tabel7.col = table5.col
Left join
table8 on tabel8.col = table4.col
Left join
table9 on tabel9.col = table8.col
Left join
table10 on tabel10.col = table5.col
union
select
colA, colB, ...
from
table1History
join
table2 on tabel1History.col = table2.col
join
table3 on tabel3.col = table2.col
join
table4 on tabel4.col = table2.col
join
table5 on tabel5.col = table2.col
join
table6 on tabel6.col = table5.col
left join
table7 on tabel7.col = table5.col
Left join
table8 on tabel8.col = table4.col
Left join
table9 on tabel9.col = table8.col
Left join
table10 on tabel10.col = table5.col;

How to replace an OR statement from a join in sql server

I have the following query that uses an or statement on a join, so basically if one condition on the join isn't met it must check the next condition. The problem is that with the OR statement it takes really long to run but when I remove one of the OR conditions it runs instantly. is there a better way to do this with both conditions without using the OR statement so it would speed up the query
select t5.TransactionNumber
,t4.ID
,t3.[Entry] AS Amount
,t2.Address AS AddressDetail
,t1.PhoneNumber AS ContactNumber
FROM Table1 t1 (NOLOCK)
JOIN Table2 t2 (NOLOCK) ON t2.FicaID = t1.FicaId
inner join Table3 t3 (NOLOCK) ON (t3.ID = t2.ID AND t3.Code = t2.Code) or (t3.TypeID = t2.TypeID) //on this join i have an or statement if one condition isnt met it must check the next condition
LEFT JOIN Table4 t4 (NOLOCK) ON t4.Result = t3.Result
LEFT JOIN Table5 t5 (NOLOCK) ON t5.AccNum = t3.AccNum
where t1.date>'2018-09-01' and t1.date<'2018-09-30'
By the rule of distributivity in logic,
P OR (Q AND R) can be written as
(P OR Q) AND (P OR R).. maybe that helps?
You could try by using left join and COALESCE function
select t5.TransactionNumber
,t4.ID
,COALESCE(t3.[Entry],t33.[Entry]) AS Amount
,t2.Address AS AddressDetail
,t1.PhoneNumber AS ContactNumber
FROM Table1 t1 (NOLOCK)
JOIN Table2 t2 (NOLOCK) ON t2.FicaID = t1.FicaId
left join Table3 t3 (NOLOCK) ON (t3.ID = t2.ID AND t3.Code = t2.Code)
left join Table3 t33 (t33.TypeID = t2.TypeID) //I moved it to left join
LEFT JOIN Table4 t4 (NOLOCK) ON t4.Result = t3.Result
LEFT JOIN Table5 t5 (NOLOCK) ON t5.AccNum = t3.AccNum
where t1.date>'2018-09-01' and t1.date<'2018-09-30'
You can try below query :
select * from
(
select t5.TransactionNumber
,t4.ID
,t3.[Entry] AS Amount
,t2.Address AS AddressDetail
,t1.PhoneNumber AS ContactNumber
FROM Table1 t1 (NOLOCK)
JOIN Table2 t2 (NOLOCK) ON t2.FicaID = t1.FicaId
inner join Table3 t3 (NOLOCK) ON (t3.ID = t2.ID AND t3.Code = t2.Code)
)A
join Table3 t3 (NOLOCK) ON (A.TypeID = t3.TypeID)
LEFT JOIN Table4 t4 (NOLOCK) ON t4.Result = t3.Result
LEFT JOIN Table5 t5 (NOLOCK) ON t5.AccNum = t3.AccNum
where t1.date>'2018-09-01' and t1.date<'2018-09-30'

How to join between three tables using a subquery?

I have 3 SQL tables:
tb1: name id and phone of student
tb2: courseName,courseNum
tb3: id,courseNum,grade
I need to find the name and courseName of student that their grade>80
I tried:
select * from tb1 where taz in(
select taz from tb3 where grade>80
)
and
select courseNum from tb2 where courseName in(
select * from tb3 where grade>80
)
It works but how can I join the 2 subquery?
Try using join as below:
SELECT *
FROM tb3 t3 INNER JOIN tb2 t2
ON t3.courseNum = t2.courseNum
INNER JOIN tb1 t1
ON t2.id = t1.id
WHERE t3.grade > 80
Try this :
SELECT c.name, b.courseName, a.grade
FROM tb3 a
JOIN tb2 b ON a.courseNum = b.courseNum
JOIN tb1 c ON a.id = c.id
WHERE a.grade > 80
Following query will give you the required result:
SELECT t1.Name, t2.CourseName, t3.grade from tb3
INNER JOIN tb2 t2 ON t3.CourseNum = t2.CourseNum
INNER JOIN tb1 t1 ON t1.id = t3.Id
where t3.grade > 80
SMA answer is true, but need little modification.
first: condition on second ON should be: t3.id = t1.id
second: we want name and courseName.
SELECT t1.name, t2.coursename
FROM tb3 t3 INNER JOIN tb2 t2
ON t3.courseNum = t2.courseNum
INNER JOIN tb1 t1
ON t3.id = t1.id
WHERE t3.grade > 80

How to join 3 tables in sql with or condition

I have 3 tables:
table1
inner join with table2; or:
inner join with table3
The query:
select table1.x
from table1 right outer join
table2
on table1.x = table2.x right outer join
table3
on table1.x = table3.x
but I can only see x values that are in both table2 and table3
Use left join, not right join, along with an appropriate filter condition:
select table1.x
from table1 left join
table2
on table1.x = table2.x left join
table3
on table1.x = table3.x
where table2.x is not null or table3.x is not null;
You might consider writing this using exists:
select t1.*
from table1 t1
where exists (select 1 from table2 t2 where t2.x = t1.x) or
exists (select 1 from table3 t3 where t3.x = t1.x);
That seems like a more natural way to implement your logic -- and you won't get duplicates when x is duplicated in table2 or table3.

Joining tbl1 to select statement twice with join to tbl2 that also joins to tbl3

I'm using SQL server manger.
I have 3 tables
I need a query that pulls t1 ands add an Origin Basin and a Destination Basin.
So far I have the following:
select T1.[Country (destination)], T3.AreaName
From T1
left outer join T2 on
T1.[Country (destination)] = T2.CountryName
inner join T3 on
T2.AreaID = T3.AreaID
inner join T3 on
T2.AreaID = T3.AreaID
Which returns:
Country | Area
However, I'm having trouble doing this for the second country column. I believe you use aliases. I've tried:
select (select AreaName
FROM T3
where T3.AreaID = T2.AreaID) as 'Area Imp',
(select AreaID
From T2
where T2.CountryName = T1.[Country (origin)]) as 'x',
(select AreaID
From T2
where T2.CountryName = T1.[Country (destination)]) as 'y'
FROM T1
But I can't get it to work.
This is what you need to do:
select t1.date, t1.country_destination, t1.country_origin, destination_area.AreaName as area_destination, origin_area.AreaName as area_origin
from t1 as t1 join t2 as destination on t1.country_destination = destination.countryname
join t2 as origin on t1.country_origin = origin.countryname
join t3 as destination_area on t2.areaid = destination_area.areaid
join t3 as origin_area on t2.areaid = origin_area.areaid
You will need to join with the same table twice, both for t2 and t3 so that you get the matching records for your needs.
It helps usually to put aliases that match the purpose of the join (in this case, destination and origin) when writing the query.
I think what you're trying to do is something like this:
select T1.*, T3dest.AreaName, T3orig.AreaName
From
T1
inner join
T2 T2dest on
T1.[Country (destination)] = T2dest.CountryName
inner join
T3 T3dest on
T2dest.AreaID = T3dest.AreaID
inner join
T2 T2orig on
T1.[Country (origin)] = T2orig.CountryName
inner join
T3 T3orig on
T2orig.AreaID = T3orig.AreaID
Note that I've switched to inner joins throughout, at the moment. If you do want left join semantics, you either need to use those for all of the joins to the T2 and T3 tables or you need to change the join order (so that the relevant T3 joins to the T2 tables occur before the attempted join with T1). It's not clear from the sample data if that's required, however.
Try this, You would still want to join on area id's
select T1.Date,T1.[Country (destination)], null [Country (origin)], T3.AreaName [AreaName(Destination)], null [AreaName(Origin)]
From T1
left outer join T2 on
T1.[Country (destination)] = T2.CountryName
inner join T3 on
T2.AreaID = T3.AreaID
union all
select T1.Date,null [Country (destination)], t1.[Country (origin)], Null [AreaName(Destination)], t3. [AreaName(Origin)]
From T1
left outer join T2 on
T1.[Country (Origin)] = T2.CountryName
inner join T3 on
T2.AreaID = T3.AreaID