Database Programming With SQL 6-3: Inner Versus Outer Joins Practice Activities

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Database Programming with SQL

6-3: Inner versus Outer Joins


Practice Activities
Objectives
• Compare and contrast an inner and an outer join
• Construct and execute a query to use a left outer join
• Construct and execute a query to use a right outer join
• Construct and execute a query to use a full outer join Vocabulary
Identify the vocabulary word for each definition below.
Performs a join on two tables, retrieves all the rows in the Left
table, even if there is no match in the Right table. It also retrieves
all the rows in the Right table, even if there is no match in the Left
FULL OUTER JOIN table.
A join that returns the unmatched rows as well as matched rows
outer join
Performs a join on two tables, retrieves all the rows in the Left
LEFT OUTER JOIN table even if there is no match in the Right table.
Performs a join on two tables, retrieves all the rows in the
RIGHT  OUTER JOIN Right table even if there is no match in the Left table.
A join of two or more tables that returns only matched rows
INNER JOIN

Try It / Solve It

Use the Oracle database for problems 1-7.

1. Return the first name, last name, and department name for all employees including those
employees not assigned to a department.

Ans:

SELECT emp.first_name "First Name", emp.last_name "Last Name" , dpt.department_name


"Department Name"
FROM employees emp LEFT OUTER JOIN departments dpt ON emp.department_id =
dpt.department_id;

OUTPUT:
First Name Last Name

Steven King

Neena Kochhar

Lex De Haan

2. Return the first name, last name, and department name for all employees including those
departments that do not have an employee assigned to them.

Ans:

SELECT emp.first_name "First Name", emp.last_name "Last Name" , dpt.department_name


"Department Name"
FROM employees emp RIGHT OUTER JOIN departments dpt ON emp.department_id =
dpt.department_id;

OUTPUT:

First Name Last Name

Jennifer Whalen

Michael Hartstein

Pat Fay

3. Return the first name, last name, and department name for all employees including those
departments that do not have an employee assigned to them and those employees not assigned
to a department.

Ans:

SELECT emp.first_name "First Name", emp.last_name "Last Name" , dpt.department_name


"Department Name"
FROM employees emp FULL OUTER JOIN departments dpt ON emp.department_id =
dpt.department_id;
OUTPUT:

First Name Last Name

Steven King

Neena Kochhar

Lex De Haan

4. Create a query of the DJs on Demand database to return the first name, last name, event date,
and description of the event the client held. Include all the clients even if they have not had an
event scheduled.

Ans:

SELECT ct.first_name, ct.last_name, ev.event_date, ev.description


FROM d_clients ct LEFT OUTER JOIN d_events ev ON ct.client_number = ev.client_number;

OUTPUT:

FIRST_NAME LAST_NAME EVENT_DATE

Hiram Peters 14-May-2004

Lauren Vigil 28-Apr-2004

Serena Jones -

5. Using the Global Fast Foods database, show the shift description and shift assignment date even
if there is no date assigned for each shift description.

Ans:
SELECT f_shifts.description "shift description", f_shift_assignments.shift_assign_date AS "shift
assignment date" FROM f_shifts LEFT OUTER JOIN f_shift_assignments ON f_shifts.code =
f_shift_assignments.code;

OUTPUT:

shift description

8am to 12pm 06-May-2004

6pm to 10pm -

You might also like