Search code examples
sqlinner-join

Using AND in an INNER JOIN


I am fairly new with SQL would like to understand the logic below.

SELECT *
FROM Table A A1 
INNER JOIN TABLE B B1 ON B1.ID = A1.ID AND A1 = 'TASK';

Not sure if this is a clear detail but please let me know. Thanks!


Solution

  • SELECT *
    FROM Table A A1
        INNER JOIN TABLE B B1 ON B1.ID = A1.ID AND A1.Column = 'TASK'
    

    is the same as

    SELECT *
    FROM Table A A1
        INNER JOIN TABLE B B1 ON B1.ID = A1.ID
    WHERE A1.Column = 'TASK'
    

    It's even the same performance wise, it's just a different way to write the query. In very large queries it can be more readable to use an AND directly on an INNER JOIN instead of "hiding" it the in the WHERE part.