![]() | |
#11
| |||
| |||
|
|
On Feb 20, 1:21 pm, "Lennart" <Erik.Lennart.Jons... (AT) gmail (DOT) com> wrote: On Feb 20, 5:59 pm, "greg.fenton" <greg.fen... (AT) gmail (DOT) com> wrote: On Feb 19, 4:04 pm, "Lennart" <Erik.Lennart.Jons... (AT) gmail (DOT) com> wrote: I might be wrong, but since: AND cs.skill_id IN (5, 15) is part of the join condition, isn't all candidates returned, with or without skills? I.e. the way the query is expressed I don't think it will produce the same result as an inner join. No, that would be the case if you wrote: AND ( cs.skill_id IN (5, 15) OR cs.skill_id IS NULL ) greg.fenton can you provide an example where: OR cs.skill_id IS NULL ) makes a difference? /Lennart assume the result set before applying the AND clause has two rws: JOE 15 BILL NULL (ie Bill has no skills) then using this: AND ( cs.skill_id IN (5, 15) OR cs.skill_id IS NULL ) the final result set still has both rows. BUT the original query had this: AND ( cs.skill_id = 5 OR cs.skill_id = 15) BILL fails the condition (NULL does not match either 5 or 15) IOW the original query in this thread returns the same result as an inner join. If you don't believe me, try it. Ed |
#12
| |||
| |||
|
|
On Feb 20, 1:21 pm, "Lennart" <Erik.Lennart.Jons... (AT) gmail (DOT) com> wrote: On Feb 20, 5:59 pm, "greg.fenton" <greg.fen... (AT) gmail (DOT) com> wrote: On Feb 19, 4:04 pm, "Lennart" <Erik.Lennart.Jons... (AT) gmail (DOT) com> wrote: I might be wrong, but since: AND cs.skill_id IN (5, 15) is part of the join condition, isn't all candidates returned, with or without skills? I.e. the way the query is expressed I don't think it will produce the same result as an inner join. No, that would be the case if you wrote: AND ( cs.skill_id IN (5, 15) OR cs.skill_id IS NULL ) greg.fenton can you provide an example where: OR cs.skill_id IS NULL ) makes a difference? /Lennart assume the result set before applying the AND clause has two rws: JOE 15 BILL NULL (ie Bill has no skills) then using this: AND ( cs.skill_id IN (5, 15) OR cs.skill_id IS NULL ) the final result set still has both rows. BUT the original query had this: AND ( cs.skill_id = 5 OR cs.skill_id = 15) BILL fails the condition (NULL does not match either 5 or 15) IOW the original query in this thread returns the same result as an inner join. If you don't believe me, try it. Ed |
#13
| |||
| |||
|
|
On Feb 21, 8:59 pm, "Ed Prochak" <edproc... (AT) gmail (DOT) com> wrote: On Feb 20, 1:21 pm, "Lennart" <Erik.Lennart.Jons... (AT) gmail (DOT) com> wrote: On Feb 20, 5:59 pm, "greg.fenton" <greg.fen... (AT) gmail (DOT) com> wrote: On Feb 19, 4:04 pm, "Lennart" <Erik.Lennart.Jons... (AT) gmail (DOT) com> wrote: I might be wrong, but since: AND cs.skill_id IN (5, 15) is part of the join condition, isn't all candidates returned, with or without skills? I.e. the way the query is expressed I don't think it will produce the same result as an inner join. No, that would be the case if you wrote: AND ( cs.skill_id IN (5, 15) OR cs.skill_id IS NULL ) greg.fenton can you provide an example where: OR cs.skill_id IS NULL ) makes a difference? /Lennart assume the result set before applying the AND clause has two rws: JOE 15 BILL NULL (ie Bill has no skills) then using this: AND ( cs.skill_id IN (5, 15) OR cs.skill_id IS NULL ) the final result set still has both rows. BUT the original query had this: AND ( cs.skill_id = 5 OR cs.skill_id = 15) BILL fails the condition (NULL does not match either 5 or 15) [creates/inserts deleted] SELECT c.id, cs.id, cs.skill_id FROM candidates AS c LEFT OUTER JOIN candidates_skills AS cs ON c.id = cs.candidate_id ID ID SKILL_ID ----------- ----------- ----------- 1 1 15 2 - - 2 record(s) selected. SELECT c.id, cs.id, cs.skill_id FROM candidates AS c LEFT OUTER JOIN candidates_skills AS cs ON c.id = cs.candidate_id AND ( cs.skill_id IN (5, 15) ) ID ID SKILL_ID ----------- ----------- ----------- 1 1 15 2 - - 2 record(s) selected. [] /Lennart- Hide quoted text - - Show quoted text - |
#14
| |||
| |||
|
|
"greg.fenton" <greg.fen... (AT) gmail (DOT) com> wrote: Yep, using a LEFT OUTER JOIN instead of a simple inner join is a waste in this case, though a good RDBMS optimizer should recognize that the criteria exclude NULL rows from the right table and automatically rewrite the join... Will you name one RDBMS optimizer that will actually perform the action you describe above? I know a few optimizers, and would be very surprised if any of them got this one. |
![]() |
| Thread Tools | |
| Display Modes | |
| |