2.1

The JOIN Clause

The Join Clause

Below is a basic example of a SQL statement with an inner join clause using explicit syntax.

1    USE world;
2    SELECT city.name AS "City Name", 
3        country.name AS "Country Name" 
4    FROM country 
6        JOIN city 
5            ON city.CountryCode = country. Code;

You could write SQL statements more succinctly with an inner join clause using table aliases. Instead of writing out the whole table name to qualify a column, you can use a table alias.

1    USE world;
2    SELECT ci.name AS "City Name", 
3        co.name AS "Country Name" 
4    FROM city ci 
5        JOIN country co 
6            ON ci.CountryCode = co.Code;

The results of the join query would yield the same results as shown below whether or not table names are completely written out or are represented with table aliases. The table aliases of co for country and ci for city are defined in the FROM clause and referenced in the SELECT and ON clause:

Results:

01_joins.png

Let us break the statement line by line:

USE world;

SELECT ci.name AS “City Name”, co.name AS “Country Name”

FROM city ci

JOIN country co

ON ci.CountryCode = co.Code;

CC BY-NC-ND International 4.0

CC BY-NC-ND International 4.0: This work is released under a CC BY-NC-ND International 4.0 license, which means that you are free to do with it as you please as long as you (1) properly attribute it, (2) do not use it for commercial gain, and (3) do not create derivative works.