0% found this document useful (0 votes)
542 views34 pages

CCS0021L (Information Management) (F4-Formative) Formative Assessment 4

Here are the SQL queries with their outputs: 1. SELECT employee_id, first_name, last_name FROM EMPLOYEES WHERE manager_id = 114 2. SELECT employee_id, salary FROM EMPLOYEES WHERE salary > 11999 3. SELECT job_title, job_id FROM JOBS WHERE job_id IN ('AC_MGR ','IT_PROG') 4. SELECT * FROM DEPARTMENTS WHERE manager_id IS NULL 5. SELECT * FROM COUNTRIES WHERE country_name = 'Italy' OR country_name = 'Singapore' OR country_name = 'United States of America' OR region_
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
542 views34 pages

CCS0021L (Information Management) (F4-Formative) Formative Assessment 4

Here are the SQL queries with their outputs: 1. SELECT employee_id, first_name, last_name FROM EMPLOYEES WHERE manager_id = 114 2. SELECT employee_id, salary FROM EMPLOYEES WHERE salary > 11999 3. SELECT job_title, job_id FROM JOBS WHERE job_id IN ('AC_MGR ','IT_PROG') 4. SELECT * FROM DEPARTMENTS WHERE manager_id IS NULL 5. SELECT * FROM COUNTRIES WHERE country_name = 'Italy' OR country_name = 'Singapore' OR country_name = 'United States of America' OR region_
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

COLLEGE OF COMPUTER STUDIES AND MULTIMEDIA ARTS

CCS0021L
(INFORMATION MANAGEMENT)

[F4-FORMATIVE]
Formative Assessment 4
EXERCISE

7
DATA MANIPULATION LANGUAGE

Student Name / Group


Name: Kimberly Anne Dimal
Name Role
Members (if Group):

Section: DO21
Professor: Mr. Tim Jamison Awat

I. PROGRAM OUTCOME/S (PO) ADDRESSED BY THE LABORATORY EXERCISE


a. Apply knowledge through the use of current techniques and tools necessary for the IT profession. [PO: I]

II. COURSE LEARNING OUTCOME/S (CLO) ADDRESSED BY THE LABORATORY EXERCISE


• Create SQL statements that retrieve information requirements of the organization needed for reports generation.
[CLO: 4]

CCS0021L-Information Management Page 2 of 34


III. INTENDED LEARNING OUTCOME/S (ILO) OF THE LABORATORY EXERCISE
At the end of this exercise, students must be able to:
• Use SQL command to manipulate the data in the table
• Use the commit, rollback and save point as transaction control

IV. BACKGROUND INFORMATION


• A DML statement is executed when you:
✓ Add new rows to a table
✓ Modify existing rows in a table
✓ Remove existing rows from a table
• A transaction consists of a collection of DML statements that form a logical unit of work.
INSERT
• Add new rows to a table by using the INSERT statement:

INSERT INTO table [(column [, column...])]


VALUES (value [, value...]);

• With this syntax, only one row is inserted at a time.

• Adds one or more rows to a table


• Inserting into a table


• Inserting a record that has some null attributes requires identifying the fields that actually get data

• Inserting from another table

UPDATE
• Modify existing values in a table with the UPDATE statement:
UPDATE table
SET column = value [, column = value, ...]
[WHERE condition];

• Update more than one row at a time (if required).

SELECT
• Used for queries on single or multiple tables
• Clauses of the SELECT statement:
– SELECT

CCS0021L-Information Management Page 3 of 34


• List the columns (and expressions) to be returned from the query
– FROM
• Indicate the table(s) or view(s) from which data will be obtained
– WHERE
• Indicate the conditions under which a row will be included in the result
– GROUP BY
• Indicate categorization of results
– HAVING
• Indicate the conditions under which a category (group) will be included
– ORDER BY
• Sorts the result according to specified criteria
Syntax:

SELECT column, group_function


FROM table
[WHERE condition]
[GROUP BY group_by_expression]
[HAVING group_condition]
[ORDER BY column];

SELECT STATEMENT WITH AGGREGATE FUNCTIONS


Useful aggregate functions:
AVG() - Returns the average value
COUNT() - Returns the number of rows
FIRST() - Returns the first value
LAST() - Returns the last value
MAX() - Returns the largest value
MIN() - Returns the smallest value
SUM() - Returns the sum

Syntax:
SELECT AVG(column_name) FROM table_name;
SELECT min(column_name) FROM table_name;
SELECT max(column_name) FROM table_name where [condition];

SQL Scalar functions


SQL scalar functions return a single value, based on the input value.
Useful scalar functions:
UCASE() - Converts a field to upper case
LCASE() - Converts a field to lower case
MID() - Extract characters from a text field
LEN() - Returns the length of a text field
ROUND() - Rounds a numeric field to the number of decimals specified
NOW() - Returns the current system date and time
FORMAT() - Formats how a field is to be displayed

SQL ORDER BY Syntax


SELECT column_name, column_name
FROM table_name
ORDER BY column_name ASC|DESC, column_name ASC|DESC;

CCS0021L-Information Management Page 4 of 34


Example:
SELECT * FROM Customers
ORDER BY Country ASC;

SELECT * FROM Customers


ORDER BY Country;

The SQL BETWEEN Operator


The BETWEEN operator selects values within a range. The values can be numbers, text, or dates.

SQL BETWEEN Syntax


SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;

Example:
SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20;

SELECT * FROM Products


WHERE Price NOT BETWEEN 10 AND 20;

BETWEEN Operator with IN Example

The following SQL statement selects all products with a price BETWEEN 10 and 20, but products with a CategoryID of 1,2, or 3
should not be displayed:

Example
SELECT * FROM Products
WHERE (Price BETWEEN 10 AND 20)
AND NOT CategoryID IN (1,2,3);
The SQL LIKE Operator
The LIKE operator is used to search for a specified pattern in a column.

SQL LIKE Syntax


SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern;

Tip: The "%" sign is used to define wildcards (missing letters) both before and after the pattern. You will learn more about
wildcards in the next chapter.
The following SQL statement selects all customers with a City ending with the letter "s":
Example:
SELECT * FROM Customers
WHERE City LIKE '%s';

DELETE
Removes rows from a table
Delete certain rows
DELETE FROM CUSTOMER_T WHERE CUSTOMERSTATE = ‘HI’;

CCS0021L-Information Management Page 5 of 34


Delete all rows
DELETE FROM CUSTOMER_T;

TRUNCATE Statement
• Removes all rows from a table, leaving the table empty and the table structure intact.
• It is a data definition language (DDL) statement rather than a DML statement; cannot easily be undone.

A database transaction consists of one of the following:


• DML Statements that constitute one consistent change to the data.
• One DDL statement.
• One data control language (DCL) statement.

SAVEPOINT
• Create a marker in the current transaction by using the SAVEPOINT statement.

ROLLBACK
• Discard all pending changes by using the ROLLBACK statement.
• Roll back to the marker by using the ROLLBACK TO SAVEPOINT statement.

COMMIT
• Save the changes made permanently in the database by using COMMIT.

CCS0021L-Information Management Page 6 of 34


V. GRADING SYSTEM / RUBRIC

Criteria Descriptions Points


Creation of Tables (1-10 All the tables in the ERD are properly made 50
and complete
Changes made on the Tables The use of correct Data Definition 50
Language to change the characteristics of
the table are correct
Total 100%

VI. LABORATORY ACTIVITY

Instructions:
Make sure you finish Exercise 6 on F3-formative before doing this.
Connect your SQL developer to your Oracle Cloud Account and write the DML code the show the following. Then for
each number, copy and paste the code including the screenshot of the output.

Queries:
1. Show the employee id, first name, and last names of employees who are managed by manager id
114.
SELECT employee_id, first_name, last_name FROM EMPLOYEES
WHERE manager_id = 114

CCS0021L-Information Management Page 7 of 34


2. Show the employee id and the salary of the employees who has a salary higher than 11,999.
SELECT employee_id, salary FROM EMPLOYEES
WHERE salary > 11999

3. Show the job title of the job id AC_MGR and IT_PROG.


SELECT job_title, job_id FROM JOBS
WHERE job_id IN ('AC_MGR ','IT_PROG')

4. Show all the columns of the departments table that has no manager id.
SELECT * FROM DEPARTMENTS
WHERE manager_id IS NULL

CCS0021L-Information Management Page 8 of 34


5. Show the country id and country names of countries that are either Italy, Singapore, United States
of America or countries found in region 1.
SELECT * FROM COUNTRIES
WHERE country_name = 'Italy' OR country_name = 'Singapore' OR country_name = 'United
States of America' OR region_id = 1

CCS0021L-Information Management Page 9 of 34


6. Show all the columns of the employees that has a job id of AD_PRES.
SELECT * FROM EMPLOYEES
WHERE job_id = 'AD_PRES'

7. Show the job id and the number of employees that has a job id of SA_REP or ST_CLERK. Group
it by job id and arrange it by its job id.
SELECT job_id, phone_number FROM EMPLOYEES
WHERE job_id IN ('SA_REP ','ST_CLERK')
GROUP BY job_id, phone_number

8. Show the job ids and average salaries per job id of the employees table, group it based on job id.
Show only the job ids that has average greater than 10,000, and arrange it based on job id.
SELECT job_id, AVG(salary) FROM EMPLOYEES
GROUP BY job_id HAVING AVG(salary) > 10000

CCS0021L-Information Management Page 10 of 34


9. Show the salary, and the count of employees who has the same salary. Arrange it by salary in
ascending order. Rename the count of employees to ‘Number_of_Persons’.
SELECT count(salary) AS Number_of_Persons FROM EMPLOYEES
WHERE salary = salary

Please do the following in sequential order:


10. Add a new job record in the jobs table. The new record has the following details:
Job id – MK_ASST
Job title – Marketing Assistant
Minimum salary 3000
Maximim salary is 13000
INSERT INTO JOBS
VALUES ('MK_ASST', 'Marketing Assistant', 3000, 13000)

CCS0021L-Information Management Page 11 of 34


11. Add a new employee using the new job id that was created in number 11. The following details are
below:
Employee id is 500
First name is Alicia
Last name is Santos
Phone number is 650.124.0000
Job id is MK_ASST
Salary is 10000
Department id is 20

INSERT INTO employees (employee_id, first_name, last_name, email, phone_number,


hire_date, job_id, salary, department_id)
VALUES (500, 'Alicia', 'Santos', 'aliciaS@gmail' , '650.124.0000', SYSDATE, 'MK_ASST',
10000, 20);

CCS0021L-Information Management Page 12 of 34


12. Change the last name Delos Santos and salary to 12,500 of the employee with an employee id of
500.
UPDATE employees
SET last_name = 'Delos Santos'
WHERE employee_id = 500;

13. Create a save point name SPEmp500.


SAVEPOINT SPEmp500;

14. Delete the record of the employee id 500.


DELETE FROM employees
WHERE employee_id = 500;

15. Restore the deleted record (employee id 500) back to the employees table.
ROLLBACK TO SPEmp500;

16. Create a view that has all the details of the employees table.
Create view employee_details as
Select * from employees;

VII. QUESTION AND ANSWER

1. What is the advantage of using a view?


Views can represent a subset of the data contained in a table. Consequently, a view can limit the
degree of exposure of the underlying tables to the outer world: a given user may have permission to query
the view, while denied access to the rest of the base table.

VIII. REFERENCES

• Hoffer, J.A., Prescott, M.B., McFadden, F.R. (2007). Modern Database Management 8th
Edition. New Jersey: Pearson Prentice Hall.

CCS0021L-Information Management Page 13 of 34


CCS0021L-Information Management Page 14 of 34
EXERCISE

8
ADVANCED SQL

Student Name / Group


Name:
Name Role
Members (if Group):

Section:

Professor:

I. PROGRAM OUTCOME/S (PO) ADDRESSED BY THE LABORATORY EXERCISE


b. Apply knowledge through the use of current techniques and tools necessary for the IT profession. [PO: I]

II. COURSE LEARNING OUTCOME/S (CLO) ADDRESSED BY THE LABORATORY EXERCISE


• Create SQL statements that retrieve information requirements of the organization needed for reports generation.
[CLO: 4]

CCS0021L-Information Management Page 15 of 34


III. INTENDED LEARNING OUTCOME/S (ILO) OF THE LABORATORY EXERCISE
At the end of this exercise, students must be able to:
• Use SQL command to manipulate the data in the table
• Use the commit, rollback and save point as transaction control

IV. 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.
Example:

Processing Multiple Tables Using Subqueries

CCS0021L-Information Management Page 16 of 34


• 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

V. GRADING SYSTEM / RUBRIC

VI. LABORATORY ACTIVITY

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 ID, street address, city, state or province, and country in the
output. Use a NATURAL JOIN to produce the results.

CCS0021L-Information Management Page 17 of 34


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.

CCS0021L-Information Management Page 18 of 34


CCS0021L-Information Management Page 19 of 34
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.

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.

CCS0021L-Information Management Page 20 of 34


CCS0021L-Information Management Page 21 of 34
5. Modify question#4 to display all employees including King, who has no manager. Order the results by the
employee number.

CCS0021L-Information Management Page 22 of 34


CCS0021L-Information Management Page 23 of 34
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.

CCS0021L-Information Management Page 24 of 34


CCS0021L-Information Management Page 25 of 34
Part 2 Subqueries

7. The HR department needs a query that prompts the user for an employee last name. The query then
displays the last name and hire date of any employee in the same department as the employee whose
name they supply (excluding that employee). For example, if the user enters Zlotkey, find all employees who
work with Zlotkey (excluding Zlotkey).

CCS0021L-Information Management Page 26 of 34


CCS0021L-Information Management Page 27 of 34
8. 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.

CCS0021L-Information Management Page 28 of 34


CCS0021L-Information Management Page 29 of 34
9. 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.

CCS0021L-Information Management Page 30 of 34


CCS0021L-Information Management Page 31 of 34
10. The HR department needs a report that displays the last name, department number, and job ID of all
employees whose department location ID is 1700. Modify the query so that the user is prompted for a
location ID.

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

CCS0021L-Information Management Page 32 of 34


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

VII. QUESTION AND ANSWER

1. What is the difference between a Join and a Natural Join?


Join is a relational operation that causes two or more tables with a common domain to be combined
into a single table or view while a Natural Join is an equi-join in which one of the duplicate columns is
eliminated in the result table

CCS0021L-Information Management Page 33 of 34


VIII. REFERENCES

• Hoffer, J.A., Prescott, M.B., McFadden, F.R. (2007). Modern Database Management 8th
Edition. New Jersey: Pearson Prentice Hall.

CCS0021L-Information Management Page 34 of 34

You might also like