This question already has answers here:
Inner join vs Where
(19 answers)
Closed 6 years ago.
Whats the difference between Query A and Query B. Both table_a and table_b are having 700k+ records. The obvious difference I can see is the speed (performance). Additionally, our Oracle consultants tend to write sql script using Query B.
Query A
select *
from table_a a
inner join table_b b
on a.id = b.id
Query B
select *
from table_a a,table_b b
where a.id = b.id
The second query is using more of a relational model(it is not recommended anymore.) concept whereas the first one is ANSI compliant and more readable.
Although this article is in SQL Server by Aaron but it gives a useful insight about the old practice: Bad habits to kick : using old-style JOINs
Related
This question already has answers here:
Does the join order matter in SQL?
(4 answers)
Closed 7 years ago.
To help clarify, here's some code:
Method 1
SELECT * FROM tableA a
JOIN tableB b ON a.id=b.id
JOIN tableC c ON a.id=c.id
JOIN tableD d ON a.id=d.id
Method 2
SELECT * FROM tableA a
JOIN tableB b ON a.id=b.id
JOIN tableC c ON b.id=c.id
JOIN tableD d ON c.id=d.id
THERE IS NO DIFFERENCE.
Keep in mind, that databases are based on mathematical set theory. And in terms of set theory, these joins are equal.
Therefore, if you look at the actual query execution plan, you will see that SQL server is even reorganizing joins, and it might rearrange them in a completely other way.
For example, if a table contains only 10 records, then this table is often taken for the first join, because by cutting away only 5 records, you can already cut down 50% of the whole result set.
The database is maintaining some statistics about number of records and distribution of the content. With these statistics, the query engine can make a very good "guess" which order would be the fastest one.
You can influence this behaviour by using query hints, but this is only useful in very rare situations.
This question already has answers here:
Explicit vs implicit SQL joins
(12 answers)
Closed 8 years ago.
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?
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?
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;
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
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
This question already has answers here:
What does (+) do in Oracle SQL?
(4 answers)
Closed 9 years ago.
I just stumbled across a SQL+-Query which uses (+). I've never worked with SQL+ before and I've never seen something like this. I tried to ask Google about it, but I couldn't find anything useful since Google obviously filters the "(+)" and just ignores it...
Example:
[...]
where [...]
AND 16791688 = T7mm.child_fielddef_id (+)
AND T7mm.parent_dbid = T7.dbid (+)
AND T1.dbid <> 0 [...]
it is an Oracle specific shortcut for OUTER JOIN
It makes the join an outer join rather than an inner join:
SELECT
A.*,
B.*
FROM
A,
B
WHERE
A.ID = B.ID(+)
is equivalent to:
SELECT
A.*,
B.*
FROM
A
LEFT JOIN B ON A.ID = B.ID
The (+) notation is the old Oracle syntax for SQL queries. Now it is generally viewed as best practice to use the ANSI standard with the LEFT JOIN keywords instead
This question already has answers here:
Explicit vs implicit SQL joins
(12 answers)
Condition within JOIN or WHERE
(10 answers)
Closed 9 years ago.
I know, if you want the inner join of two tables, you can write SQL in the following syntax.
select tableA.columnA
from tableA
inner join tableB on tableA.columnB=tableB.columnB
Alternatively, you can write the following.
select tableA.columnA
from tableA,
tableB
where tableA.columnB=tableB.columnB
so, which is better in terms of performance?
There is no difference in terms of performance. The where clause is in fact the same as INNER JOIN when it comes to relational algabra.
Read here for a brief explaination
Make sure you understand how inner joins work though, inner join will return more records than you might expect if one of your tables contain duplicate records. So basically, for each record in table A, it will return all the matching records in table B, and if the next record in table A matches the same records in table B, they will appear again. Read more here.
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.