What is the difference between making an INNER JOIN with ON and with Where? [duplicate]

0

I want to combine 2 tables with a INNER JOIN but I find 2 different syntaxes where they are placed like this:

  

INNER JOIN table_2 ON table_1_ID = table_2_ID

and the other in this way, changing only where

  

INNER JOIN table_2 WHERE table_1_ID = table_2_ID

What would be the difference or would they do the same?

    
asked by Aetos2501 18.04.2018 в 21:35
source

2 answers

1

If you are talking about results, there will not be many differences . If you talk about performance, the SQL Server engine is much smarter and will automatically rewrite your search in most cases, so will not have differences in performance . That said, I prefer you to use INNER JOIN when a query involves more than one table, since it is the valid syntax of ANSI .

If you're ever going to use OUTER JOIN , the question of JOIN vs WHERE does not make any sense, since the answer may be different in most cases.

See an example:

Assuming we have these two tables.

-- JOIN
SELECT s.SalesRep, o.Amount
FROM SalesRep s
INNER JOIN Orders o ON s.id = o.id
GO
-- WHERE
SELECT s.SalesRep, o.Amount
FROM SalesRep s, Orders o
WHERE s.id = o.id
GO

You will notice in the result set that both queries return exactly the same result. As I mentioned earlier when we use the clause INNER JOIN and WHERE , there is no impact of the result if the condition JOIN and clause WHERE have almost the same condition.

Let's see a quick example in which the external combination provides absolutely different results compared to the cases in which there is a totally different commercial logic when the external combination has to be used. As I mentioned earlier, it's like comparing apples and oranges if you compare the outer union and the where clause. They are not the same logically.

 -- JOIN
    SELECT s.SalesRep, o.Amount
    FROM SalesRep s
    LEFT OUTER JOIN Orders o ON s.id = o.id
    GO
    -- WHERE
    SELECT s.SalesRep, o.Amount
    FROM SalesRep s, Orders o
    WHERE s.id = o.id
    GO

  

Source: What is the Difference Between An INNER JOIN and WHERE Clause

    
answered by 18.04.2018 в 21:49
0

INNER JOIN ON VS WHERE

When you use the ON instruction, the presence of INNER JOIN depends or is necessary, and it will be applicable, for example, to indicate the relationship of kinship or similarity between one table and another, that is, you are indicating in which columns you should be based to identify if there is a relationship; it will be very useful when you need to make the relationship or link with multiple tables

When you use WHERE it is usually to tell you to find or dump the information of a query when there is a match, but it will be limited by questions of presentation of the query, in the following example I show you a use case where they apply the two; by doing an inner join of the table users and the table posts to get all the posts of the user with id 2:

SELECT users.*, posts.* FROM posts
INNER JOIN users
ON users.id = posts.id_user
WHERE users.id = 2;
  

Remember that when using any JOIN sentence, it is part of the   relational algebra that will indicate a collection of results   only when the coincidence that marks the join is present

    
answered by 18.04.2018 в 21:42