Lab Manual 5
Lab Manual 5
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.
ISO Standard:
SELECT * FROM TABLE1 CROSS JOIN TABLE2;
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
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.
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”.
Example
SELECT WORKER.ENAME, MANAGER.ENAME FROM EMP WORKER, EMPMANAGER
WHERE WORKER.MGR = MANAGER.EMPNO;
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);
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.
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.
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.
Practice Queries
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';
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.