Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which SQL do you write?

When joining two tables, what are the difference between the two blocks below and which is the better approach?

Pattern A:

SELECT ...
FROM A
    INNER JOIN B
        ON A.PK = B.FK
WHERE 1=1
    AND A.Name = "Foo"
    AND B.Title = "Bar"

Pattern B:

SELECT ...
FROM A
    INNER JOIN B
        ON A.PK = B.FK
            AND B.Title = "Bar"
WHERE 1=1
    AND A.Name = "Foo"
like image 739
Adrian Godong Avatar asked Apr 26 '26 11:04

Adrian Godong


2 Answers

This is going to differ from person to person, but I think that Pattern A is better.

What it does is it separates the table level joins from the filters. This could be helpful for queries with multiple joins and multiple filters because it clearly separates the two types of joining that is going on.

like image 108
bogertron Avatar answered Apr 29 '26 05:04

bogertron


I prefer pattern A, but there is probably not any difference. You really have to look at the execution plan though to make sure it is running efficiently.

like image 20
Mike Valenty Avatar answered Apr 29 '26 05:04

Mike Valenty



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!