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

SQL

Uploaded by

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

SQL

Uploaded by

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

PL/SQL – Joins

One of the most important features of SQL is the ability to define relationships between multiple
tables and draw information from them in terms of these relationships, all within a single command.

With joins we can combine columns from different tables.

Join Condition

Condition specified in the where clause to evaluate the joins is called Join Condition. Most of the
queries contain Join Conditions that compare two or more columns.

Example: -

SQL> select deptname,empname from dept d,emp e

where d.deptno = e.deptno;

Here “d.deptno = e.deptno” is the join condition.

For joins having three tables, Oracle joins first two of them based on the join condition and then
joins with the third one based on the join condition containing third table. Oracle does this process
until all tables are joined.

Types of Joins:

1) Equi Join
2) Non-Equi Joins
3) Self-Join
4) Cross join (Cartesian Join)
5) Outer join
a) Left outer join
b) Right outer join
c) Full outer join

1) Equi join -
When two tables joined together using equality operator, then it is called Equi Join. This join
retrieves information by using equality condition. This is represented by (=) sign.
Example:
List the employee numbers, names, department numbers and dept names

Select empno,ename,emp.deptno,dname from emp,dept


where emp.deptno=dept.deptno;

Here, the deptno column exists in both the tables. To avoid ambiguity, the column name
should be qualified with the table name. Both the table names need to be specified (emp and
dept) where clause defines the joining condition.
2) Non Equi Join -
A nonequi join is an inner join statement that uses an unequal operation (i.e.: <>, >, <, !=,
BETWEEN, etc.) to match rows from different tables.
Example:
Write a query to display those employees details whose salary of emp1 table >= salary of
emp table employees

select empno,ename,emp1.sal,emp.sal from emp1,emp


where emp1.sal>=emp.sal;

3) Self-Join –
To join a table to itself means that each row of the table is combined with itself with every
other row of the table. The self-join can be viewed as a join of two copies of the same table.
The table is not actually copied, but SQL performs the command as though it were.
Example:
List out the names of the manager with the employees in the emp table

Select worker.ename, manager.ename ‘Manager’ from emp worker, emp manager


where worker.mgr=manager.empno;

4) Cross join (Cartesian Join) -


A Cross Join or Cartesian join or Cartesian product is a join of every row of one table to every
row of another table. This type of join can be used in situations where it is desired, to select
all possible combinations of rows and columns from both tables. This kind of join is usually
not preferred as it may run for a long time and produce a huge result set that may not be
useful.
Example:
Display the list of employees working in each department. Display the employee information
even if no such department belongs to the dept table and also display the department
details even if no employee belongs to that department

select empno,ename,sal,emp.deptno,dname,loc from emp Cross join dept;

5) Outer Join –

Outer Join is used to join tables with sparse data in one or more table for the columns used
in the join condition. Outer join returns all rows which satisfies the join condition plus other
records from one table and no records from other table. The column will be nullified for the
records has no data.

a) Left outer join –


This left outer join displays all matching records of both tables along with the
records in left hand side table of join clause which are not in right hand side table of
join clause.
Example:
Display the list of employees working in each department. Display the department
information even if no employee belongs to that department
select empno,ename,sal,emp.deptno,dname,loc from emp, dept
where emp.deptno(+)=dept.deptno;

b) Right Outer Join -


This right outer join displays all matching records of both tables along with the
records in left hand side table of join clause which are not in right hand side table of
join clause.
Example:
Display the list of employees working in each department. Display the employee
information even if no such department belongs to the dept table

select empno,ename,sal,emp.deptno,dname,loc from emp, dept


where emp.deptno=dept.deptno(+);

c) Full Outer Join –


A full outer join returns all rows from both the tables left and right side of the join
clause, extended with nulls if they do not satisfy the join condition.
Example:
Display the list of employees working in each department. Display the employee
information even if no such department belongs to the dept table

select empno,ename,sal,emp.deptno,dname,loc from emp, dept


ON emp.deptno=dept.deptno;

You might also like