You are on page 1of 3

Database Management Systems I.

Laboratory Lesson #02

Querying data from multiple tables 1. More relevant information can be retrieved from databases by querying data from multiple tables. The most primitive operation is the cartesian product, which returns all possible combinations of all rows from the tables defined in the FROM clause. The following equivalent queries return all possible combinations of countries and cities.

SELECT * FROM country, city; SELECT * FROM country CROSS JOIN city;
2. The WHERE clause can be added to the cartesian product in order to provide more useful information. The query below displays the alphabetical list of countries, their corresponding cities and the continent they belong to. Note that the ambiguity caused by common column names in the source tables is eliminated by qualifying the column with the table name.

SELECT country.name AS country, city.name AS city, continent FROM country, city WHERE country.code = city.countrycode ORDER BY country;
A similar list, limited to Asian countries, is shown below.

SELECT country.name country, city.name city FROM country CROSS JOIN city WHERE country.code = city.countrycode and continent = 'Asia' ORDER BY country;
II. Inner Joins 1. A join allows data to be retrieved from multiple tables (not necessarily distinct) such that only rows that satisfy the join condition (i.e., a comparison between columns from the first source table with columns in the second source table) are included in the query result. The natural join or inner join, which combines rows based on equality between a column in one table and the primary key of another table, is the most frequently used type. 2. Although the join condition in an inner join can be specified using the WHERE clause, the SQL standard provides the INNER JOIN...ON syntax. The following queries show the equivalent statements for the queries above (part I #2).

SELECT country.name AS country, city.name AS city, continent FROM country INNER JOIN city ON country.code = city.countrycode ORDER BY country; SELECT country.name country, city.name city FROM country INNER JOIN city ON country.code = city.countrycode WHERE continent = 'Asia' ORDER BY country;
The following query displays the capital cities of Asian countries with population information.

SELECT country.name country, country.population, city.name city, city.population FROM country, city WHERE country.capital = city.ID AND continent = Asia ORDER BY country.population;
3. The JOIN, CROSS JOIN and INNER JOIN are syntactically equivalent in MySQL. The use of these keywords, in the absence of a join condition, results in the Cartesian product of the source tables.

4. The USING clause may be used instead of the ON clause for join conditions based on equal values of columns that have the same name in the source tables. The query result in this case shows the join column/s only once. When the ON clause is used, the common columns in the join condition are treated as distinct columns. These alternatives for join queries are illustrated below. Example queries are obtained from the SAKILA database, which contains common attributes across tables.

Observe the appearance of the join column country_id twice (i.e., the common columns from both source tables are displayed).

SELECT * FROM country JOIN city ON country.country_id = city.country_id;


Selecting the common column in the SELECT clause requires qualification via the table name.

SELECT country.country_id, country, city FROM country JOIN city ON country.country_id = city.country_id;
Below are the equivalent queries with the USING clause. The join column country_id appears only once and does not have to be qualified by a table name when included in the SELECT clause.

SELECT * FROM country JOIN city USING (country_id); SELECT country_id, country, city FROM country JOIN city USING (country_id);
5. The NATURAL JOIN syntax may also be used instead of the USING clause. This automatically joins tables based on equal values of the combination of all columns that have identical names. This eliminates the need for specifying the join columns in the statement. Join columns appear only once, just as in the USING clause.

Below are the corresponding NATURAL JOIN queries for the previous exampies.

SELECT * FROM country NATURAL JOIN city; SELECT country_id, country, city FROM country NATURAL JOIN city;
6. Qualifying column names with table names can be very time consuming, particularly if table names are lengthy. Table aliases can be used instead of table names.

SELECT a.country_id, country, city FROM country a JOIN city b ON a.country_id = b.country_id;
7. A join may involve the same table, that is, a table is joined to itself (a.k.a. self-join). The following equivalent queries display the list of cities that belong to the same country as a given city (sample given city is Lapu-Lapu).

SELECT b.city FROM city a JOIN city b ON a.country_id = b.country_id WHERE a.city = "Lapu-Lapu"; SELECT b.city from city a JOIN city b USING (country_id) WHERE a.city = "Lapu-Lapu";
8. Below are equivalent queries that illustrate a three-way join (i.e., data is extracted from three tables). In general, to join n tables, at least n-1 join conditions/join columns are required.

SELECT * FROM film f JOIN film_actor fa ON f.film_id = fa.film_id JOIN actor a ON fa.actor_id = a.actor_id; SELECT * FROM film JOIN film_actor USING (film_id) JOIN actor USING (actor_id); SELECT * FROM film NATURAL JOIN film_actor NATURAL JOIN actor;

III. Outer Joins 1. The LEFT [OUTER] JOIN returns all rows from the first source table regardless of whether the rows satisfy the specified join condition or not. In effect, it returns not only the result of the inner join, but also the rows from the first source table that do not have a match (via the join condition) in the second source table. The unmatched columns are displayed with null values. The RIGHT [OUTER] JOIN is analogous to the left join and returns all rows from the second source table instead.

The following equivalent queries display film information along with the corresponding actor IDs of the actors who are included in the films. Based on the definition of the inner join, rows from both tables are combined together based on equal values of the respective film_id columns from both source tables.

SELECT * FROM film JOIN film_actor USING (film_id) ORDER BY actor_id; SELECT * FROM film INNER JOIN film_actor ON film.film_id = film_actor.film_id ORDER BY actor_id; SELECT * FROM film NATURAL JOIN film_actor ORDER BY actor_id;
The following equivalent queries display ALL films along with the corresponding actor IDs of the actors who are included in the films, if there are any. Observe that the result has 3 more rows than the previous query. The first three rows contain information on films that do not have corresponding actors. (film_actor columns are null).

SELECT * FROM film LEFT JOIN film_actor USING (film_id) ORDER BY actor_id; SELECT * FROM film LEFT JOIN film_actor ON film.film_id = film_actor.film_id ORDER BY actor_id; SELECT * FROM film NATURAL LEFT JOIN film_actor ORDER BY actor_id;
The following equivalent queries display only those films without actors. The outer join provides a convenient way to accomplish this.

SELECT * FROM film LEFT JOIN film_actor USING (film_id) WHERE actor_id IS NULL; SELECT * FROM film LEFT JOIN film_actor ON film.film_id = film_actor.film_id WHERE actor_id IS NULL; SELECT * FROM film NATURAL LEFT JOIN film_actor WHERE actor_id IS NULL;
Equivalent queries can be created via the RIGHT JOIN provided that the order of the source tables is reversed. Like the inner join, the outer join can be extended to extract data from more than two tables.

You might also like