03 CCFP4.0 RDBMS
03 CCFP4.0 RDBMS
Copyright Guideline
2014 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.
Confidential
Confidential Information
This Document is confidential to Infosys Limited. This document contains information and data that
Infosys considers confidential and proprietary (Confidential Information).
Confidential Information includes, but is not limited to, the following:
Corporate and Infrastructure information about Infosys
Infosys project management and quality processes
Project experiences provided included as illustrative case studies
Any disclosure of Confidential Information to, or use of it by a third party, will be damaging to Infosys.
Ownership of all Infosys Confidential Information, no matter in what media it resides, remains with
Infosys.
Confidential information in this document shall not be disclosed, duplicated or used in whole or in part
for any purpose other than reading without specific written permission of an authorized representative of
Infosys.
This document also contains third party confidential and proprietary information. Such third party
information has been included by Infosys after receiving due written permissions and authorizations from
the party/ies. Such third party confidential and proprietary information shall not be disclosed, duplicated
or used in whole or in part for any purpose other than reading without specific written permission of
an authorized representative of Infosys.
3
Confidential
Course Information
Course Code: CCFP4.0-RDBMS
Course Name: Relational Database Management System
Document Number: RDBMS-03
Version Number: 4.0
Confidential
Introduction to Joins
Scenario
Manager of EasyShop retail chain wants to know details of items and available quantity in
their retail outlets
Observation
Item details and stock details are present in different tables
Manager requires specific information that are present in those tables
By joining tables
Confidential
Types of JOIN
CROSS JOIN
INNER JOIN
Outer join
LEFT OUTER JOIN
RIGHT OUTER JOIN
FULL OUTER JOIN
Self join
Confidential
CROSS JOIN
Guided Activity: Execute the following statement and discuss the result:
SELECT * FROM item CROSS JOIN retailstock;
Confidential
CROSS JOIN
SELECT * FROM item CROSS JOIN retailstock;
If table1 has m rows and table2 has n rows then the output will have m*n rows
item
retailstock
itemcode
description
I1001
Britannia Marie
R1001
I1001
25
1600
I1002
R1002
I1003
50
6600
I1003
Best Rice
Output
itemcode
description
I1001
Britannia Marie
R1001
I1001
25
1600
I1001
Britannia Marie
R1002
I1003
50
6600
I1002
R1001
I1001
25
1600
I1002
R1002
I1003
50
6600
I1003
Best Rice
R1001
I1001
25
1600
I1003
Best Rice
R1002
I1003
50
6600
Confidential
CROSS JOIN
Did the manager get the specific information with the
help of cross join?
Confidential
NO!
10
INNER JOIN
Scenario
The manager of EasyShop retail chain wants to know details of items and the quantity
available in their retail outlets
SELECT i.itemcode, description, retailoutletid, qtyavailable FROM
item i INNER JOIN retailstock rs ON i.itemcode = rs.itemcode;
item
retailstock
itemcode description
I1001 Britannia Marie
I1002
Taj Mahal Tea
I1003
Best Rice
Required output
itemcode
description
retailoutletid
qtyavailable
I1001
I1003
Britannia Marie
Best Rice
R1001
R1002
25
50
Confidential
11
item
retailstock
itemcode description
I1001 Britannia Marie
I1002
Taj Mahal Tea
I1003
Best Rice
Output
itemcode
description
retailoutletid
itemcode
qtyavailable
I1001
I1001
I1002
I1002
I1003
I1003
Britannia Marie
Britannia Marie
Taj Mahal Tea
Taj Mahal Tea
Best Rice
Best Rice
R1001
R1002
R1001
R1002
R1001
R1002
I1001
I1003
I1001
I1003
I1001
I1003
25
50
25
50
25
50
Confidential
12
Output
itemcode
description
retailoutletid
qtyavailable
I1001
I1003
Britannia Marie
Best Rice
R1001
R1002
25
50
Confidential
13
Quotation details (supplierid, itemcode, quotedprice, quotationstatus, etc.) are available in quotation table
Observation
Three tables need to be joined to get the desired output
The common column supplierid is present in supplier and quotation table
The common column itemcode is present in item and quotation table
Quotation status Closed can be checked from quotationstatus column
Confidential
14
supplierid
item
itemcode
quotation
suppliername
itemtype
suppliercontactno
description
price
supplieremailid
category
qtyonhand
reorderlevel
quotationstatus
Confidential
15
Guided activity
The management of Easy Shop would like to know the id and name of customers
whose total bill amount is more than 7000.
SELECT c.customerid, c.customername
FROM customer c
JOIN purchasebill p ON c.customerid=p.customerid
GROUP BY c.customerid, c.customername
HAVING SUM(billamount) >7000;
Confidential
16
Scenario
List all the employees along with the locations of the outlets they are working.
This should also include employees who are not allocated to any retail outlet
retailoutlet
employee
empid empname worksin
1001 George R1001
1002
Kevin
R1001
1003
Lisa
R1001
1004
Allen
1005
Peter
R1002
1006
John
R1002
Required output
empid
1001
1002
1003
1005
1006
1004
Confidential
17
Scenario
List all the employees along with the locations of the outlets they are working.
This should also include employees who are not allocated to any retail outlet
NO
Confidential
18
employee
empid empname worksin
1001 George R1001
1002
Kevin
R1001
1003
Lisa
R1001
1004
Allen
NULL
1005
Peter
R1002
1006
John
R1002
retailoutlet
NULL
Required output:
empid empname retailoutletid retailoutletlocation
1001 George
R1001
California
1002
Kevin
R1001
California
1003
Lisa
R1001
California
1005
Peter
R1002
New York
1006
John
R1002
New York
1004
Allen
NULL
NULL
Can we get the same output by changing the order of the table in the query ?
Confidential
19
A scenario
The Manager of EasyShop would like to know the customer names along
with their wifes name
customer
Required output
Susan
2001
2005
Nancy
2002
customerid husband
2001
John
2002
Jason
wife
Susan
Nancy
Self join
Confidential
20
Working of Self-Join
SELECT h.customerid, h.customername AS husband, w.customername
AS wife FROM customer h INNER JOIN customer w
ON h.spouse = w.customerid AND h.gender = M ;
customerid customername spouse gender
2001
John
2004
M
2002
Jason
2005
M
2003
Smith
M
2004
Susan
2001
2005
Nancy
2002
2004
Susan
2001
2004
Susan
2001
2005
Nancy
2002
2005
Nancy
2002
Confidential
21
Self join
h
customerid
customerid
customername
M
M
2001
2002
John
Jason
2003
Smith
spouse gender
2002
John
Jason
2003
Smith
2004
Susan
2001
2004
Susan
2001
2005
Nancy
2002
2005
Nancy
2002
2001
2004
2005
customerid
husband
wife
2001
John
Susan
2002
Jason
Nancy
2004
2005
M
M
M
Confidential
22
Guided activity
The Super Manager of Easy Shop wants to generate a list of all the items with
their unit price and category having the same category as that of item Xbox
Gamepad
Confidential
23
Guided activity
The Super Manager of Easy Shop wants to generate a list of all other items with
their unit price and category having the same category as that of item Xbox
Gamepad
Confidential
24
Summary
Joins
CROSS JOIN
INNER JOIN
Outer join
LEFT OUTER JOIN
RIGHT OUTER JOIN
Self join
Confidential
25
Self-Study
Refer to NPTEL course: http://nptel.ac.in/courses.php
Course : Course : NPTEL >> Computer Science and Engineering >> Database Design
Videos:
Structured Query Language
Structured Query Language II
Refer to:
https://class.stanford.edu/courses/Home/Databases/Engineering/about
Confidential
26
References
Abraham Silberschatz, Henry Korth and S. Sudarshan, Database System Concepts
Jan 27, 2010
C.J. Date, Database Design and Relational Theory: Normal Forms and All That Jazz
(Theory in Practice) Apr 24, 2012
Kevin Loney, George Koch Oracle 9i, The Complete reference Oracle Press
http://en.wikipedia.org/wiki/Database_normalization
http://en.wikipedia.org/wiki/Entity%E2%80%93relationship_model
http://www.w3schools.com/sql/default.asp
http://docs.oracle.com/cd/E11882_01/server.112/e41084/toc.htm
http://online.stanford.edu/course/intro-to-databases-winter-2014
http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-830database-systems-fall-2010/lecture-notes/
http://www.techopedia.com/definition/1245/structured-query-language-sql
Confidential
Thank You
2013 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.