Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

INNER JOIN keywords | with and without using them

Tags:

sql

join

SELECT * FROM TableA
INNER JOIN TableB
ON TableA.name = TableB.name

SELECT * FROM TableA, TableB
where TableA.name = TableB.name

Which is the preferred way and why? Will there be any performance difference when keywords like JOIN is used?

Thanks

like image 736
jai Avatar asked Apr 16 '10 10:04

jai


People also ask

How do you do inner join without using inner join?

You can replace the JOIN keyword with a comma in the FROM clause. What do you do next? There's no ON keyword for you to state the joining condition as there would be when using JOIN , e.g., on which two columns you want to join the tables. In this method, you simply use a WHERE clause to do so.

Can inner join used with and?

With the AND in the inner join you can specify it even more. Join the tables on the columns, where A1. Column = 'TASK' and throw away the rest. You could just as easily move the AND to the WHERE -Clause.

What happens if you use inner join with no conditions?

We can use 'cross join' without on condition. Cross join gives the result in cartesian product form. For instance, if in one table there are 3 records and another table has 2 records, then the first record will match with all the second table records. Then, the same process will be repeated for second record and so on.

Can you inner join without on?

If you use INNER JOIN without the ON clause (or if you use comma without a WHERE clause), the result is the same as using CROSS JOIN : a Cartesian product (every row of o1 paired with every row of o2 ).


2 Answers

The second way is the classical way of doing it, from before the join keyword existed.

Normally the query processor generates the same database operations from the two queries, so there would be no difference in performance.

Using join better describes what you are doing in the query. If you have many joins, it's also better because the joined table and it's condition are beside each other, instead of putting all tables in one place and all conditions in another.

Another aspect is that it's easier to do an unbounded join by mistake using the second way, resulting in a cross join containing all combinations from the two tables.

like image 195
Guffa Avatar answered Oct 05 '22 19:10

Guffa


Use the first one, as it is:

  • More explicit
  • Is the Standard way

As for performance - there should be no difference.

like image 39
Oded Avatar answered Oct 05 '22 17:10

Oded