0

Do the queries do the same? What is the standard?

Will I lose performance if I change one of these ways of write the query?

Query 1

   SELECT a.*, b.id AS b_id
     FROM table_a AS a
LEFT JOIN table_b AS b
       ON a.id = b.id

Query 2

   SELECT a.*, b.id AS b_id
     FROM table_a a, table_b b
    WHERE a.id = b.id
Jensen Ching
  • 3,144
  • 4
  • 26
  • 42
Gabriel Santos
  • 4,934
  • 2
  • 43
  • 74
  • What happens when you try them? (Doing so will answer your first question, and will probably answer your second one as well.) – Ken White Nov 06 '12 at 02:34

1 Answers1

5

They return different results.

A LEFT JOIN statement will return rows even if there is no associated records in table_b that match table_a id. So it will return all rows in table_a, paired with EITHER a matching row in table_a OR a blank/null table_b row (if for that row in table_a there isn't any matching row in table_b).

The second query is a shortcut for an INNER JOIN. This query will ONLY exclusively return rows that match the condition a.id = b.id. The second query can also be written as:

SELECT a.*, b.id AS b_id
FROM table_a a
INNER JOIN table_b b 
ON a.id = b.id

To answer your performance question, please see the answers on a related SO thread here.

Community
  • 1
  • 1
Jensen Ching
  • 3,144
  • 4
  • 26
  • 42