niksrb
niksrb

Reputation: 459

Filter on the column on which two tables are joined

Are next two queries going to return same result set?

SELECT * FROM tableA a
JOIN tableB b
ON a.id = b.id
WHERE a.id = '5' 
--------------------------------
SELECT * FROM tableA a
JOIN tableb b
ON a.id = b.id
WHERE b.id = '5' 

Also, will answer be different if LEFT JOIN is used instead of JOIN?

Upvotes: 0

Views: 42

Answers (3)

kanataki
kanataki

Reputation: 433

Yes they will. A simple join works like an inner join by default. It checks for instances where the item you're joining on exist on both tables. Since you're joining on where a.id=b.id the results will be the same.

If you change the type of join to a left, the results will include all a.id's regardless of whether they are equal to 5.

Upvotes: 0

Hubii
Hubii

Reputation: 348

Yes the result will be the same.

With a left join you will get every dataset of both table who got a ID. With a join (Inner Join) you will get only the dataset's who a.id = b.id.

This site will explain you how to join https://www.w3schools.com/sql/sql_join.asp

Upvotes: 1

Gordon Linoff
Gordon Linoff

Reputation: 1269603

As written, they will return the same result.

The two will not necessarily return the same result with a left join.

Upvotes: 1

Related Questions