What are the difference between these two SQL queries? [duplicate] - sql

This question already has answers here:
Will ANSI JOIN vs. non-ANSI JOIN queries perform differently?
(7 answers)
Closed 8 years ago.
What are the differences between these two SQL queries?
Query #1:
select mytab.name, mytab.age, films.title, films.author
from films, mytab
where films.id = mytab.id;
Query #2:
select mytab.name, mytab.age, films.title, films.author
from films inner join mytab
on films.id = mytab.id;
First is a normal SQL query using 'where' statement. The second is using inner join. The result of both queries is exactly the same.
films -> id, title author
mytab -> id, name, age
It`s the poorest example as is possible.
Here is analogical example :
http://www.w3schools.com/sql/sql_join_inner.asp

Both Queries will yield same results but the only difference is in syntax,
1st query uses old syntax for join where you describe the relation in WHERE clause .
2nd Query uses newer ANSI syntax, where relation between tables are defined in ON clause.
Second syntax is preferred though.
Read Here for more information.

They are logically equivalent, and will produce the same results.
The first uses older join syntax.
Second uses the ANSI-92 join syntax, and is the preferred style.

Related

Difference between INNER JOIN and , COMMA [duplicate]

This question already has answers here:
Explicit vs implicit SQL joins
(12 answers)
Closed 5 years ago.
I get the same result in both queries below and same execution plan, any difference? or it is just how I prefer to write my query?
SELECT PS.StepID,PR.ProgramID FROM ProgramSteps PS, Programs PR
WHERE PS.ProgramID = PR.ProgramID
SELECT PS.StepID,PR.ProgramID FROM ProgramSteps PS
INNER JOIN Programs PR ON PS.ProgramID = PR.ProgramID
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.
And as #Tim Biegeleisen says in the comments:
the comma version is deprecated as of the ANSI-92 SQL standard

Are these two select sql statements equal? "join...on" vs. "where" [duplicate]

This question already has answers here:
INNER JOIN ON vs WHERE clause
(12 answers)
Closed 5 years ago.
Are the following two select SQL statements exactly equal? I usually use the join keyword, but I found that the sqlite author uses the second form in some of his document pages on sqlite.org.
The "inner join...on" form
SELECT * FROM Order INNER JOIN OrderItem ON Order.Id = OrderItem.ParentId
The "where" form
SELECT * FROM Order, OrderItem WHERE Order.Id = OrderItem.ParentId
Update 1:
I found this question might be duplicated, it seems has something to do with the SQL89 and SQL92 standards, but it doesn't answer me the second form is an "INNER JOIN", "OUTER JOIN" or anything else.
Result will be the same but there are separate purpose of each syntax.
for more details check this inner join vs where
If you talk about specifically the query you mentioned then yes both will result the same but join and where clause don't give same results. join is used for joining two tables and where clause is further used to specify the data selection from those tables.

What are the differences between JOIN statement and one without it? [duplicate]

This question already has answers here:
Explicit vs implicit SQL joins
(12 answers)
Closed 6 years ago.
I am currently learning SQL. I am trying to create Views using Oracle SQL Developer. I came across the JOIN statement but I do not understand why do we need JOIN. According to W3School, the purpose of JOIN is to join multiple rows together. I can do that without the expression JOIN (refer to the following code blocks, both of them produce the same view/table). So what is the purpose of JOIN?
With INNER JOIN:
SELECT
Acc.Cust_Id,
Cus.Address,
Acc.Avail_Balance
FROM
Account Acc
INNER JOIN
Customer Cus
ON
Acc.Cust_Id = Cus.CUST_ID
Without INNER JOIN:
SELECT
Acc.Cust_Id,
Cus.Address,
Acc.Avail_Balance
FROM
Account Acc,
Customer Cus
WHERE
Acc.Cust_Id = Cus.CUST_ID
The two queries are the same, except the 1st one ANSI-92 SQL syntax and the and other is older SQL syntax which didn't incorporate the join clause. They should produce exactly the same internal query plan, although you may like to check.
check This link
ANSI vs. non-ANSI SQL JOIN syntax

What is the difference between these two SQL? [duplicate]

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

Difference between inner join and where in select join SQL statement [duplicate]

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.