Reputation: 133
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.
Upvotes: 0
Views: 64
Reputation: 176
You can also use IN operator like this:
Select *
From TableA
Where ID in
(
Select distinct ID
From TableB
)
Upvotes: 1
Reputation:
this should work
SELECT B.ID
FROM TableA A
JOIN TableB B
ON (A.ID=B.ID)
WHERE A.ID=B.ID
Upvotes: 2
Reputation: 16917
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
.
Upvotes: 3