0
select a.name from student a, student b
where a.id = b.id and a.id = 1;

vs

select a.name from student a
inner join student b
on a.id = b.id and a.id = 1;

Are they actually the same?

Deqing
  • 14,098
  • 15
  • 84
  • 131
  • 1
    There's no difference but in syntax (join is prefferable) – Dmitry Bychenko May 19 '14 at 12:50
  • The former is written by web developers who don't know any better, the latter is written by people who want explicit, readable and maintainable code. *[Just to be very slightly contentious.]* – MatBailie May 19 '14 at 13:09

4 Answers4

0

Obviously they are not the same syntactically, but they are semantically (they return the same). However, I'd write the second one as:

select a.name from student a
inner join student b on a.id = b.id 
where a.id = 1;
Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
  • So is there any performance difference among them? – Deqing May 19 '14 at 12:58
  • Do you mean the samples you provided or between the last sample you provided and my "improved" version? If you ask whether the `JOIN` performs better over the `WHERE`, @BaconBits answer provides information on possible optimization benefits. – Thorsten Dittmar May 19 '14 at 13:02
0

The join you're doing is pointless because you're giving the same table different alias and joining on the same field i.e. id.

Your select statement should simply be as follows:

SELECT name FROM stud WHERE id = 1
SamoanProgrammer
  • 954
  • 4
  • 13
  • 27
0

the latter is per ansi standard and the formeris conventionally supported..they are functionally equivalent..if you use the latter, there is greater chance of your SQL being portable across vendors

Aadith Ramia
  • 10,005
  • 19
  • 67
  • 86
0

They are the same as far as the query engine is concerned.

The first type, commonly called a comma join, is an implicit inner join in most (all?) RDBMSs. The syntax is from ANSI SQL 89 and earlier.

The syntax of the second join, called an explicit inner join, was introduced in ANSI SQL 92. It is considered improved syntax because even with complex queries containing dozens of tables it is easy to see the difference between join conditions and filters in the where clause.

The ANSI 92 syntax also allows the query engine to potentially optimize better. If you use a something in the join condition, then it happens before (or as) the join is done. If the field is indexed, you can get some benefit since the query engine will know to not bother with certain rows in the table, whereas if you put it in the WHERE clause, the query engine will need to join the tables completely and then filter out results. Usually the RDBMS will treat them identically -- probably 999 cases out of 1000 -- but not always.

See also:

Why isn't SQL ANSI-92 standard better adopted over ANSI-89?

Community
  • 1
  • 1
Bacon Bits
  • 30,782
  • 5
  • 59
  • 66