# 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?
LVL 7
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Author Commented:
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';
``````
0
Commented:
I'm not seeing how it would return different data.
0
Author Commented:
From City
is same as

From Country?
0
Commented:
Single table isn't the same.

But when you do an INNER JOIN between two tables, table order doesn't matter.
0
MSSQL 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
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';
``````
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

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.