SQL JOIN: is there a difference between USING, ON or WHERE?

前端 未结 6 1834
渐次进展
渐次进展 2020-11-22 00:11

I was wondering if there is any difference in the way SQL performs on these join statements:

SELECT * FROM a,b WHERE a.ID = b.ID

SELECT * FROM a JOIN b ON a         


        
6条回答
  •  盖世英雄少女心
    2020-11-22 01:11

    I despise when you force a join by using WHERE. It just doesn't to me look right, a dirty hack. Proper ANSI join is to use ON:

    SELECT 
        p.Product,
        o.Order
    FROM 
        Product p
    INNER JOIN
        Order o
    ON
        o.OrderID = p.OrderID
    

    Prefer using ON when joining and WHERE to filter results. Remember WHERE is one of the last things you will use besides grouping and order by where you want to filter your results. So you shouldn't join your tables using WHERE as it is much difficult to read.

    SELECT 
        p.Product,
        o.Order
    FROM 
        Product p
    INNER JOIN
        Order o
    ON
        o.OrderID = p.OrderID
    WHERE
        o.Category = 'IT'
    

    In the end you (the developer) might not be around in the future so readability and maintainability will help the pour soul who has to take over your code :).

    When I see developers use WHERE to join their tables it's usually an indication that they don't know enough T-SQL. That is my personal opinion.

提交回复
热议问题