0% found this document useful (0 votes)
7 views

Quiz 4 DB Solution

Uploaded by

ak5933487
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)
7 views

Quiz 4 DB Solution

Uploaded by

ak5933487
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/ 8

QUIZ-4

Q1: Normalize the table upto 3rd normal form.


Solution:
To normalize the given unnormalized form (UNF) table into the Third Normal Form (3NF), we'll follow these steps:
1. First Normal Form (1NF):
o Ensure each table has a primary key.
o Eliminate repeating groups, ensuring atomicity.
2. Second Normal Form (2NF):
o Ensure the table is in 1NF.
o Remove partial dependencies; ensure non-key attributes are fully dependent on the primary key.
3. Third Normal Form (3NF):
o Ensure the table is in 2NF.
o Remove transitive dependencies; ensure non-key attributes are only dependent on the primary key.

Q2: Perform Left outer join and Natural join


Table 1: Employees

EmployeeID EmployeeName DepartmentID

1 John Smith 101

2 Jane Doe 102


Table 2: Departments

DepartmentID DepartmentName

101 Human Resources

102 Finance

103 IT

104 Marketing

1. Left Outer Join


A left outer join returns all records from the left table (Employees), and the matched records from the right table (Departments). The
result is NULL from the right side if there is no match.
SQL Query:
SELECT Employees.EmployeeID, Employees.EmployeeName, Departments.DepartmentName
FROM Employees
LEFT OUTER JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;
Result:

EmployeeID EmployeeName DepartmentName

1 John Smith Human Resources

2 Jane Doe Finance

2. Full Outer Join


A full outer join returns all records when there is a match in either left (Employees) or right (Departments) table records. It returns NULL
for non-matching rows on either side.
SQL Query:
SELECT Employees.EmployeeID, Employees.EmployeeName, Departments.DepartmentName
FROM Employees
FULL OUTER JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;
Result:

EmployeeID EmployeeName DepartmentName

1 John Smith Human Resources

2 Jane Doe Finance

NULL NULL IT

NULL NULL Marketing

3. Right Outer Join


A right outer join returns all records from the right table (Departments), and the matched records from the left table (Employees). The
result is NULL from the left side if there is no match.
SQL Query:
SELECT Employees.EmployeeID, Employees.EmployeeName, Departments.DepartmentName
FROM Employees
RIGHT OUTER JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;
Result:
EmployeeID EmployeeName DepartmentName

1 John Smith Human Resources

2 Jane Doe Finance

NULL NULL IT

NULL NULL Marketing

4. Natural Join
A natural join automatically joins tables based on columns with the same name and data type in both tables.
SQL Query:
SELECT Employees.EmployeeID, Employees.EmployeeName, Departments.DepartmentName
FROM Employees
NATURAL JOIN Departments;
Result:

EmployeeID EmployeeName DepartmentName

1 John Smith Human Resources

2 Jane Doe Finance

EXAMPLE -2
Table 1: Customers

CustomerID CustomerName City

1 Alice Johnson New York


2 Bob Smith Los Angeles

Table 2: Orders

OrderID CustomerID OrderAmount

101 1 250

102 1 300

103 3 150

104 2 400

1. Left Outer Join


A left outer join returns all records from the left table (Customers), and the matched records from the right table (Orders). The result is
NULL from the right side if there is no match.
SQL Query:
SELECT Customers.CustomerID, Customers.CustomerName, Orders.OrderID, Orders.OrderAmount
FROM Customers
LEFT OUTER JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
Result:

CustomerID CustomerName OrderID OrderAmount

1 Alice Johnson 101 250

1 Alice Johnson 102 300

2 Bob Smith 104 400

2. Full Outer Join


A full outer join returns all records when there is a match in either left (Customers) or right (Orders) table records. It returns NULL for
non-matching rows on either side.
SQL Query:
SELECT Customers.CustomerID, Customers.CustomerName, Orders.OrderID, Orders.OrderAmount
FROM Customers
FULL OUTER JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
Result:

CustomerID CustomerName OrderID OrderAmount

1 Alice Johnson 101 250

1 Alice Johnson 102 300

2 Bob Smith 104 400

NULL NULL 103 150

3. Right Outer Join


A right outer join returns all records from the right table (Orders), and the matched records from the left table (Customers). The result
is NULL from the left side if there is no match.
SQL Query:
SELECT Customers.CustomerID, Customers.CustomerName, Orders.OrderID, Orders.OrderAmount
FROM Customers
RIGHT OUTER JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
Result:

CustomerID CustomerName OrderID OrderAmount

1 Alice Johnson 101 250

1 Alice Johnson 102 300

2 Bob Smith 104 400

NULL NULL 103 150

4. Natural Join
A natural join automatically joins tables based on columns with the same name and data type in both tables.
SQL Query:
SELECT Customers.CustomerID, Customers.CustomerName, Orders.OrderAmount
FROM Customers
NATURAL JOIN Orders;
Result:

CustomerID CustomerName OrderAmount

1 Alice Johnson 250

1 Alice Johnson 300

2 Bob Smith 400


_____________________________________

You might also like