sql server join for nonmatch - sql-server

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

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

Does this query working like cross join?

I am trying to run this query but couldn't understand its working process.
SELECT *
FROM TABLE1 T1
INNER JOIN TABLE2 T2 ON T1.ID = T2.ID
LEFT JOIN TABLE1 T3 ON T1.ID = T2.ID
table1 contains 3 records with sequential id 1, 2, 3 and table2 contains 4 records with sequential id 1, 2, 3, 4
and one another thing i also want to know that is, does this query executing from right to left? i mean left join process executes first then inner join? i am saying this according to query execution plan.
Here is your current query:
SELECT *
FROM table1 t1
INNER JOIN table2 t2
ON t1.id = t2.id
LEFT JOIN table1 t3
ON t1.id = t2.id
The first join is just a normal inner join between table1 and table2. The second join is a left join, but the ON condition is superfluous, and I believe the behavior would be the same without this condition even being there. The reason for this is that the join condition t1.id = t2.id will already always be true at that point in the query, for every record in the intermediate table. Hence, it appears that the second join would effectively be a cross join with table1.
Typically, your join condition will involve the two tables being joined.
Yes it's working like a cross join. If this is the intention you should rewrite it correctly. What you have there is misleading and confusing (where did it come from originally?)
select * from table1 t1
inner join table2 t2
on t1.id=t2.id
cross join table1 t3
The order that tables, filters, joins are evalulated are dictated by the query plan (press CTRL-L). This may change at any time. You shouldn't be concerned about the ordered these run in - you just need to know that you will get the same results no matter how it is executed. The query planner might choose one method over the other if it thinks it will be faster

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

most efficient way to quickly delete records using a join?

What is the fastest way to delete records from a SQL Server database for a large year-end purge, when the query has to join to another table? I understand cursors are slow. Do I do
DELETE FROM table1 WHERE table1_id in (SELECT table1_id FROM table2 WHERE whatever)
Seems like it might be faster to join to the table inside the query using a different technique, something like
DELETE FROM table1 WHERE table1_id = table2
A join would be the best way to go.
I assume you have a foreign key association from table2 to table1, so you're going to want to delete from the second table before the primary table.
Example:
DELETE T2
FROM table1 T1
JOIN table2 T2 ON -- JOIN CRITERIA HERE
WHERE -- FILTER CRITERIA HERE
DELETE T1
FROM table1 T1
JOIN table2 T2 ON -- JOIN CRITERIA HERE
WHERE -- FILTER CRITERIA HERE
If you attempt to delete from the primary table first, you will likely encounter a foreign key constraint violation.
Could you add a cascade delete relationship on the foreign key then get SQL server to cascade the delete for you?
Maybe something like this would be faster.
delete from (select * FROM table1 WHERE table1_id in (SELECT table1_id FROM table2 WHERE whatever))
You can try this (should be faster than IN statement, because query stops than find first value in subquery and search next value):
DELETE t1
FROM table1 t1
WHERE EXISTS (SELECT 0
FROM table2 t2
WHERE t1.table1_id = t2.table1_id
AND whatever)
Or you can do JOIN:
DELETE t1
FROM table1 t1
JOIN table2 t2 ON t1.table1_id = t2.table1_id
WHERE whatever

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