Learning INNER JOIN syntax 

To inner join two tables, use the following syntax: 

SELECT column(s)
FROM table1
INNER JOIN table2
ON table1.column = table2.column
WHERE conditions
ORDER BY column(s);

The preceding syntax shows you how to join two tables together with an INNER JOIN. You join a column in table1 that matches a column in table2. The WHERE and ORDER BY clauses are optional. They are there to show you that the INNER JOIN syntax goes between the FROM and WHERE clauses. 

The following example will help you to understand how to use the INNER JOIN

SELECT lahmansbaseballdb.people.playerid, birthyear, yearid, teamid
FROM lahmansbaseballdb.appearances
INNER JOIN lahmansbaseballdb.people
ON lahmansbaseballdb.people.playerid = lahmansbaseballdb.appearances.playerid
WHERE yearid = 2017;

In the preceding code example, you will see that you list your columns as usual, but where the column exists in both tables, you will need to specify the table you want the column to be queried from; otherwise, you will get an error. For example, if you didn't preface playerid with lahmansbaseballdb.people, then you would receive this error message: 

Error Code: 1052. Column 'playerid' in field list is ambiguous

In this case, you could preface playerid with either table in the inner join since that field exists and is NOT NULL in either table.

Also, you place a table you want to query FROM the same as in querying a single table. In the case of an INNER JOIN, it doesn't matter which order you place the tables in the INNER JOIN.

Next, you will see the INNER JOIN clause. This clause is where you place the table you want to join to the table in the FROM clause.

Then, you will see the ON clause. This clause tells the query which column you want to join the tables on. In this case, the only column that exists in both is the playerid column, so it's the natural choice for joining.

You will see a WHERE clause to limit the results; otherwise, the query takes a long time to run without the WHERE clause to restrict it.

In the following screenshot, you will see the results returned with the preceding query: 

You can also leave off the INNER on an INNER JOIN. The following query will run the same as the preceding query: 

SELECT lahmansbaseballdb.people.playerid, birthyear, yearid, teamid
FROM lahmansbaseballdb.appearances
JOIN lahmansbaseballdb.people
ON lahmansbaseballdb.people.playerid = lahmansbaseballdb.appearances.playerid
WHERE yearid = 2017;

For clarity's sake, it's best to use the INNER JOIN syntax, especially if you are joining more tables using other joins besides INNER JOIN.

In MySQL, you can join up to 61 tables in a query. The best practice is to use the fewest joins possible to avoid issues with computer resources. 
In Oracle, PostgreSQL, and SQL Server, the only limit on table joins is computer resources. 

In the following query, you will be joining three tables to each other:

SELECT lahmansbaseballdb.people.playerid, birthyear,
lahmansbaseballdb.appearances.yearid,
lahmansbaseballdb.appearances.teamid, G_defense, H
FROM lahmansbaseballdb.appearances
INNER JOIN lahmansbaseballdb.people
ON lahmansbaseballdb.people.playerid = lahmansbaseballdb.appearances.playerid
INNER JOIN lahmansbaseballdb.batting
ON lahmansbaseballdb.people.playerid = lahmansbaseballdb.batting.playerid
WHERE lahmansbaseballdb.batting.yearid = 2017
AND H <> 0
ORDER BY lahmansbaseballdb.people.playerid,
lahmansbaseballdb.appearances.yearid,
lahmansbaseballdb.appearances.teamid, G_defense, H;

In the preceding query, you can see that you are now joining another table, lahmansbaseballdb.battingto the query. You use the same syntax as joining the lahmansbaseballdb.appearances table. The preceding query will return the rows where the people, batting, and appearances tables have matching rows. The following screenshot shows the results of the previous query:

The following screenshot shows the Venn diagram of which rows would be returned:

What the query will return is illustrated by the gray area where people, appearances, and batting overlap in the preceding image. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.39.142