You are on page 1of 7

Page |1

INFORMATION TECHNOLOGY AND INFORMATION SYSTEM


DEPARTMENT

ADVANCED DATABASE MANAGEMENT SYSTEM

EXERCISE

4
ADVANCED SQL

AZAGRA, JEANE ROSE M. BSIT 201


NAME SECTION
March 1, 2021 March 2, 2021
DATE PERFORMED DATE FINISHED

GJPRosales
Page |2

I. OBJECTIVES

At the end of this exercise, students must be able to:


a. Join tables using an equijoin
b. Performing outer and self-joins
c. Adding conditions
d. Creating subqueries to query values based on unknown criteria
e. Using subqueries to find out which values exist in one set of data and not in
another

II. BACKGROUND INFORMATION

Write the appropriate SQL statement for the following queries. The result of the queries will be
checked from your computer.

• Use a join to query data from more than one table:


SELECT table1.column, table2.column
FROM table1
[NATURAL JOIN table2] |
[JOIN table2 USING (column_name)] |
[JOIN table2
ON (table1.column_name = table2.column_name)]|
[LEFT|RIGHT|FULL OUTER JOIN table2
ON (table1.column_name = table2.column_name)]|
[CROSS JOIN table2];

• Join–a relational operation that causes two or more tables with a common domain to be
combined into a single table or view
• Equi-join–a join in which the joining condition is based on equality between values in the
common columns; common columns appear redundantly in the result table
• Natural join–an equi-join in which one of the duplicate columns is eliminated in the result
table
• Outer join–a join in which rows that do not have matching values in common columns
are nonetheless included in the result table (as opposed to inner join, in which rows must
have matching values in order to appear in the result table)
• Union join–includes all columns from each table in the join, and an instance for each row
of each table
• The common columns in joined tables are usually the primary key of the dominant table
and the foreign key of the dependent table in 1:M relationships.

GJPRosales
Page |3

Example:

Processing Multiple Tables Using Subqueries

• Subquery–placing an inner query (SELECT statement) inside an outer query


• Options:
– In a condition of the WHERE clause
– As a “table” of the FROM clause
– Within the HAVING clause
• Subqueries can be:
– Noncorrelated–executed once for the entire outer query
– Correlated–executed once for each row returned by the outer query
Example

III. EXPERIMENTAL PROCEDURE

PART 1:
Scenario
The HR department needs your assistance in creating some queries.

Task
Write the appropriate SQL statement for the following queries. The result of the
queries will be checked from your computer.

PROVIDE A SCREENSHOT OF THE SCRIPT AND THE OUTPUT PER STEP

Part 1 Multiple Queries

1. Write a query for the HR department to produce the addresses of all the
departments. Use the LOCATIONS and COUNTRIES tables. Show the location

GJPRosales
Page |4

ID, street address, city, state or province, and country in the output. Use a JOIN
to produce the results.

2. The HR department needs a report of all employees. Write a query to display the
last name, department number, and department name for all the employees.

3. The HR department needs a report of employees in Toronto. Display the last


name, job, department number, and the department name for all employees who
work in Toronto.

GJPRosales
Page |5

4. Create a report to display employees’ last name and employee number along
with their manager’s last name and manager number. Label the columns
Employee, Emp#, Manager, and Mgr#, respectively.

5. Modify question#4 to display all employees including King, who has no manager.
Order the results by the employee number.

6. Create a report for the HR department that displays employee last names,
department numbers, and all the employees who work in the same department
as a given employee. Give each column an appropriate label.

Part 2 Subqueries

GJPRosales
Page |6

7. Create a report that displays the employee number, last name, and salary of all
employees who earn more than the average salary. Sort the results in order of
ascending salary.

8. Write a query that displays the employee number and last name of all employees
who work in a department with any employee whose last name contains a u.

9. Create a report for HR that displays the last name and salary of every employee
who reports to King.

10. Create a report for HR that displays the department number, last name, and job
ID for every employee in the Executive department.

GJPRosales
Page |7

IV. QUESTION AND ANSWER

1. Cite 3 importance on using join statements. Support your statements.

Join statements used to combine records from two or more tables in a


database. In real life scenario, there are large number of tables, that’s why there
is a constant need to use join for multiple tables based on the logical
relationships between them. Using join statements can make our tables or view
easy to read and use.

V. REFERENCES

Hoffer, J.A., Prescott, M.B., McFadden, F.R. (2016). Modern Database Management
12th Edition, Prentice Hall.
Microsoft. (2012). Database Administration Fundamentals . USA: Wiley.

GJPRosales

You might also like