Multiple inner join with multiple where clause - inner-join

i want to ask how to do multiple inner join with multiple where clause
for exemlpe i want to select records from tab1, tab2, tab3 and tab4
and make inner join between reuslt tables
thank you

U can try to either inner join tab1 to tab4 and then select the rows or you could try something like:
SELECT * FROM (SELECT rowsFromTab1 FROM Tab1 WHERE whereClauseForTab1) INNER JOIN (SELECT rowsFromTab2 FROM Tab2 WHERE whereClauseForTab2)

Related

Simple joining two tables with group by

I have 2 tables (pics 1,2). There are 2 columns in tab1 and many in tab2. There is ID column which is same for every record in both tables. I need output (pic 3) with every column from both tables grouped by ID. Left or right join doesn't really matter. I just need match records by ID and have every column of both tables listed.
Thank you.
1 https://i.stack.imgur.com/1OmDT.png
2 https://i.stack.imgur.com/nShTy.png
3 https://i.stack.imgur.com/vLIFm.png
You can simply use this simple query :
select a.ID, b.name, b.code, a.quantity
from Tab1 a
inner join Tab2 b on a.ID = b.ID
group by a.ID, b.name, b.code, a.quantity
order by a.ID
and you can learn more from here https://www.w3schools.com/sql/sql_join.asp
select tab1.id,name,code, quantity from tab1 join tab2 using(id);
The JOINs(INNER OR LEFT) will change in the below query depending on whether you need to show the ID info irrespective you have any quantity(NULL or 0 using left join) for it in tab2 or only show the quantity for matching IDs(INNER join) there.
SELECT
t1.*,
SUM(t2.quantity) AS quantity
FROM
tab1 AS t1
INNER JOIN tab2 AS t2 ON t1.id = t2.id -- CHANGE THIS TO LEFT JOIN IF NEED
GROUP BY
t1.id,
t1.name,
t1.code

sql server join for nonmatch

what ever BU is present in table-2 that should be present in table-1 BU so write a query to find out the records available in table-2 whose BU is not available in table-1 BU.
Try to use left join where Table2 is left table so that you will get all the records from Table2 and matching records from Table1. And for those records from Table2 there is no match, result will shoe NULL entries for Table1. That's it-- that's what you are looking for --
SELECT T2.BU FROM Table2 T2
LEFT OUTER JOIN Table1 T1
ON T2.BU = T1.BU
WHERE T1.BU IS NULL
SELECT DISTINCT T2.BU FROM Table2 T2
LEFT OUTER JOIN Table1 T1
ON T2.BU = T1.BU
WHERE T1.BU IS NULL

The column "xxxx" was specified multiple times for "CTE_YYYY"

I am new to Microsoft SQL Server. I am trying to join two tables that has common key named CampaignID using LEFT OUTER JOIN. I need to reuse the result in a different query, so I decided to capture the result set using CTE_Results. For example,
-- This is my CTE script
WITH CTE_Results AS
(
SELECT t1.CampaignID, t2.CampaignID, t1.Name, t2.Vendor
FROM CampaignDetails AS t1
LEFT OUTER JOIN CampaignOnlineDetails AS t2
ON t1.CampaignID = t2.CampaignID
)
-- This is the script I want to use to compare the resulting table. For example,
SELECT Vendor
FROM CTE_Results
However, when I ran above, I get:
The column `CampaignID` was specified multiple times for `CTE_Results`.
From reading through old StackOverflow questions and answers, it seems like since CampaignID is in both tables that are being joined, I must use table aliases to specify whose (which table's) CampaignID I want to SELECT. But I think I did that and even that it seems like the error still occurs.
Is there a way for me to select and keep BOTH CampaignID's in my CTE? If so, what should be changed? Thank you for the answers!
You have CampaignID selected twice in CTE, use different alias name to fix the problem
WITH CTE_Results
AS (SELECT t1.CampaignID AS cd_CampaignID,
t2.CampaignID AS cod_CampaignID,
t1.NAME,
t2.Vendor
FROM CampaignDetails AS t1
LEFT OUTER JOIN CampaignOnlineDetails AS t2
ON t1.CampaignID = t2.CampaignID)
-- This is the script I want to use to compare the resulting table. For example,
SELECT Vendor
FROM CTE_Results
or use this
WITH CTE_Results(cd_CampaignID, cod_CampaignID, NAME, Vendor)
AS (SELECT t1.CampaignID,
t2.CampaignID,
t1.NAME,
t2.Vendor
FROM CampaignDetails AS t1
LEFT OUTER JOIN CampaignOnlineDetails AS t2
ON t1.CampaignID = t2.CampaignID)
-- This is the script I want to use to compare the resulting table. For example,
SELECT Vendor
FROM CTE_Results
You need to Alias the CampaignID Columns in your CTE or define the returned column names in the CTE declaration. Otherwise it would be like creating a table with two columns with the same name.
Example Column Alias:
WITH CTE_Results AS
(
SELECT t1.CampaignID as 'CampaignID1', t2.CampaignID as 'CampaignID2', t1.Name, t2.Vendor
FROM CampaignDetails AS t1
LEFT OUTER JOIN CampaignOnlineDetails AS t2
ON t1.CampaignID = t2.CampaignID
)
Or In CTE declaration:
WITH CTE_Results (CampaignID1, CampaignID2, [Name], Vendor) AS
(
SELECT t1.CampaignID, t2.CampaignID , t1.Name, t2.Vendor
FROM CampaignDetails AS t1
LEFT OUTER JOIN CampaignOnlineDetails AS t2
ON t1.CampaignID = t2.CampaignID
)

Is the order of multiple INNER JOIN-s related with the tables relashionship?

I want to join some tables using INNER JOIN statement.Some of them have the many to many relashionship ,some one to many and some one to one. I want to know does the order of INNER JOIN-s statement matters and is it related with the type of relashionship(One to one,one to many etc.)? So does these three codes below output the same result?
SELECT ....
FROM table1
INNER JOIN (table2 INNER JOIN table3 ON table2.col=table3.col)
ON table1.col=table2.col
SELECT ....
FROM table1
INNER JOIN (table2 INNER JOIN table3 ON table2.col=table3.col)
ON table1.col=table3.col
SELECT ....
FROM table2
INNER JOIN (table1 INNER JOIN table3 ON table1.col=table3.col)
ON table3.col=table2.col
And can I replace the INNER JOIN of two tables with this code below?So does this code below represents the inner join of table 1 and table2?
SELECT ...
FROM table1,table2
WHERE (table1.col=table2.col)
Exactly order of joins is not matter.
Better to use
select ...
from table1
inner join table2 on table2.col=table1.col
inner join table3 on table3.col=table1.col
Yes, INNER JOIN's could be replaced with
WHERE t1.col=t2.col
And SQL plan will be the same.
But if there are other filters in where condition - will mix.
Also, if there is additional join conditions - better to filter out all not required records first.
It makes absolutely no difference to the results. Because you are using only inner joins, only the matches in all three tables will show.
If you were to use a LEFT OUTER join and an INNER join in one query, you could vary the resultsets.
For INNER JOIN it will give same result but with other type of join it will give different result.
for example:
SELECT ....
FROM table1
LEFT JOIN (table2 INNER JOIN table3 ON table2.col=table3.col)
ON table1.col=table2.col
above is equivalent to
SELECT ....
FROM table1
LEFT JOIN (
Select table2.col
From table2 INNER JOIN table3 ON table2.col=table3.col
) tbl
ON table1.col=tbl.col
First it will do INNER JOIN of table2 & table3 then the table1 will left joined with the result

The effect of a select with multiple tables in FROM is the same as INNER JOIN but what is the ON clause then?

I have a query like this:
SELECT
*
FROM
table1,
table2
I know this is somewhat equivalent to:
SELECT
*
FROM
table1
INNER JOIN
table2
ON ???
However, what would be the resulting ON clause for the join?
Update
After some testing in SSMS here are my findings
SELECT * FROM table1,table2
gives the same execution plan and the same records as
SELECT * FROM table1 INNER JOIN table2 ON 1=1
and the same thing for
SELECT * FROM table1 CROSS JOIN table2
the column that defines their relationship.
SELECT *
FROM table1
INNER JOIN table2
ON table1.ID = table2.ID
actually the query you have showed is not equal. The first one produces cartesian product of all the records on both table or in other words CROSS JOIN.
SELECT
*
FROM
table1,
table2
is equivalent to:
SELECT
*
FROM
table1
CROSS JOIN
table2
there is no ON statement with a CROSS JOIN. If you need to filter a CROSS JOIN, put it in the WHERE clause.
WHERE table1.DateCreated <= table2.DateModified
After some testing in SSMS here are my findings
SELECT * FROM table1,table2
gives the same execution plan and the same records as
SELECT * FROM table1 INNER JOIN table2 ON 1=1
and the same thing for
SELECT * FROM table1 CROSS JOIN table2

Resources