DB_Lab_Manual
DB_Lab_Manual
Database Systems
Lab Manual (Lab 5)
1|Page
Manual Designed by: Miss Ayesha Majid
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:
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
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
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)
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:
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.
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.
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.
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.
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).
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:
You will need to write queries using different types of joins to solve the problem.
Tables:
1. books
2. authors
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