DBMS Lab 02 Lesson

Embed Size (px)

Citation preview

  • 7/28/2019 DBMS Lab 02 Lesson

    1/3

  • 7/28/2019 DBMS Lab 02 Lesson

    2/3

    4. The USING clause may be used instead of the ON clause for join conditions based on equal values of columnsthat 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 SAKILAdatabase,

    which contains common attributes across tables.

    Observe the appearance of the join column country_idtwice (i.e., the common columns from bothsource 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_idappears onlyonce 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. TheNATURAL JOIN syntax may also be used instead of the USING clause. This automatically joins tablesbased 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 correspondingNATURAL 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 equivalentqueries 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). Ingeneral, to join ntables, at least n-1join conditions/join columns are required.

    SELECT * FROM film f JOIN film_actor faON 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;

  • 7/28/2019 DBMS Lab 02 Lesson

    3/3

    III.Outer Joins1. The LEFT [OUTER] JOIN returns all rows from the first source table regardless of whether the rows satisfythe specified join condition or not. In effect, it returns not only the result of the inner join, but also the rowsfrom the first source table that do not have a match (via the join condition) in the second source table. Theunmatched 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 theactors who are included in the films. Based on the definition of the inner join, rows from both tables arecombined together based on equal values of the respective film_idcolumns from both source tables.

    SELECT * FROM film JOIN film_actor USING (film_id)

    ORDER BY actor_id;

    SELECT * FROM film INNER JOIN film_actorON 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 whoare included in the films, if there are any. Observe that the result has 3 more rows than the previousquery. 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 aconvenient 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 isreversed. Like the inner join, the outer join can be extended to extract data from more than two tables.