Labsheet12 Join
Labsheet12 Join
IS222 – DATABASE
CONCEPTS AND DESIGN
Lab sheet
Lab 12: JOINS
IS222
Table of Contents
LAB 12: JOINS.....................................................................................................................2
Objectives:.................................................................................................................................2
Tools/Software:.........................................................................................................................2
Concepts & Descriptions:............................................................................................................2
Relational DBMS..........................................................................................................................................2
SELECT Statement with NATURAL JOIN........................................................................................................4
SELECT Statement with CROSS JOIN............................................................................................................4
SELECT Statement with JOIN and USING Clause..........................................................................................4
SELECT Statement with JOIN and ON Clause...............................................................................................5
SELECT Statement to Join 3 Tables..............................................................................................................5
SELECT Statement with LEFT and RIGHT OUTER JOIN..................................................................................5
SELECT Statement with FULL OUTER JOIN...................................................................................................6
SELECT Statement with SELF JOIN................................................................................................................6
SELECT Statement with EQUIJOIN................................................................................................................6
SELECT Statement with Cartesian Product Join...........................................................................................7
SELECT Statement with Nonequijoin...........................................................................................................8
SELECT Statement with Outerjoin................................................................................................................8
Lab Activities:.............................................................................................................................0
Deliverables:..............................................................................................................................3
LAB 12: JOINS
Objectives:
This lesson covers the following objectives:
create SELECT Statements using NATURAL JOIN
create SELECT Statements using CROSS JOIN
create SELECT Statements using JOIN and USING clause
create SELECT Statements using JOIN and ON clause
create SELECT Statements using JOIN of 3 tables
create SELECT Statements using LEFT & RIGHT OUTER JOIN
create SELECT Statements using FULL OUTER JOIN
create SELECT Statements using Equijoin
create SELECT Statements using Cartesian Product Join
create SELECT Statements using Nonequijoin
create SELECT Statements using Outer join
Tools/Software:
To accomplish this session, students should have access to APEX.
By now, you have learned from lecture that RDBMS applies tables that are related to
each other through a common column/s. In SQL_Schema.sql that you downloaded in Lab 1,
there are 4 table designs: employees/departments, wf_countries, fast foods, and DJs on
demand. They are all available in our APEX so it’s better to understand their relationships.
Let’s set the employees/departments as an example. You may refer to the figure in the
next page.
There are 8 tables and 7 of them are related through common fields or columns. The
lines connecting the tables signifies these relationships. For example, Regions is related to
Countries through their common field Region_ID. Note than Region_ID is the primary key of
Regions and a Foreign Key of Countries.
Up to now, your experience using SQL has been limited to querying and returning
information from one database table at a time. This would not be a problem if all data in the
database were stored in only one table. But you know from data modeling that separating data
into individual tables and being able to associate the tables with one another is the heart of
relational database design.
Fortunately, SQL provides join conditions that enable information to be queried from
separate tables and combined in one report.
A SQL join clause combines fields from 2 (or more) tables in a relational database.
SELECT Statement with NATURAL JOIN
A natural join is based on all columns in two tables that have the same name and
selects rows from the two tables that have equal values in all matched columns.
Example Explanation
SELECT first_name, last_name, job_id, job_title This join will return columns from the
FROM employees NATURAL JOIN jobs employees table and their related
WHERE department_id > 80; job_title from the jobs table based on the
common column job_id.
Example Explanation
SELECT last_name, department_name The employees table contains 20 rows
FROM employees CROSS JOIN departments; and the departments table has 8 rows.
Performing a CROSS JOIN will return
160 rows.
Example Explanation
SELECT first_name, last_name, department_id, The columns referenced in the USING
department_name FROM employees JOIN clause should not have a qualifier (table
departments USING (department_id); name or alias) anywhere in the SQL
statement.
Example Explanation
SELECT last_name, job_title In this example, the ON clause is used to
FROM employees e JOIN jobs j join the employees table with the jobs
ON (e.job_id = j.job_id); table.
SELECT last_name, salary, grade_level, This is to know the grade_level for each
lowest_sal, highest_sal employees salary. Even if the
FROM employees JOIN job_grades job_grades table does not have a
ON(salary BETWEEN lowest_sal AND common column with the employees
highest_sal); table.
Example Explanation
SELECT last_name, department_name AS To produce a report of employees, their
"Department", city department, and the city where the
FROM employees JOIN departments USING department is located, we need to join
(department_id) three tables: employees, departments
JOIN locations USING (location_id); and locations.
Example Explanation
SELECT e.last_name, d.department_id, note that the table name listed to the left
d.department_name of the words "left outer join" is referred
FROM employees e to as the "left table."
LEFT OUTER JOIN departments d ON
(e.department_id = d.department_id);
Example Explanation
SELECT e.last_name, d.department_id, This query will return all employee last
d.department_name names, both those that are assigned to a
FROM employees e department and those that are not.
LEFT OUTER JOIN departments d ON
(e.department_id = d.department_id);
Example Explanation
SELECT e.last_name, d.department_id, Employees and departments tables are
d.department_name fully joined on their department_id using
FROM employees e FULL OUTER JOIN.
FULL OUTER JOIN departments d ON
(e.department_id = d.department_id);
Example Explanation
SELECT worker.last_name, worker.manager_id, “Manager name” is alias names that
manager.last_name relate to the data's association with that
AS "Manager name" table. Manager_id in the worker table is
FROM employees worker JOIN employees equal to employee_id in the manager
manager table.
ON (worker.manager_id = manager.employee_id);
Aliases
SELECT last_name, e.job_id, job_title FROM Table aliases are used in the query. But
employees e, jobs j WHERE e.job_id = j.job_id when column names are not duplicated
AND department_id = 80; between two tables, you do not need to
add the table name or alias to the
column name.
Join 3 Tables
SELECT last_name, city To produce a report of employees, their
FROM employees e, departments d, locations l department, and the city where the
WHERE e.department_id = d.department_id department is located, we need to join
AND d.location_id = l.location_id three tables: employees, departments
and locations.
Example Explanation
SELECT e.last_name, d.department_id, This query will return all employee last
d.department_name names, including those that are assigned
FROM employees e, departments d to a department and those that are not.
WHERE e.department_id = The same results could be obtained
d.department_id(+); using an ANSI LEFT OUTER JOIN.
SELECT *
FROM countries
NATURAL JOIN regions ;
SELECT *
FROM countries
JOIN regions ON countries.region_id = regions.region_id;
SELECT *
FROM countries
JOIN regions USING (region_id);
2. Write 3 SQL statements to display location ID, state province, and country ID from
locations table, plus country name from countries table.
Use Natural Join
SELECT LOCATION_ID, STATE_PROVINCE,COUNTRY_ID, COUNTRY_NAME
FROM locations
NATURAL JOIN countries ;
LEFT JOIN is used to retrieve all the rows from the "employees" table, regardless of
whether they have a matching record in the "job_history" table. . If there is no match, the
columns from the "job_history" table will be NULL.
4. Use EQUIJOIN to write a SQL statement to display employee ID, first name, manager
ID, and manager’s name from employees table.
5. Use EQUIJOIN to write a SQL statement to display the employee ID, first name,
department ID, job ID from employees table, as well department name from
departments table and job title from jobs table.
SELECT *
FROM employees emp
JOIN departments dep ON emp.department_id = dep.department_id;
7. Use EQUIJOIN to write a SQL statement to display an employee’s employee ID, first
name, last name, department ID, department name, job ID, and job title by joining
employees, departments, and jobs tables.
8. Use EQUIJOIN to write a SQL statement to display employee ID, first name, job ID,
and job title from employees and jobs tables, as well as start date, job ID, and job titl
from job_history and jobs tables.
9. Create a query of the Oracle database that shows employee last names , department
IDs, and department names. Include all employees even if they are not assigned to a
department.
Deliverables:
At the end of this session, students are expected to have written all the SQL statements in
the activity with correct output list.