The JOIN Clause

The Join Clause

  • A JOIN clause allows you to access data from two or more tables in a query.
  • A join links to tables on a common key between the two tables. Usually the primary key on one table is compared to the foreign key on another table using the equals ( = ) sign. This is an equijoin or an inner-join. However, other comparison operators are also valid.
  • If column names from each table in the join have the same name, they must be qualified with the table name or a table alias.

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:



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.

End-of-Chapter Survey

: How would you rate the overall quality of this chapter?
  1. Very Low Quality
  2. Low Quality
  3. Moderate Quality
  4. High Quality
  5. Very High Quality
Comments will be automatically submitted when you navigate away from the page.
Like this? Endorse it!