Having trouble with this simple SQL Select statement - sql

I have two tables, A and B.
Both have the exact same columns.
I need to select all the items in TableA that ARE NOT in TableB.
This is intersection, right? How can I do this?

assuming TableA and TableB have a primary key of name id.
select TableA.*
from TableA
left outer join TableB on TableB.id = TableA.id
where TableB.id is null;
This will find all entries where table b does not have an instance of table a's id.

You could use the EXISTS clause
SELECT * FROM TableA
WHERE NOT Exists
(
SELECT Column1 FROM TableB
WHERE TableA.Column1 = Table2.Column1
AND TableA.Column2 = Table2.Column2
....
)
Replace .... with the rest of the columns in the two tables.

SELECT ColumnA, ColumnB
FROM TableA
EXCEPT
SELECT ColumnA, ColumnB
FROM TableB

You have your terminology wrong. The intersection would be the rows that are in both Table A and Table B. What you are actually looking for is the relative complement of A and B. To get a relative complement you want to do an antijoin:
SELECT * FROM TableA EXCEPT SELECT * FROM TableB.

or NOT IN
SELECT *
FROM TableA
WHERE TableA.Id NOT IN (SELECT TableB.Id FROM TableB)

Related

SQL how to insert and left join

name table a
1.budy
2.rendy
3.rona
4.sandi
5.susi
table b
1.budy
2.rendy
3.rona
how I can show on table b sandi and susi from table a with sql?
To test: http://rextester.com/HDZ43705
This will do what you're looking for:
select * from A
except
select * from B
Try It:-
INSERT INTO tableb
SELECT tablea.id,tablea.name,tableb.id,tableb.name
FROM tablea
LEFT JOIN tableb
ON tablea.name = tableb.name;
You can check the existence of data in TableA using LEFT JOIN or NOT EXISTS which is not exist in TableB and then insert in TableB
INSERT INTO tableb(name)
SELECT A.name
FROM tableA A
LEFT JOIN tableB B ON B.name = A.name
WHERE B.name IS NULL
OR
SELECT A.name
FROM tableA A
WHERE NOT EXISTS(SELECT 1 FROM tableB B WHERE B.name = A.name)
Use this
INSERT INTO tableb
SELECT tablea.id,tablea.name FROM dbo.tablea
LEFT JOIN dbo.tableb
ON tablea.id=tableb.id
WHERE tableb.id is null
Might not be the most optimized solution, but guess it will work:
SELECT name
FROM TableB
UNION
SELECT name
FROM TableA
Will return a DISTINCT version of the data in both tables, if that was what you were looking for (a joined view of the tables).
If you only want to show the names in A that are not in B, then something like this:
SELECT name FROM TableA WHERE name NOT IN (SELECT name FROM TableB)
I think this is essentially the same soution that DEEPAK suggested, only his was more compact and modern.

Join of Two Tables where Data Matches in One Column

For some reason I have a hard time grasping joins and this one should be very simple with the knowledge that I have in SQL.
Anyway, I have 2 tables. We will call them TableA and TableB. One of the columns in TableA is "ID". TableB only consists of the column "ID". I want to return all rows in TableA whose ID is present in TableB.
I know this should be very simple to figure out, but my brain doesn't want to work today.
You can do this using an EXISTS:
Select A.*
From TableA A
Where Exists
(
Select *
From TableB B
Where A.Id = B.Id
)
You can also use a JOIN if you wish, but depending on your data, you may want to couple that with a SELECT DISTINCT:
Select Distinct A.*
From TableA A
Join TableB B On A.Id = B.Id
One thing to keep in mind is that the ID of TableA is not necessarily related to the ID of TableB.
this should work
SELECT B.ID
FROM TableA A
JOIN TableB B
ON (A.ID=B.ID)
WHERE A.ID=B.ID
You can also use IN operator like this:
Select *
From TableA
Where ID in
(
Select distinct ID
From TableB
)

How to filter table with conditions stored as parameters in another table?

I want to use parameters stored in tableA to filter tableB.
Here is my tableA with parameters:
I want to filter tableB with more or less such a query:
WITH A AS
(SELECT
[FilterType]
,[MaxID]
FROM TableA
WHERE [FilterType]=1
)
SELECT * FROM TableB B
WHERE B.ID>A.MaxID
I want to get all the records from TableB where B.ID is larger than MaxID chosen from TableA for a FilterType 1. How to do it? Speaking more generally, how to get a parameter from table and use this parameter for query?
Not a lot of detail here but something like this?
select b.Columns
from TableB b
join TableA a on a.MaxID <= b.ID
where a.FilterType = 1
SELECT B.*
FROM TableB AS B
JOIN TableA AS A ON B.ID > A.MaxID
WHERE A.FilterType = 1
WITH A AS
(SELECT
[FilterType]
,[MaxID]
FROM TableA
WHERE [FilterType]=1
)
SELECT B.* FROM TableB B,A
WHERE B.ID>A.MaxID

Delete distinct multiple columns in sql

I have Query in Access that I'm building in SQL Server.
Access:
DELETE DISTINCT * from [TableA] INNER JOIN TableB
ON [TableA].[Column1]=[TableB].[column1]
AND [TableA].[Column2]=[TableB].[column2]
I know I could use
Delete from tableA where ID in (
Select * from from [TableA] INNER JOIN TableB
ON [TableA].[Column1]=[TableB].[column1]
AND [TableA].[Column2]=[TableB].[column2])
But I get an error saying "Only one expression can be specified in the select list when the subquery is not introduced with EXISTS"
My Goal is to delete the Distinct records from the Access query mentioned at the top.
You want to delete the rows in TableA that are in TableB, according to the column matches. How about doing this:
delete from tableA
where exists (select 1
from tableB
where tableA.Column1 = tableB.Column1 and tableA.column2 = tableB.column2
);
This seems to be the intent of what you are trying to do.
In the sub-query u have to select the ID column from the respective table that is the only column u need
DELETE a
FROM tableA a
JOIN (SELECT DISTINCT Column1 ,column2
FROM tableA
WHERE EXISTS (SELECT 1
FROM tableB
WHERE tableA.Column1 = tableB.Column1
AND tableA.column2 = tableB.column2)) b
ON A.Column1 = B.Column1
AND A.column2 = B.column2

Retrive Records Form One Table as long as they do not exist in Another table T-SQL [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What's the difference between NOT EXISTS vs. NOT IN vs. LEFT JOIN WHERE IS NULL?
I need to wite a query that will retrieve the records from Table A , provided that the key in Table A does not exist in Table B.
Any help will be appreciated.
Thanks
select a.*
from
tableA a
left join tableB b
ON a.id = b.id
where
b.id is null
SELECT *
FROM A
WHERE ID NOT IN
(SELECT ID FROM B)
None of the above solutions would work if the key comprises of multiple columns.
If the tables have compound primary keys, you'll have to use a "NOT EXISTS" clause similar to the one below.
SELECT *
FROM TableA AS a
WHERE NOT EXISTS (
SELECT *
FROM TableB b
WHERE b.id1 = a.id1
AND b.id2 = a.id2
AND b.id3 = a.id3
);
Use a left join. The DB tries to map datasets from TableB to TableA using the id fields. If there is no fitting data set available in TableB, the TableB data gets NULL. Now you just have to check for TableB.id to be NULL.
SELECT TableA.* FROM TableA LEFT JOIN TableB ON TableA.id = TableB.id WHERE TableB.id IS NULL
Assuming:
TableA's Id = Id
TableB's Id = Id
select * from TableA ta where ta.Id not in (select Id from TableB)
SELECT * FROM TableA
WHERE NOT Exists(SELECT * FROM TableB WHERE id=TableA.id)
also works and it's almost self documenting...