Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

INNER JOIN vs multiple table names in "FROM" [duplicate]

Tags:

sql

Possible Duplicate:
INNER JOIN versus WHERE clause — any difference?

What is the difference between an INNER JOIN query and an implicit join query (listing multiple tables after the FROM keyword)? For example:

Given the following two tables:

CREATE TABLE Statuses(   id INT PRIMARY KEY,   description VARCHAR(50) ); INSERT INTO Statuses VALUES (1, 'status');  CREATE TABLE Documents(   id INT PRIMARY KEY,   statusId INT REFERENCES Statuses(id) ); INSERT INTO Documents VALUES (9, 1); 

What is the difference between these two SQL queries? From the testing I've done, they return the same result. Do they do the same thing? Are there situations where they will return different result sets?

SELECT s.description FROM Documents d, Statuses s WHERE d.statusId = s.id AND d.id = 9;  SELECT s.description FROM Documents d INNER JOIN Statuses s ON d.statusId = s.id WHERE d.id = 9; 
like image 999
Michael Avatar asked Feb 25 '11 14:02

Michael


People also ask

Does inner join keep duplicates?

The answer is yes, if there are any. If there are duplicate keys in the tables being joined.

What's the difference between join and multiple tables in from?

Your performance question has been answered: there is no difference. In general: there should be no difference.

How do I prevent duplicate rows from joining multiple tables?

Solution. Select column values in a specific order within rows to make rows with duplicate sets of values identical. Then you can use SELECT DISTINCT to remove duplicates. Alternatively, retrieve rows in such a way that near-duplicates are not even selected.

Why do multiple table joins produce duplicate rows?

Using an Incomplete ON Condition. Unwanted rows in the result set may come from incomplete ON conditions. In some cases, you need to join tables by multiple columns. In these situations, if you use only one pair of columns, it results in duplicate rows.


1 Answers

If you do it the first way, people under the age of 30 will probably chuckle at you, but as long as you're doing an inner join, they produce the same result and the optimizer will generate the same execution plan (at least as far as I've ever been able to tell).

This does of course presume that the where clause in the first query is how you would be joining in the second query.

This will probably get closed as a duplicate, btw.

like image 156
Jordan Avatar answered Sep 29 '22 08:09

Jordan