144

I have a table Person with a column id that references a column id in table Worker.

What is the difference between these two queries? They yield the same results.

SELECT * 
FROM Person 
JOIN Worker 
ON Person.id = Worker.id;

and

SELECT * 
FROM Person, 
     Worker 
WHERE Person.id = Worker.id;
philipxy
  • 14,867
  • 6
  • 39
  • 83
Big Money
  • 9,139
  • 6
  • 26
  • 37

5 Answers5

96

There is no difference at all.

First representation makes query more readable and makes it look very clear as to which join corresponds to which condition.

Sateesh Pagolu
  • 9,282
  • 2
  • 30
  • 48
  • Is there any performance difference? How about in PostgreSQL? – Liam Feb 23 '15 at 10:23
  • 2
    There is absolutely no difference in performance. Final version prepared by SQL Server would be the same in both the cases. – Sateesh Pagolu Feb 27 '15 at 00:31
  • 70
    For larger queries I would say that the first way is more readable. If you have a mix left joins and some inner joins, then it is far more consistent to have the joins condition on the join clause, rather than some in the joins and some in the where clause. – wobbily_col Sep 14 '15 at 11:05
  • 11
    The first is way more readable and explicit – Stephen York Aug 27 '18 at 04:29
  • 8
    This answer is not correct. In the MySQL manual on the page https://dev.mysql.com/doc/refman/5.5/en/join.html it states: "However, the precedence of the comma operator is less than that of INNER JOIN, CROSS JOIN, LEFT JOIN, and so on. If you mix comma joins with the other join types when there is a join condition, an error of the form Unknown column 'col_name' in 'on clause' may occur" – Captain Payalytic Oct 17 '18 at 11:27
  • 2
    @CaptainPayalytic: The two queries given in the question are logically equivalent. As you point out, there is actually a difference between the two notations. (I concur with you that this answer is wrong. There is, in fact, a *difference* between the two queries... we could say that one uses the old school comma syntax for the join operation, the other uses the JOIN keyword.) – spencer7593 Oct 17 '18 at 19:18
  • 1
    Further to earlier comments, using the first format you can group all join conditions into `ON` clauses, leaving your `WHERE` clauses to specify filters for your data (with the caveat that it's possible to write join conditions that act as filters, but in general, keeping to this practice makes the code more readable). – youcantryreachingme Feb 20 '19 at 22:01
  • 1
    Run a `EXPLAIN /SHOW WARNINGS` and you will see those queries optimizes/internally rewriten (notice the warning message) the same -> https://www.db-fiddle.com/f/us9ffeFw7vcMVt2WoNXQzm/0 – Raymond Nijland Oct 03 '19 at 18:03
  • Are they also the same in Oracle? – amy Mar 23 '21 at 10:59
  • Technically by using commas performs slightly better (but indistinguishable unless a good profiling tool is used) because the parsing happens slightly quicker. The less characters the better for a parser. So the slowest is when using INNER JOIN keyword, the next best to perform is when using the JOIN keyword, and the best performance is when using a comma. Although using commas has not been ansi syntax for 30+ years. – JustBeingHelpful Apr 09 '23 at 19:16
67

The queries are logically equivalent. The comma operator is equivalent to an [INNER] JOIN operator.

The comma is the older style join operator. The JOIN keyword was added later, and is favored because it also allows for OUTER join operations.

It also allows for the join predicates (conditions) to be separated from the WHERE clause into an ON clause. That improves (human) readability.


FOLLOWUP

This answer says that the two queries in the question are equivalent. We shouldn't mix old-school comma syntax for join operation with the newer JOIN keyword syntax in the same query. If we do mix them, we need to be aware of a difference in the order of precedence.

excerpt from MySQL Reference Manual

https://dev.mysql.com/doc/refman/5.6/en/join.html

INNER JOIN and , (comma) are semantically equivalent in the absence of a join condition: both produce a Cartesian product between the specified tables (that is, each and every row in the first table is joined to each and every row in the second table).

However, the precedence of the comma operator is less than that of INNER JOIN, CROSS JOIN, LEFT JOIN, and so on. If you mix comma joins with the other join types when there is a join condition, an error of the form Unknown column 'col_name' in 'on clause' may occur. Information about dealing with this problem is given later in this section.

Community
  • 1
  • 1
spencer7593
  • 106,611
  • 15
  • 112
  • 140
  • 8
    comma is CROSS JOIN or INNER JOIN ON 1=1. It has lower precedence than keyword joins. INNER JOIN ON is defined in terms of comma & WHERE. – philipxy Dec 10 '18 at 10:28
17

Beside better readability, there is one more case where explicitly joined tables are better instead of comma-separated tables.

let's see an example:

Create Table table1
(
    ID int NOT NULL Identity(1, 1) PRIMARY KEY ,
    Name varchar(50)
)

Create Table table2
(
    ID int NOT NULL Identity(1, 1) PRIMARY KEY ,
    ID_Table1 INT NOT NULL
)

Following query will give me all columns and rows from both tables

SELECT
    *
FROM table1, table2

Following query will give me columns from first table with table alias called 'table2'

SELECT
    *
FROM table1 table2

If you mistakenly forget comma in comma-separated join, second table automatically convert to table alias for first table. Not in all cases, but there is chances for something like this

veljasije
  • 6,722
  • 12
  • 48
  • 79
  • If `table1` and `table2` are unrelated, I would rate the `FROM` + comma syntax here above the alternative and more verbose `INNER JOIN table2 ON 1 = 1` since the `ON` predicate is redundant. – Matt Arnold Feb 28 '19 at 10:24
  • 1
    @MattArnold : with MySQL, the `ON` clause is optional. the `ON 1=1` has no effect and is unnecessary. Also, the INNER keyword is optional, and has no effect. i.e. `FROM table1 JOIN table2` is sufficient, although in this case, with no join condition in the ON or WHERE clause, we could include the optional `CROSS` keyword as an aid to the future reader. To help the reader comprehend that the absence of a join condition was intentional. i.e. `FROM table1 CROSS JOIN table2`. I think this form communicates our intent more clearly than the comma syntax. – spencer7593 Jun 04 '19 at 20:13
7

Using JOINS makes the code easier to read, since it's self-explanatory.

In speed there is no difference (I tested it) and the execution plan is the same

If the query optimizer is doing its job right, there should be no difference between those queries. They are just two ways to specify the same desired result.

Zweedeend
  • 2,565
  • 2
  • 17
  • 21
vhadalgi
  • 7,027
  • 6
  • 38
  • 67
3

The SELECT * FROM table1, table2, etc. is good for a couple of tables, but it becomes exponentially harder as the number of tables increases.

The JOIN syntax makes it explicit what criteria affects which tables (giving a condition). Also, the second way is the older standard.

Although, to the database, they end up being the same

Vinayak Pahalwan
  • 2,915
  • 4
  • 26
  • 34