This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
SQL join: where clause vs. on clause
What are the difference between of following table joining ways
and which one is more appreciate for query optimization.
SELECT SP.*, S.SNAME, S.STATUS, S.CITY
FROM S INNER JOIN SP
ON S.SNO = SP.SNO;
and
SELECT SP.*, S.SNAME, S.STATUS, S.CITY
FROM S, SP
WHERE S.SNO = SP.SNO;
The query optimiser will treat them both as equivalent - there is no difference in performance on all the major database engines.
From a readability and stylistic point of view, using the explicit join syntax is usually preferred - whatever you do, settle on a convention and stick to it!
there is one point to remember here
the column on which index is defined should be the first in on clause make a little difference
Related
This question already has answers here:
Is there any significance in the order of table in sql join statement
(5 answers)
Does the order of tables in straight joins, with no hint directives, affect performance?
(3 answers)
Does the order of tables referenced in the ON clause of the JOIN matter?
(9 answers)
Closed 2 years ago.
I have always had the doubt if it is the same or if it has consequences to make a query in the following ways, or if there is any standard to follow when establishing the order of a join depending on whether or not it is the table that has the key foreign.
Alumno Escuela
—————— ——————
Id_alumno Id_escuela
Nombre Nombre
Id_escuela ——————
1:
Select e.Nombre, a.id_alumno, a.nombre
From escuela e, alumno a
Where a.Id_escuela = e.Id_escuela
2:
Select e.Nombre, a.id_alumno, a.nombre
From alumno a, escuela e
Where e.Id_escuela = a.Id_escuela
And the same if I were using the reserved word JOIN
The order of the columns in the comparison does not matter.
SQL is a declarative language, not a procedural language. That means that a SQL query describes the result set, but does not specify how the results are generated. The SQL compiler and optimizer take care of handling the execution plan.
The two conditions specify exactly the same results, and so should have no impact on the execution plan.
That said, you should be using proper, explicit, standard, readable JOIN syntax.
The order is not important because the vast majority of modern databases automatically reorder joins according to what is most optimal for the query. You can verify this yourself by examining the execution plan of your different versions of the query. You will see that in all cases, the execution plan is the same.
For this reason, unless the order affects the result of the query itself (which is not the case in your example, but it would be if it were a LEFT JOIN for example), simply write the query to make it work for you. readable and easy to understand. The database will take care of reorganizing the clauses of your query automatically to maximize performance.
With this in mind, I recommend that you avoid using implicit join notation. Rather, it favors explicit joins, which are easier to understand, especially when the query wraps multiple tables with multiple joins:
select e.nombre, a.id_alumno, a.nombre
from escuela e
join alumno a on a.id_escuela = e.id_escuela
This question already has answers here:
INNER JOIN ON vs WHERE clause
(12 answers)
Closed 9 years ago.
I have two select join SQL statements:
select a.id from table_a as a, table_b as b where a.id=b.id;
select a.id from table_a as a inner join table_b as b on a.id=b.id;
Obviously, they are the same in result. But is there any difference between them , such as performance, portability.
One difference is that the first option hides the intent by expressing the join condition in the where clause.
The second option, where the join condition is written out is more clear for the user reading the query. It shows the exact intent of the query.
As far as performance or any other difference, there shouldn't be any. Both queries should return the exact same result and perform the same under most RDBMS.
The inner join syntax was added to SQL sometime in the 1990s. It's possible, but unlikely, that the optimizer can do better with it than with the old syntax that used the where clause for the join condition.
They should both be highly portable as things are now.
The inner join syntax is preferable because it is easier on the reader, as others have already remarked.
Both are standard SQL. Different DB systems may optimize them differently, but because they are so simple, I would be a little surprised if they do. But that is the nature of SQL: it is declarative, which gives the implementation a great deal of leeway in how to execute your query. There is no guarantee that these perform the same, or if they are different, which is faster.
They are exactly the same in SQL server. There is no performance difference.
This question already has answers here:
Condition within JOIN or WHERE
(10 answers)
Closed 9 years ago.
which example below is best practice for inner joins? the examples below are
pretty simple but, what if there are multiple tables involved?
which approach would you choose?
example queries:
simple
SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date
FROM suppliers, orders
WHERE suppliers.supplier_id = orders.supplier_id;
using keyword INNER JOIN
SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date
FROM suppliers
INNER JOIN orders
ON suppliers.supplier_id = orders.supplier_id;
Since 1992 ANSI recommends the JOIN notation, and regarding to readability this is what I would recommend using.
Everything has been said on this topic, but I will just remind that from a logical point of view it makes much more sense to separate the join conditions from the value filters for 2 reasons:
that's not the same thing at all
Oracle internally (and mysql probably too, even though I don't really know) first calculates intermediate recordsets and THEN, afterwards, applies the WHERE filters.
I'd recommend explicitly stating JOIN type and using ON clause.
But you need to use it correctly. In your example you forgot to include FROM clause and suppliers table.
SELECT suppliers.supplier_id,
suppliers.supplier_name,
orders.order_date
FROM suppliers s INNER JOIN
orders o ON s.supplier_id = o.supplier_id;
In terms of functionalities both WHERE AND JOINS are same..
This question already has answers here:
INNER JOIN ON vs WHERE clause
(12 answers)
Closed 9 years ago.
I have two select join SQL statements:
select a.id from table_a as a, table_b as b where a.id=b.id;
select a.id from table_a as a inner join table_b as b on a.id=b.id;
Obviously, they are the same in result. But is there any difference between them , such as performance, portability.
One difference is that the first option hides the intent by expressing the join condition in the where clause.
The second option, where the join condition is written out is more clear for the user reading the query. It shows the exact intent of the query.
As far as performance or any other difference, there shouldn't be any. Both queries should return the exact same result and perform the same under most RDBMS.
The inner join syntax was added to SQL sometime in the 1990s. It's possible, but unlikely, that the optimizer can do better with it than with the old syntax that used the where clause for the join condition.
They should both be highly portable as things are now.
The inner join syntax is preferable because it is easier on the reader, as others have already remarked.
Both are standard SQL. Different DB systems may optimize them differently, but because they are so simple, I would be a little surprised if they do. But that is the nature of SQL: it is declarative, which gives the implementation a great deal of leeway in how to execute your query. There is no guarantee that these perform the same, or if they are different, which is faster.
They are exactly the same in SQL server. There is no performance difference.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
INNER JOIN versus WHERE clause — any difference?
SQL JOIN: is there a difference between USING, ON or WHERE?
For example, I have this SQL statement:
SELECT *
FROM orders, inventory
WHERE orders.product = inventory.product
or
SELECT *
FROM orders
JOIN inventory
ON orders.product = inventory.product
What is the difference between these two?
They do exactly the same thing, but I'd recommend the second approach for readability and maintainability.
Using JOIN allows you to separate the conditions that define relationships between tables from conditions which are filters on the result set.
Using JOIN makes it easier to see if you are missing a join condition.
Using JOIN allows you to easily choose between INNER or OUTER JOIN. The comma syntax is equivalent to INNER JOIN (though some databases do have an extension to allow an outer join when using the first approach).
The most important is to be consistent about which you use. The comma syntax has different precedence from the JOIN keyword which can lead to confusing errors if you try to mix the two syntaxes in the same query. Because of point 3, it is easier to be consistent if you always use JOIN.
Inner join is ansi syntax, should be the preferred method.
Imagine how ugly this solution could get if you were to use the , with say many tables?
SELECT * FROM orders, inventory, products, logistics, accounting, materials, ...
Be kind to your future developers and anyone else looking at or maintaining this code use the JOIN syntax.
The comma (,) is equivalent to an CROSS JOIN. Using an explicit CROSS JOIN is more intuitive and recommended, as it can easily be changed to a LEFT JOIN, RIGHT JOIN, etc. Using CROSS JOIN is also ANSI-compliant.
They are functionally equivalent. The second one is 'newer'.