SQL inner join, which style is better? [duplicate] - sql

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.

Related

How do these two queries return the same result but use different joins? (Implicit vs. Explicit Query) [duplicate]

This question already has answers here:
Inner join vs Where
(19 answers)
Closed 2 years ago.
I have 2 tables: Order and Customer. Order has a foreign key from Customer:
I can join them in 2 different ways:
First way
Select *
from [Order]
join Customer
on [Order].Customer_id = Customer.id;
Second way
Select *
from [Order],Customer
where [Order].Customer_id = Customer.id;
The 2 queries return the same result set which leads me to my related questions:
Which query is the better of the two?
Is there a difference between them involving time execution?
Why is it that when I search join examples all of them are using the first way?
I learned the second type of query in college - is it wrong to use?
The difference occurs if you are using LEFT OUTER JOIN or RIGHT OUTER JOIN. filtration location matters here because criteria specified in the ON clause is applied before the JOIN is made. Criteria against an OUTER JOINed table provided in the WHERE clause is applied after the JOIN is made. This can produce very different result sets.
In comparison, it doesn't matter for INNER JOINs if the criteria is provided in the ON or WHERE clauses, the result will be the same.

When using multiple joins in SQL, is it faster to join everything to table A , or join Table A to Table B, Table B to Table C, etc? [duplicate]

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.

SQL Server JOINS: Are 'JOIN' Statements 'LEFT OUTER' Associated by Default in SQL Server? [duplicate]

This question already has answers here:
What is the difference between "INNER JOIN" and "OUTER JOIN"?
(28 answers)
Closed 8 years ago.
I have about 6 months novice experience in SQL, TSQL, SSIS, ETL. As I find myself using JOIN statements more and more in my intern project I have been experimenting with the different JOIN statements. I wanted to confirm my findings. Are the following statements accurate pertaining to the conclusion of JOIN statements in SQL Server?:
1)I did a LEFT OUTER JOIN query and did the same query using JOIN which yielded the same results; are all JOIN statements LEFT OUTER associated in SQL Server?
2)I did a LEFT OUTER JOIN WHERE 2nd table PK (joined to) IS NOT NULL and did the same query using an INNER JOIN which yielded the same results; is it safe to say the the INNER JOIN statement will yield only matched records? and is the same as LEFT OUTER JOIN where joined records IS NOT NULL ?
The reason I'm asking is because I have been only using LEFT OUTER JOINS because that is what I was comfortable with. However, I want to eliminate as much code as possible when writing queries to be more efficient. I just wanted to make sure my observations are correct.
Also, are there any tips that you could provide on easily figuring out which JOIN statement is appropriate for specific queries? For instance, what JOIN would you use if you wanted to yield non-matching records?
Thanks.
A join or inner join (same thing) between table A and table B on, for instance, field1, would narrow in on all rows of table A and B sharing the same field1 value.
A left outer join between A and B, on field1, would show all rows of table A, and only those rows of table B that have a field1 existing in table A.
Where the rows of field1 on table A have a field1 value that doesn't exist in table B, the table B value would show null for field1, but the row of table A would be retained because it is an outer join. These are rows that wouldn't show up in a join which is an implied inner join.
If you get the same results doing a join between table A and table B as you do a left outer join between table A and B, then whatever fields you're joining on have values that exist in both tables. No value for any of the joined fields in A or B exist exclusively in A or B, they all exist in both A and B.
It is also possible you're putting criteria into the where clause that belongs in the on clause of the outer join, which may be causing your confusion. In my example above of tables A and B, where A is being left outer joined with B, you would put any criteria related to table B in the on clause, not the where clause, otherwise you would essentially be turning the outer join into an inner join. For example if you had b.field4 = 12 in the WHERE clause, and table B didn't have a match with A, it would be null and that criteria would fail, and it'd no longer come back even though you used a left outer join. That may be what you are referring to.
JOIN's are mapped to 'INNER JOIN' by default

Difference between these joins [duplicate]

This question already has answers here:
Inner join vs Where
(19 answers)
Closed 8 years ago.
Is there any difference between
SELECT *
FROM TABLE_A a
JOIN TABLE_B b
ON a.propertyA = b.propertyA
And the query
SELECT * from TABLE_A a, TABLE_B b where a.propertyA=b.propertyA.
INNER JOIN is ANSI (SQL-92) syntax which you use on the first one. It is generally considered more readable, especially when you join lots of tables.
The WHERE syntax (SQL-89) is more relational model oriented. A result of two tables JOIN'ed is a cartesian product of the tables to which a filter is applied which selects only those rows with joining columns matching.
It's easier to see this with the WHERE syntax.
I'd rather go on the ANSI type join because if you some how omit the ON clause, an error is generated whereas the old type of join if you omit the condition on where clause will not produce an error message and thus it will generate cartesian product.
The two examples are the same. Both perform an INNER JOIN operation (even if it's just JOIN in the 2nd example) which basically returns all rows that contain matching results in relation to the ON clause.
My guess is that the JOIN and INNER JOIN operations are just a bit faster since they're designed for that specific purpose while SELECT statements can be modified around to do much more.
The "join" version has been around about 20 years and is preferred because it clearly identifies predicates used for the join as opposed to those used for filtering results.
It also allow outer joins if used with left join (where you still get table_a's row if there isn't a matching row in table_b).
The "comma" version doesn't allow outer joins (you won't get table_a's row if there isn't a matching row in table_b)

What are the differences between these two SQL queries - SQL Server [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Is it better to do an equi join in the from clause or where clause
I have following two SQL queries. Can you please explain me the differences between these two ?
QUERY: 1
SELECT a.*, b.*
FROM Table1 a
INNER JOIN Table2 b
ON a.id = b.id
AND a.col = 'value'
QUERY: 2
SELECT a.*, b.*
FROM Table1 a
INNER JOIN Table2 b
ON a.id = b.id
WHERE a.col = 'value'
Thanks
Nothing unless case sensitivity is turned on on the database
For an INNER JOIN, filtering on the JOIN condition vs filtering in the WHERE clause should give identical results.
If this were an OUTER JOIN, the results would be different since the first one pre-filters the results in the source table before the JOIN condition.
in this particular case it's the same result, but it's not the same thing.
the WHERE applies to the whole select, while the ON ... AND is only for the inner join (actually probably the same thing, but not for outer joins)
Yes, everything is pretty much the same. Case sensitive database will return error if your ID fields is not spelled correctly.
The difference is purely stylistic. Personally, I like to put the clauses that are key to performing the table joins up under the 'JOIN ... ON a=b' section, and put the clauses that have to do with my specific query filtering under the "WHERE" clause. There is no technical reason to do it one way or the other though, so long as you're talking about inner joins.