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

DB_Lab_Manual

This lab manual, designed by Miss Ayesha Majid for Fall 2024, focuses on understanding database joins using Oracle 10g. It covers various types of joins (Inner, Left, Right, Full, Self, and Cross), their syntax, and provides lab exercises for practical application. Additionally, it includes a task related to a library database where students must create tables and write queries using different types of joins.

Uploaded by

akhtarwaheed902
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

DB_Lab_Manual

This lab manual, designed by Miss Ayesha Majid for Fall 2024, focuses on understanding database joins using Oracle 10g. It covers various types of joins (Inner, Left, Right, Full, Self, and Cross), their syntax, and provides lab exercises for practical application. Additionally, it includes a task related to a library database where students must create tables and write queries using different types of joins.

Uploaded by

akhtarwaheed902
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Manual Designed by: Miss Ayesha Majid

Database Systems
Lab Manual (Lab 5)

Session: Fall 2024

LAB INSTRUCTOR: AYESHA MAJID ALI

1|Page
Manual Designed by: Miss Ayesha Majid

Lab Manual: Understanding Database Joins


Objective:

To learn how to create and use DDL commands (specifically the CREATE command) to define the
structure of a relational database in Oracle 10g.

Prerequisites:

• Basic knowledge of SQL.


• Access to Oracle 10g Database.

Lab Environment Setup:

1. Oracle Installation: Ensure that Oracle 10g is installed and running.

1. Introduction to Database Joins


What is a Join?

A join is an operation in SQL that combines rows from two or more tables based on a related
column between them. Joins allow us to retrieve data from multiple tables in a single query,
enabling more powerful data analysis and reporting.

Types of Joins

There are several types of joins, each used for different purposes:

1. Inner Join: Returns only the rows that have matching values in both tables.
2. Left Join (Left Outer Join): Returns all rows from the left table and the matching rows
from the right table. If no match exists, NULLs are returned for the right table’s columns.
3. Right Join (Right Outer Join): Similar to Left Join, but returns all rows from the right
table, with NULLs for unmatched rows from the left table.
4. Full Join (Full Outer Join): Returns all rows when there is a match in either the left or
right table.
5. Self Join: A join where a table is joined with itself.
6. Cross Join: Returns the Cartesian product of two tables.

2|Page
Manual Designed by: Miss Ayesha Majid

2. Setting Up the Environment

To complete this lab, you'll need a database with sample tables. We'll use a simple relational
database set up with two tables: employees and departments.

Database Setup

1. Create the employees table:

CREATE TABLE employees (


emp_id INT PRIMARY KEY,
emp_name VARCHAR(50),
dept_id INT
);

2. Create the departments table:

CREATE TABLE departments (


dept_id INT PRIMARY KEY,
dept_name VARCHAR(50)
);

3. Insert sample data into the employees table:

INSERT INTO employees (emp_id, emp_name, dept_id) VALUES (1, 'John Doe', 1)
INSERT INTO employees (emp_id, emp_name, dept_id) VALUES (2, 'Jane Smith', 2)
INSERT INTO employees (emp_id, emp_name, dept_id) VALUES (3, 'Jim Brown', 1)
INSERT INTO employees (emp_id, emp_name, dept_id) VALUES (4, 'Jake White', 3)

4. Insert sample data into the departments table:


INSERT INTO departments (dept_id, dept_name) VALUES (1, 'Sales')
INSERT INTO departments (dept_id, dept_name) VALUES (2, 'Marketing')
INSERT INTO departments (dept_id, dept_name) VALUES (3, 'HR');

3. Inner Join
Explanation:

An Inner Join returns only the rows where there is a match in both tables. If there is no match,
the row is excluded from the result set.

3|Page
Manual Designed by: Miss Ayesha Majid

Syntax:
SELECT column1, column2, ...
FROM table1
INNER JOIN table2 ON table1.column = table2.column;

Lab Exercise:

Write a query to retrieve the emp_name and dept_name for all employees who have a department
assigned:

SELECT employees.emp_name, departments.dept_name


FROM employees
INNER JOIN departments ON employees.dept_id = departments.dept_id;

4. Left Join (Left Outer Join)


Explanation:

A Left Join returns all the rows from the left table and the matching rows from the right table. If
there is no match, the result will include NULL values for columns from the right table.

Syntax:
SELECT column1, column2, ...
FROM table1
LEFT JOIN table2 ON table1.column = table2.column;

Lab Exercise:

Write a query to retrieve all employees and their respective department names. If an employee
does not belong to a department, return NULL for the dept_name.

SELECT employees.emp_name, departments.dept_name


FROM employees
LEFT JOIN departments ON employees.dept_id = departments.dept_id;

5. Right Join (Right Outer Join)


Explanation:

A Right Join works similarly to a Left Join, but it returns all rows from the right table and the
matching rows from the left table. If no match is found, the left table columns will contain
NULL.

4|Page
Manual Designed by: Miss Ayesha Majid

Syntax:
SELECT column1, column2, ...
FROM table1
RIGHT JOIN table2 ON table1.column = table2.column;

Lab Exercise:

Write a query to retrieve all department names and the employee names that belong to them. If a
department has no employees, display NULL for the employee name.

SELECT employees.emp_name, departments.dept_name


FROM employees
RIGHT JOIN departments ON employees.dept_id = departments.dept_id;

6. Full Join (Full Outer Join)


Explanation:

A Full Join returns all rows when there is a match in either the left or right table. If there is no
match, NULLs will be returned for the missing side.

Syntax:
SELECT column1, column2, ...
FROM table1
FULL JOIN table2 ON table1.column = table2.column;

Lab Exercise:

Write a query to retrieve all employees and their department names, including those who don't
have a department, and all departments even if they have no employees.

SELECT employees.emp_name, departments.dept_name


FROM employees
FULL JOIN departments ON employees.dept_id = departments.dept_id;

7. Self Join
Explanation:

A Self Join is a join where a table is joined with itself. This is useful for hierarchical or relational
data within the same table.

Syntax:
SELECT a.column1, b.column2
FROM table a
INNER JOIN table b ON a.column = b.column;

5|Page
Manual Designed by: Miss Ayesha Majid

Lab Exercise:

Write a query to find employees in the same department. Display their names along with the
names of their department colleagues.

SELECT e1.emp_name AS employee, e2.emp_name AS colleague


FROM employees e1
INNER JOIN employees e2 ON e1.dept_id = e2.dept_id
WHERE e1.emp_id != e2.emp_id;

8. Cross Join
Explanation:

A Cross Join returns the Cartesian product of two tables. It returns every possible combination
of rows from both tables.

Syntax:
SELECT column1, column2
FROM table1
CROSS JOIN table2;

Lab Exercise:

Write a query to generate a combination of each employee with every department (even though
they may not belong to them).

SELECT employees.emp_name, departments.dept_name


FROM employees
CROSS JOIN departments;

6|Page
Manual Designed by: Miss Ayesha Majid

TASK

You are working with a library database, and you need to retrieve details about the books and the
authors who wrote them. The database consists of the following two tables:

1. books: Contains information about the books available in the library.


2. authors: Contains information about authors and the books they have written.

You will need to write queries using different types of joins to solve the problem.

Tables:

1. books

2. authors

Task 1: Create ERD diagram for above scenario.


Task 2: Create table against drawn ERD diagram. Both tables are related.
Task 3: JOINS

Question 1: Retrieve a list of books along with their corresponding authors, but only
include books that have an assigned author.

7|Page
Manual Designed by: Miss Ayesha Majid

Question 2: Retrieve a list of all books along with their authors. Include books that do not
have an assigned author, showing NULL for the author name in those cases.

Question 3: Retrieve a list of authors and the books they have written. If an author does not
have any books in the library, display NULL for the book title.

Question 4: Retrieve a list of all books and authors, including those that do not have
matches. Books with no authors and authors with no books should still appear in the
results.

Instructions: Paste the queries along with output as screenshot against each questions.

8|Page

You might also like