inner query

how below query
SELECT SUM(City.population)
FROM Country
INNER JOIN City
    ON Country.Code = City.CountryCode
WHERE Country.Continent='Asia';

different from below query

SELECT SUM(City.population)
FROM City//from City instead of Country here makes any difference?
INNER JOIN Country//from Country instead of City here makes any difference?
    ON City.CountryCode=Country.Code //here which is before and after matters in case of inner, left, right inner joins??
WHERE Country.Continent='Asia';

where i can learn in detail about all about inner and outer queries like any free video tutorials?
please advise
LVL 7
gudii9Asked:
Who is Participating?
 
Mark WillsTopic AdvisorCommented:
Vitor and Slightwv are quite correct, there is no real difference on an inner join as to what your result set will be....

An INNER JOIN is going to connect the two tables using ON. So, the two queries
SELECT SUM(City.population)
FROM Country
INNER JOIN City ON Country.Code = City.CountryCode
WHERE Country.Continent='Asia';


SELECT SUM(City.population)
FROM City
INNER JOIN Country  ON City.CountryCode=Country.Code 
WHERE Country.Continent='Asia';

Open in new window

Will return the same results. In that case they are 'the same'.

But there can be differences in how they execute. The "ON" part of the join tells the database engine how the relationship works. The Engine will look for Indexes, will look at statistics and decide how it will materialize the relationship.

In that regard, if there in an Index on (say) Country.Code but isnt on City.CountryCode then the Engine might decide to scan the tables and that does have an impact on how they execute. The Engine will also take into account any "where" clauses, uniqueness and a range of other factors in building an execution plan.

So, while there is no real difference in 'what' you get, there can be differences in 'how' it is executed.

That may or may not be the case here - just some theory to consider when joining tables. And that's what we consider with "tuning" and "performance"

Now an over-simplification...

All joins will extend the number of columns by the number of tables in the join (which you then select ). And there are two basic joins. an INNER or an OUTER. So, an INNER join is like an "AND", and the outer join is more like an "OR" - but in so much as the existence of the row being targeted by the outer join is optional.  In which case that table's column will be displayed with data OR with NULL if the outer join didnt find any matching rows. Where as the inner join will show the data from both  (or more) tables.

As Vitor said for an inner join, "It's like saying (2+4 is different than 4+2)...". It isnt really different as a result set.
As Slightwv said "Google around for: JOIN Venn Diagram"

Good place to start is : https://www.w3schools.com/sql/sql_join.asp
0
 
slightwv (䄆 Netminder) Commented:
When learning joins, Google around for: JOIN Venn Diagram

http://www.khankennels.com/blog/index.php/archives/2007/04/20/getting-joins/

The above link is referenced here:
https://blog.codinghorror.com/a-visual-explanation-of-sql-joins/
1
 
gudii9Author Commented:
how below query
SELECT SUM(City.population)
FROM Country
INNER JOIN City
    ON Country.Code = City.CountryCode
WHERE Country.Continent='Asia';

Open in new window


different from below query

SELECT SUM(City.population)
FROM City//from City instead of Country here makes any difference?
INNER JOIN Country//from Country instead of City here makes any difference?
    ON City.CountryCode=Country.Code //here which is before and after matters in case of inner, left, right inner joins??
WHERE Country.Continent='Asia';

Open in new window

0
Network Scalability - Handle Complex Environments

Monitor your entire network from a single platform. Free 30 Day Trial Now!

 
slightwv (䄆 Netminder) Commented:
I'm not seeing how it would return different data.
0
 
gudii9Author Commented:
From City
is same as

From Country?
0
 
slightwv (䄆 Netminder) Commented:
Single table isn't the same.

But when you do an INNER JOIN between two tables, table order doesn't matter.
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
Why your telling that they are producing different results?
It's like saying (2+4 is different than 4+2). In INNER JOIN the table order doesn't matter.
1
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.