CCFP4 1-RDBMSAssignments PDF
CCFP4 1-RDBMSAssignments PDF
System
Version 4.1
COPYRIGHT NOTICE
© 2017 Infosys Limited, Bangalore, India. All Rights Reserved.
Infosys believes the information in this document is accurate as of its publication date; such
information is subject to change without notice. Infosys acknowledges the proprietary rights of
other companies to the trademarks, product names and such other intellectual property rights
mentioned in this document. Except as expressly permitted, neither this documentation nor
any part of it may be reproduced, stored in a retrieval system, or transmitted in any form or by
any means, electronic, mechanical, printing, photocopying, recording or otherwise, without the
prior permission of Infosys Limited and/ or any named intellectual property rights holders
under this document.
Monika Verma,
Kiran R.K., Lakshmi
1.0 Jan 2014 Satyendra Singh, Initial Draft
D.L.
Diwakar Jaiswal
Monika Verma,
1.1 Feb 2014 Kiran R.K. As per feedback comments
Lakshmi D.L.
Monika Verma, Incorporated additional
2.0 May 2014 Kiran R.K.
Manjeet Singh Juneja assignments
CONFIDENTIAL
CONTENTS
CONFIDENTIAL
Assignment 4: Self-Join – Guided activity .......................................................................................................................... 40
Assignments on Sub queries ................................................................................................................................................ 40
Assignment 1: Independent sub query - Single row and Multi row – Guided activity ........................................................ 40
Assignment 2: Multiple columns sub query - Demo ........................................................................................................... 42
Assignment 3: Multiple columns sub query - Guided activity............................................................................................. 43
Assignment 4: Correlated sub query – Guided activity ....................................................................................................... 44
Assignment 5: Exists/Not Exists – Guided activity ............................................................................................................ 45
Assignments on Index .......................................................................................................................................................... 45
Assignment 1: Index – Guided activity ................................................................................................................. 45
Assignments on SQL Best practices .................................................................................................................................... 46
Assignment 1: Best practice tips for writing SQL queries ............................................................................ 46
Assignments on Views......................................................................................................................................................... 49
Assignment 1: Views - Demo .................................................................................................................................... 49
Assignment 2: Views – Guided activity ................................................................................................................ 49
Assignments on Transaction and Locks............................................................................................................................... 50
Assignment 1: Concurrency issues – Self Study.................................................................................................................. 50
Assignment 2: Locks – Guided activity ............................................................................................................................... 53
Assignments on PL/SQL ..................................................................................................................................................... 54
Assignment 1: PL/SQL block – Guided activity.................................................................................................................. 54
Assignment 2: Usage of BOOLEAN data type – Guided activity ....................................................................................... 56
Assignment 3: Usage of SQL% Attributes in PL/SQL – Guided activity ........................................................................... 57
CONFIDENTIAL
Infosys Limited Assignments for RDBMS
supplier suppliers from whom items are bought to the retail store
The relations along with their attributes are mentioned below. Identify the
candidate key, primary key and foreign key(s) for these relations:
a) item (itemcode, itemtype, description, price, reorderlevel, quantityonhand,
category)
b) supplier (supplierid, suppliername, suppliercontactno, supplieremailid)
c) quotation (quotationid, supplierid, itemcode, quotedprice, quotationdate,
quotationstatus)
Objective: Given the case study, identify the data items required
Problem description:
1. Consider the EasyShop application. Identify all the data items required
for creating the database.
Refer CCFP4.1-RDBMS-EasyShop Retail Application Case Study.docx
Summary of this assignment: Identification of the data items for a given case
study
address
Degree of relationship:
Number of entities involved in the relationship
Cardinality of relationship:
The number of instances of one entity is related to the number of instances of
another entity.
Cardinality is represented by placing the appropriate number along with the
relationship
Employee
Employee
Note: ERD is usually viewed from left to right and from top to bottom.
M N
1 M Belongs
Branch Account Customer
Has to
Problem Description:
1. Consider the following relation and the given functional dependencies,
identify the candidate key and type of functional dependency
Relation:
item (itemid, marketingid, vendor, style, price)
Functional dependencies:
{itemid, marketingid} price
itemid vendor, style
Functional dependencies:
Problem Description:
1. Find out the candidate key(s) and the highest normal form in the given relation:
Suggested Solution:
Step 1: Identify the candidate key, with the help of given functional dependencies.
As per the first functional dependency, let us assume traineeid is a candidate key.
To confirm that, traineeid is a candidate key, it should determine rest of the
attributes.
The given two functional dependencies shows that, we can determine rest of the
attributes (i.e. traineename, classroomid and pcid} with the help of traineeid.
traineeid traineename
traineeid classroomid, pcid
Hence, traineeid is a candidate key. But as we know, a relation can have more
than one candidate key. Let’s check, if we have another candidate key in the given
relation.
Note:
1) SQL is case insensitive language.
2) Coding convention:
a. All the keywords must be written in UPPER case and table name, column
name etc. must be written in lower case.
3) The values of the columns whose data type is CHAR, VARCHAR2 or DATE must
be mentioned in single quotes.
Example: 'S1001', '15-Aug-1947' etc.
4) The values that are mentioned after the VALUES keyword must follow the same
order as the columns mentioned after the table name
5) The data values for VARCHAR2 or CHAR data type is case sensitive
3. SQL statement to add a record into the “supplier” table for a supplier without
email id is as follows
Summary of this assignment: You have learnt to create table without any
constraints and insert a record into the table.
Problem description:
Step 2: Create supplier table, with “supplierid“ column to have UNIQUE values
);
Best practice:
1) Every constraint must be named
2) The naming convention to be followed for a constraint:
<short table name>_<short column name>_<short
constraint type>
Example: sup_sid_unq
2. Create the table supplier where supplier id must not be left blank and it should
not repeat too
Note:
1) The composite primary key must be created as a table level
constraint
5. Create table supplier where the supplier id must start with ‘S’
Problem description:
1. Include a row in the supplier table
UPDATE supplier
SET suppliercontactno = ‘303-537-9127’
WHERE supplierid=‘S1009’;
Problem description:
1. After creating the table supplier, EasyShop wants to store the supplier city
information also in the supplier table
2. After including the new supplier city column, EasyShop wants to increase the
column size to 20
Table: item
category CHAR(1)
);
5. The category column of item table must have values, is a new requirement.
To do this, add NOT NULL constraint on category column by executing the
below SQL statement.
Solution:
ALTER TABLE item
MODIFY category CONSTRAINT itm_cg_nn NOT NULL;
Note:
1) To modify the data type of column, the column should be empty.
2) If required, to add a constraint on existing column of a table, the data
present in that column must not violate the constraint that has to be added.
3) To change the column which can accept null values to not null, use
MODIFY clause of ALTER statement.
Note:
1. The default value specified will be used when no value is specified for the
column while inserting data
2. The default value specified in the definition should satisfy the data type
and length of the column
3. If a default value is not explicitly set, the default value for the column is
implicitly set to NULL
Example:
Inserting a row into the quotation table with the quotation status as a default
value:
Inserting a row into the quotation table with quotation status is other than default
value:
Note
• Refer the document “CCFP4.1-RDBMS-EasyShop Retail
Application_DB_Design.docx” to know the DB design of
EasyShop retail application.
Problem description:
SELECT *
FROM item;
OR
2. Retrieve the supplier name and supplier’s contact number of the supplier
‘S1002’.
3. Retrieve the quotation id and supplier id of the quotations which have been
OR
SELECT quotationid, supplierid
FROM quotation
WHERE quotationstatus
IN ('Accepted', ‘Rejected');
4. Retrieve supplier details like supplier id and supplier name whose names
have ‘i’ as the second character.
OR
Note:
The table creation scripts and the insertion scripts for the retail application
are available in “Easyshop_Retailapplication_Table_Creation_Script.txt”.
Execute the SQL statements in this file and create the tables along with
data for practicing various SELECT statements
3. Retrieve the designation and salary of all ‘Manager’ and ‘Billing Staff’
4. Retrieve the designation and salary of all ‘Manager’ and ‘Billing Staff’ who
have salary in the range of 2500 to 5000 (both inclusive).
4. Retrieve the different item types and category of the items in the increasing
order of item types and category.
4. Retrieve the empname and salary in increasing order of the designation and
decreasing order of salary.
SELECT
empname, salary,
CASE
WHEN salary < 2500 THEN 'Class 3'
WHEN salary BETWEEN 2500 AND 5000 THEN 'Class 2'
WHEN salary > 5000 THEN 'Class 1'
END as Salgrade
FROM employee;
Summary of this assignment: In this assignment you have learnt the usage of
case statements and solve queries on them.
Summary of this assignment: In this assignment you have learnt the usage of
case statements and solve queries on them.
1. For a discount of 25.5% being offered on all FMCG item’s unit price, display
item code, existing unit price as “Old Price” and discounted price as “New
Price”. Round off the discounted price to two decimal values.
2. Retrieve the employee id, employee name of billing staff and the retail outlet
where they work. Perform a case insensitive search.
OR
SELECT empid, empname, worksin
FROM employee
WHERE UPPER (designation) = 'BILLING STAFF';
OR
SELECT empid, empname, worksin
FROM employee
WHERE LOWER (designation) = 'billing staff';
3. Retrieve the order id, order status and payment mode of all the orders. Display
‘Payment yet not done’ when payment mode has NULL value.
SELECT description
FROM item
WHERE LENGTH (description)>15;
6. Retrieve the order id and the number of days between order date and payment
date for all orders.
7. Retrieve the order id and the number of months between order date and
payment date for all orders.
8. Insert a record into quotation table with quotation date exactly as '16/2/2014'.
10. Retrieve the maximum salary, minimum salary, total salary and average
salary of employees.
12. Retrieve the total number of orders made and the number of orders for which
payment has been done.
Problem Description: Write the SQL queries for the following problem
statements.
Try out and observe the output:
SELECT COUNT (*)
FROM orderstatus;
2. Retrieve the orderid and the number of months between orderdate and
paymentdate for all orders where the number of months is more than one and
the payment has been done.
3. The salary of managers has been increased by 20%. Retrieve the empid,
existing salary as “Current Salary”, increased salary as “New Salary” and the
difference of existing and increased salary as “Incremented Amount”. Round
off the increased salary up to two decimal places.
4. Display the itemcode of those items where the difference of quantity on hand
and reorder level is more than 50.
Hint: use ABS() function
1. Retrieve the orderid and the number of times for which inward quantity
arrived for that order
Assignments on Joins
4. The Manager of EasyShop would like to know those supplier details with the
quoted price of those items for which quotations have been accepted.
Display supplierid, suppliername, itemcode and quotedprice for the
same.
Note: The table creation scripts and the insertion scripts for this
assignment are available in
“Emp_Dept_Vehicle_Table_Creation_Script.txt”. Execute the SQL
statements in this file and create the tables along with data for
practicing various SELECT statements
1. Display name and salary of the employees, who are drawing salary more
than 2000. Along with that display the department names in which they are
working. Structure of the table is given below
Figure 1.1
2. Display the name of employees and their department names who are
managers.
Note: See the structure in Figure 1.1
3. List the department names that have more than one employee drawing
salary more than 1500, working under it.
Note: See the structure in Figure 1.1
2. For each item, identify the stock availability in the retail outlet R1001. Display
itemcode, description and qtyavailable for the same. If there is no stock
available for an item, display ‘N.A.’ for its quantity on available.
1. For each employee, identify the vehicle owned by them. Display ename and
vehicleid for the same. Display name of employees even if they don’t own
any vehicle.
2. For each employee, identify the vehicle owned by them. Display ename and
vehiclename for the same. Display name of employees even if they don’t
own any vehicle.
1. For every retail outlet, identify the employees working in it. Display empid,
empname, retailoutletid and their retailoutletlocation.
2. Display the customer id and customer name of those customers who are co-
located. Do not display the duplicate records/rows.
3. Retrieve the customer id, customer name, and customer type of those
customers who are of the same customer type as that of customer id 2004.
Do not display the record of customer id 2004 in the result.
Note: The required schema and data has already been created using
“Easyshop_RetailApplication_TableCreationScript.txt” script
Problem Description: Write the SQL queries for the following requirements.
1. Identify the items which are shipped from the warehouse to various retail
outlets. Display itemcode, itemtype, description and category of those
items.
2. Identify the item details that have the least quoted price with the quotation
status as ‘Rejected’. Display itemcode, itemtype, description and
category of those items.
3. The management would like to know the details of the items which has
maximum quoted price amongst the quotations that have status as ‘Closed’
or ‘Rejected’. Display itemcode and description of those items.
4. Identify the item having second highest unit price. Display itemcode,
description and price of those items.
5. Identify the supplier who has submitted quotation with the least quoted
price amongst the quotations that have been accepted. Display
itemcode, description, supplierid and suppliername for the same.
Summary of this assignment: In this assignment you have learnt the concept
and usage of independent sub queries.
Note: The required schema and data has already been created using
“Easyshop_RetailApplication_TableCreationScript.txt” script
Summary of this assignment: In this assignment you have learnt the concept
of sub queries returning multiple columns.
Note: The required schema and data has already been created using
“Easyshop_RetailApplication_TableCreationScript.txt” script
Problem Description: Write the SQL queries for the following requirements.
Summary of this assignment: In this assignment you have learnt the concept
of sub queries returning multiple columns.
Note: The required schema and data has already been created using
“Easyshop_RetailApplication_TableCreationScript.txt” script
Problem Description: Write the SQL queries for the following requirements.
1. Display the itemcode, description and quotationdate for those items which
are quoted below the maximum quotation price on the same day.
3. Identify the supplier who has submitted the quotation for an item with the
quoted price, less than the maximum quoted price submitted by all other
suppliers, for the same item.
Display supplierid, suppliername and itemcode for the identified supplier.
Do not display duplicate records.
4. The payroll department requires the details of those employees who are
getting the highest salary in each designation. Display empid, empname,
designation and salary as per the given requirement.
Summary of this assignment: In this assignment you have learnt the concept
and usage of correlated sub queries.
Problem Description: Write the SQL queries for the following requirements.
1. Display the customer id and customer name of those customers who have
not purchased at all from any retail outlet.
2. Display the item code, description of those items which are shipped to any
retail outlet.
Estimated time: 30 minutes.
Summary of this assignment: In this assignment you have learnt the concept
and usage of exists and not exists.
Assignments on Index
3. -- Drop an Index
SQL> DROP INDEX supplier_idx;
Summary of this assignment: In this assignment you have learnt the concept
and usage of Index.
Objective: To write SQL queries by using the industry best practices and
formulating logically accurate queries
SELECT ename
FROM emp
WHERE empno = 7946;
5. Do not use HAVING for columns that can be used in the WHERE clause.
For example, do not write the following:
Optimized query
SELECT deptno, SUM(sal)
FROM emp
WHERE deptno IN(10, 20)
GROUP BY deptno;
6. Table aliases should be used in all queries that have more than one table
in the FROM clause. The use of table aliases speeds up the parse phase
of an oracle query, by reducing the number of recursive SQL queries.
7. When joining tables, the table returning the least number of rows should
be last in the FROM list.
8. Consider the following structure of the purchase table. Write a query to
retrieve purchaseid having value less than 100;
9. Remove unnecessary large-table full table scans. If the query returns less
and 40 percent of the table rows in an ordered table or 7 percent of the
rows in an unordered table), the query can be tuned to use an index in
lieu of the full table scan.
10. Try to avoid the use of DISTINCT clause, where ever possible. As the
DISTINCT clause will result in performance degradation, we should use
this clause only when it is necessary or unavoidable.
11. Try to drop indexes that are not being used. Because each index takes
up disk space and slow the DML operations, we should drop indexes that
are not used.
Summary of this assignment: In this assignment you have learnt a few tips for
writing good queries
Assignments on Views
Problem Description:
Summary of this assignment: In this assignment you have learnt the concept
and usage of Views
Problem description:
1. A store keeper is adding 25 units of item I1001 in retail outlet R1001. But
then he realized that he has to update the item I1002 and not I1001. So
he undid the update operation. Simultaneously 5 units of item I1001 is
being purchased from retail outlet R1001 by a customer. Sequence of the
transactions are as shown below:
What is your observation?
Dirty read
How many items should be present in the outlet finally? How can this be
resolved?
Incorrect summary
How many units of I1001 should be present in total finally? How can this be
resolved?
Step 1: Open two instances of SQL PLUS on your machine and login with your
Oracle ID in both the instances.
Note: The second instance is in wait state. The reason is the first transaction has
issued the update statement for the empid 10004 and it acquires the exclusive(X)
lock. The second transaction is also trying to update the same but because of the
X lock acquired by the first transaction it has to wait.
Summary: You have just learnt UPDATE DML statement acquire a row exclusive
lock
The X lock is released only at the end of transaction which is marked by
COMMIT/ROLLBACK.
Summary of this assignment: In this assignment you have learnt the concept
and usage of database locks.
Assignments on PL/SQL
Background: Create the tables and insert the data using the
Write a PL/SQL block as shown below, which declares variables for assigning
departmentid, departmentname and headofdepartment details and display the
same in execution section.
Summary of this assignment: You have just learnt how to declare variables in
the declaration section and how variables can be used in
DBMS_OUTPUT.PUT_LINE statements.
Problem Description: Analyze the following code and answer the questions
given below.
Problem Description:
1. Write a PL/SQL program to update the hostelfee by 15% where hostelid is
‘H1’ and verify whether any row is updated and if it has affected display how
many rows are updated.
Summary of this assignment: You have just learnt the usage of implicit cursor
attributes in PLSQL blocks.