0% found this document useful (0 votes)
19 views10 pages

Lab Manual 5

Uploaded by

shahzaib3769
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)
19 views10 pages

Lab Manual 5

Uploaded by

shahzaib3769
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/ 10

CL2005 – Database Systems Lab Lab Manual – 05

National University of Computer & Emerging Sciences,


Karachi Computer Science Department
Fall 2024, Lab Manual – 05

Course Code: CL-2005 Course: Database Systems Lab


Instructor(s): Fatima Gado

Contents:
● Introduction to Joins
● Types of Joins
● Introduction to Outer Joins and Its Types
● Introduction to Set Operator
● Types of Set Operator
● Exercise

INTRODUCTION TO JOIN
The JOIN keyword is used in an SQL statement to query data from two or more tables based on a relationship
between certain columns in these tables.

TYPES OF JOINS:
Following are the types of joins. They are:
● Cross Join / Cartesian Join
● Inner Join / Equity Join
● Outer Join
● Left Outer
● Right Outer
● Full Outer
● Self-Join
CrossCJLoi2n0/0C5a–rtDesaiatanbJaosien:Systems Lab Lab Manual – 05
In a Cartesian join, also called a Cartesian product or cross join, each recordin the first table is matched with each
record in the second table.

(# rows in Table 1) * (# rows in Table 2)

Syntax for Cross Join/Cartesian Join:


SELECT * FROM TABLE1, TABLE2;

ISO Standard:
SELECT * FROM TABLE1 CROSS JOIN TABLE2;

Inner Join / Equality Joins:


If the join contains an equality condition, it is also called Equi Join, Natural Join,Inner Join.

Syntax For Inner Join:


SELECT TABLE1_COLUMN, TABLE2_COLUMN FROM TABLE1 T1, TABLE2 T2
WHERET1. TABLE1_COLUMN = T2. TABLE2_COLUMN;

Example
To retrieve the employee name, their job and department name, we needto extract data from two
tables, EMP and DEPT:
SELECT E.ENAME, E.JOB, D.DNAME FROM EMP E, DEPT D WHEREE.DEPTNO = D.DEPTNO;
The SQL-1999 standard:
SELECT ENAME, JOB, DNAME FROM EMP NATURAL JOIN DEPT;

INNER JOIN with ON: You explicitly define the join condition using the ON clause, where you can compare any
columns, even if they have different names in the two tables.
INNER JOIN with USING: The USING clause is used when both tables share one or more columns with the same
name, and you want to join based on those columns.

Using Clause:
No matter how many common columns are available in the tables, NATURALJOIN will join with all the common
columns.
Use USING clause to join with specified columns.
CL2005 – Database Systems Lab Lab Manual – 05

SELECT Employees.EmployeeID, Employees.Name, Departments.DepartmentName


FROM Employees
INNER JOIN Departments
ON Employees.DeptID = Departments.DepartmentID;

INNER JOIN with USING:


You can use USING when the column names in both tables are the same
SELECT EmployeeID, Name, DepartmentName
FROM Employees
INNER JOIN Departments
USING (DepartmentID);

In this case, DepartmentID exists in both tables, so you can use USING (DepartmentID) to join on that
column.

In conclusion, use INNER JOIN with ON when you need more flexibility in specifying the join condition or
when the columns have different names. Use the USING clause when you are joining on columns with the
same name and want a simpler, cleaner result set.

Syntax for Using Clause:


SELECT TABLE1_COLUMN, TABLE2_COLUMN FROM TABLE1 JOIN TABLE2 USING
(TABLE2_COLUMN, TABLE1_COLUMN)
Example
SELECT EMPNO, ENAME, MGR, DNAME FROM EMP JOIN DEPT USING (DEPTNO,MGR);

Self-Join:
When a table is joined to itself then it is called as Self join or in less words we
Can just say “joining a table to itself is called self-join”.

Syntax for Self-join:


SELECT T1.TABLE1_COLUMN,T2. TABLE2_COLUMN FROM TABLE T1,TABLE T2 WHERE
T1.COLUMN = T2.COLUMN;

Example
SELECT WORKER.ENAME, MANAGER.ENAME FROM EMP WORKER, EMPMANAGER
WHERE WORKER.MGR = MANAGER.EMPNO;

INTRODUCTION TO OUTER JOIN & ITS TYPES


Use Outer join to return records which don’t have direct match.
In outer join operation, all records from the source table included in the resulteven though they don't satisfy the join
condition.

Syntax for Outer Join:


SELECT column names from both tables FROM table name 1 LEFT|RIGHT|FULL OUTER JOIN table name 2 on
condition;

Types of Outer Joins:


Outer joins are classified into three types:
1. Left Outer Join
2. Right Outer Join
3. Full Outer Join

Left Outer Join:


The left outer join produces a table that contains the matched data from the two tables, as well as the remaining rows
of the left table and null from the columns ofthe right table.
SyntaCx Lfo2r0L05ef–t ODuattearbaJsoeinS:ystems Lab Lab Manual –
05 SELECT T1. TABLE1_COLUMN, T2. TABLE2_COLUMN FROM TABLE1 T1, TABLE2 T2
WHERE T1.TABLE1_COLUMN = T2. TABLE2_COLUMN(+);
Example
SELECT E.ENAME, D.DEPTNO, D.DNAME FROM EMP E, DEPT D WHERE E.DEPTNO = D.DEPTNO (+);

NOTE: The outer join operator appears on only that side that has informationmissing.
The SQL-1999 standard:
SELECT T1. TABLE1_COLUMN, T2. TABLE2_COLUMN FROM TABLE1 T1 LEFT OUTER JOINTABLE2 T2 ON
T1.TABLE1_COLUMN = T2. TABLE2_COLUMN;
Example
SELECT E.ENAME, D.DEPTNO, D.DNAME FROM EMP E LEFT OUTER JOIN DEPT DON (E.DEPTNO =
D.DEPTNO);

Right Outer Join:


The right outer join returns a table with the matched data from the two tablesbeing joined, then the remaining rows
of the right table and null for the remaining left table's columns.

Syntax for Right Outer Join:


SELECT T1. TABLE1_COLUMN, T2. TABLE2_COLUMN FROM TABLE1 T1, TABLE2 T2
WHERE T1.TABLE1_COLUMN (+)= T2. TABLE2_COLUMN;
Example:
SELECT E.ENAME, D.DEPTNO, D.DNAME FROM EMP E, DEPT D WHEREE.DEPTNO(+) = D.DEPTNO;
SQL-1999 standard:
SELECT T1. TABLE1_COLUMN, T2. TABLE2_COLUMN FROM TABLE1 T1 RIGHT OUTERJOIN TABLE2 T2
ON T1.TABLE1_COLUMN = T2. TABLE2_COLUMN;
Example:
SELECT E.ENAME, D.DEPTNO, D.DNAME FROM EMP E RIGHT OUTER JOIN DEPT D ON (E.DEPTNO =
D.DEPTNO);

Full Outer Join:


The full outer join returns a table with the matched data of two table then remainingrows of both left table and then the
right table.

Syntax for Full Outer Join:


SELECT T1. TABLE1_COLUMN, T2. TABLE2_COLUMN FROM TABLE1 T1 FULL OUTERJOIN TABLE2
T2 ON T1.TABLE1_COLUMN = T2. TABLE2_COLUMN;
ExampClLe 2005 – Database Systems Lab Lab Manual – 05
SELECT E.ENAME, D.DEPTNO, D.DNAME FROM EMP E FULL OUTER JOIN DEPT DON
(E.DEPTNO = D.DEPTNO);

Conclusion for Joins:

INTRODUCTION TO SET OPERATOR


Set operators are used to join the results of two (or more) SELECT statements. The SEToperators available in Oracle
11g are UNION, UNION ALL, INTERSECT and MINUS.

All of the SET operators have the same order of precedence. Instead, Oracleevaluates queries from left to right or top
to bottom during execution. If parenthesesare used explicitly, the order may change because parentheses take
precedence over dangling operators.

TYPES OF SET OPERATOR


Following are the types of operators that are used for set in oracle. They are:
1. Union
2. Union all
3. Intersect
4. Minus
UnionCOLp2e0r0a5to–r:Database Systems Lab Lab Manual – 05
The SQL Union function joins the results of two or more SQL SELECT queries. The numberof datatypes and columns
in both tables on which the UNION operation is performedmust be the same in order to perform the union operation.
The duplicate rows areremoved from the result of the union operation.

Diagrammatic view of Union operator

Syntax for Union Operator


SELECT TABLE1_COLUMN, TABLE2_COLUMN FROM TABLE1 UNION SELECT
TABLE1_COLUMN_ID, TABLE2_COLUMN_ID FROM TABLE2
Example
SELECT employee_id, job_id FROM employees UNION SELECT employee_id,job_id FROM job_history;

Union All Operator:


With one exception, UNION and UNION ALL operate in a similar manner. UNION ALL,on the other hand, returns the
result set without eliminating duplication or sorting thedata.

Diagrammatic view of Union all operator

Syntax for Union All Operator


SELECT TABLE1_COLUMN, TABLE2_COLUMN FROM TABLE1 UNION ALL SELECT
TABLE1_COLUMN_ID, TABLE2_COLUMN_ID FROM TABLE2
ExampClLe 2005 – Database Systems Lab Lab Manual – 05
SELECT employee_id, job_id, department_id FROM employees UNION ALL
SELECT employee_id, job_id, department_id FROM job_history

Intersect Operator
It's used to join two SELECT statements together. The common rows from both SELECT statements are returned by
the Intersect procedure. The number of datatypes and columns in the Intersect operation must be the same. There are
no duplicates, and the data is arranged in ascending order by default.

Diagrammatic view of Intersect operator

Syntax for Intersect Operator


SELECT TABLE1_COLUMN, TABLE2_COLUMN FROM TABLE1 INTERSECT SELECT
TABLE1_COLUMN_ID, TABLE2_COLUMN_ID FROM TABLE2
Example
SELECT employee_id, job_id FROM employees INTERSECT SELECT employee_id,job_id FROM job_history;

Minus Operator
It combines the results of two SELECT statements into a single statement. The minus operator is used to show rows
that are present in the first query but not in the second.There are no duplicates, and the data is sorted ascending by
default.

Diagrammatic view of Minus operator


SyntaCx Lfo2r0M05in–usDOatpaebraasteorSystems Lab Lab Manual –
05 SELECT TABLE1_COLUMN, TABLE2_COLUMN FROM TABLE1 MINUS SELECT
TABLE1_COLUMN_ID, TABLE2_COLUMN_ID FROM TABLE2
Example
SELECT employee_id, job_id FROM employees MINUS SELECT employee_id, job_id FROM job_history

How to implement joins as set operator?

Practice Queries

select * from employees cross join departments;


select emp.first_name, emp.last_name, emp.department_id, dpt.department_name from employees
emp cross join departments dpt where emp.department_id = dpt.department_id;

select first_name, last_name, department_name from employees inner join departments on


departments.department_id = employees.department_id;
select first_name, last_name, department_name from employees natural join departments;
select first_name, last_name, department_name, department_id from employees join departments
using(department_id);
select emp1.employee_id, emp1.first_name AS EMPLOYEE_FNAME, emp1.last_name AS
EMPLOYEE_LNAME, emp2.first_name AS MANGER_FNAME, emp2.last_name AS
MANAGE_LNAME from employees emp1 inner join employees emp2 on
emp1.manager_id=emp2.manager_id;

select first_name, last_name, department_name from employees full outer join departments on
departments.department_id = employees.department_id;
SELECT e.*
FROM employees e
JOIN departments d ON e.department_id = d.department_id
JOIN locations l ON d.location_id = l.location_id
WHERE l.country_id = 'US'
AND l.state_province <> 'WA';

SELECT job_ID FROM employees UNION SELECT JOB_TITLE FROM JOBS;


SELECT department_id FROM employees
UNION
SELECT department_id FROM departments;

SELECT department_id FROM employees


MINUS
SELECT department_id FROM departments;

LAB EXERCISE
1. Write a query to list the name, job title, department name, and salary of the employees in ascending order of
their department.
2. Write a query to list the departments where at least two employees are working.
3. Fetch all records where the employee's salary is less than the lowest salary in the company.
4. Write a query to list the name, job title, annual salary, department name, and city of employees who earn
60000 or more annually and are not working as ANALYST.
5. Write a query to print details of the employees who are also Managers.
6. List department number and department name for all departments that have no employees.
7. Display employee name, salary, and department name where all employees match their department, including
employees with no assigned department.
8. Display the name, job title, department name, and city of employees who are working in departments located
in cities without a state province.
9. Write an SQL query to show records from one table that do not exist in another table.
10. Display all employees who belong to the US but not to the state of Washington.
11. Write a query to list the name, job title, department name, and location of employees who have a salary higher
than the average salary in their department.
12. Write a query to list employees who have changed their job title at least once in their job history.
13. List employees who work in the same department as their managers.
14. Write a query to list the name, department name, and location of employees who work in the same country as
their department location.
15. Write a query to find employees who work in departments with more than 5 employees.
16. Display a list of employees along with their managers' names.
17. Write a query to list the employee names and their department names where the department is located in a
different country than the employee’s residence.
18. Write a query to find employees who earn more than their department's average salary but less than the
highest salary in the company.
CL2005 – Database Systems Lab Lab Manual – 05

19. Display a list of all employees who have worked in multiple departments, showing their job history and
department names.
20. Write a query to find employees who have worked in more than one region throughout their career.
21. List all employees and the region they are working in.
22. Find employees who have the same last name but work in different departments.
23. List employees who have changed job titles more than twice.
24. Show job titles that are not currently assigned to any employee.
25. Find the top 3 employees with the highest salaries in each department.

You might also like