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

Joins 2

Uploaded by

sriprasad.a14
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)
37 views

Joins 2

Uploaded by

sriprasad.a14
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/ 7

JOINS IN SQL: Joins are used for retrieving the data from more than one table at a

time. Joins can be classified into the following types.

NON-ANSI JOINS(OLD STYLE FORMAT):

 EQUI JOIN
 NON EQUI JOIN
 SELF JOIN

ANSI-JOINS(NEW STYLE FORMAT):

 INNER JOIN
 OUTER JOIN
o LEFT OUTER JOIN
o RIGHT OUTER JOIN
o FULL OUTER JOIN
 CROSS JOIN
 NATURAL JOIN

NOTE:

When we retrieve data from multiple tables based on “WHERE” clause condition

Is called as NON-ANSI joins where as with “ON” clause condition is called as

ANSI joins.

1. EQUI JOIN

A join which contains an equal to ‘=’ operator in the joins condition. Only matching
records are displayed. Here joining conditional columns must belongs to the same data
types. The tables having the common columns then only we are using equi joins. Equi
joins are also called simple joins or inner joins.

Syntax:-

Select column1,column2 from table1,table2

Where table1.common column=table2.common column

And ……..(this is join condition);

Examples:-

Sql> SELECT emp. empno, emp. ename, deptno, dept. dname, dept.loc

FROM EMP, DEPT

WHERE emp.deptno=dept.deptno;
ERROR:- column ambiguously defined

To avoid ambiguity error in future generally we must specify every column name
along

With table name using alias names.

If there no common column names between the two tables the qualification is not
necessary But it is better.

Note :- using equi join we are retrieving only matching rows only.

Sql> SELECT emp. empno, emp. ename,emp. deptno, dept. dname, dept.loc

FROM EMP, DEPT

WHERE emp.deptno=dept.deptno;

Explaination:-

• The select clause specifies the column names to retrieve:

Empno,ename ,deptno which columns in the emp table

Dname,loc which are columns in the dept table.

• T he from clause specifies the two tables that the database must access:

Emp table

Dept table

• The where clause specifies how the tables are to be joined:

Emp.deptno=dept.deptno

Because deptno column is common to the both tables,it must be prefixed by the table
name to avoid ambiguity.

Sql> select ename,sal,d.deptno from emp ,dept

where emp.deptno=dept.deptno;

sql> select ename,job,sal ,dept.deptno ,dname,loc from emp,dept

where emp.deptno=dept.deptno

and loc='CHICAGO';

SQL> select empno,ename,sal*12 annusal ,dept.deptno ,loc from emp,dept


where emp.deptno=dept.deptno and dname='ACCOUNTING';

SQL> select ename,job,sal,emp.deptno,dname from emp,dept

where emp.deptno=dept.deptno and job='MANAGER';

Using table aliases:-

Qualifying column names with the table names can be very time consuming,
particularly if table names are lengthy.we can use table aliases instead of table names.
Just as column aliases gives column another name,a table aliases gives a table another
name. Table aliases help to keep sql code smaller ,therefore using less memory. Table
alias names will improve the performance of while retrieving data through joins. The
table name is specified full ,followed by space and then the table alias. The alias can
be up to 30 characters in length,but the shorter they are the better. The table alias
should be meaningful. The table alias is valid only for the current sql statement.

Note:-the emp table has been given an alias of ‘e’,and the dept table has as alias of ‘d’.

Examples of table aliases using equi joins:-

Sql> select e.empno,e.ename,e.sal,e.deptno,d.dname,d.loc from emp e,dept d

where e.deptno=d.deptno;

sql> select d.deptno, dname,sum(sal) from emp e,dept d

where e.deptno=d.deptno

group by d.deptno, dname;

sql> select dname,sum(sal) from emp e,dept d

where e.deptno=d.deptno

group by dname

having sum(sal)>8000;

2. NON-EQUI JOIN

A join which contains an operator other than equal to ‘=’ in the joins condition.

The SQL NON EQUI JOIN uses comparison operator instead of the equal sign like >, <,
>=, <= along with conditions.

It is used between operator ,it is called as between joins.

Syntax:-

Select * FROM table_name1, table_name2


WHERE table_name1.column [> | < | >= | <= ] table_name2.column;

Examples:-

sql> select ename,sal,losal,hisal from emp,salgrade

where sal>=losal and sal<=hisal;

Explaination:-all of the employees salaries lie within the limits provided by the
salgrade table. That is no employee earns less than the lowest value contained in the
losal column, or more than the highest value contained in the hisal column

other conditions such as <=,>= can be used ,but between is simplest. Remember to
specify the low value first and high value last when using between.

Sql>select empno,ename,sal,losal,hisal from emp ,salgrade

Where sal between losal and hisal;

Sql> select empno,ename,job,dname,loc from emp e,dept d where e.deptno > d.deptno;

sql> select ename,sal,losal,hisal from emp,salgrade

where sal>=losal and sal<=hisal;

3. Self Join:-

A self join is a join of a table to itself. This table appears twice in the FROM clause
and is followed by table aliases that qualify column names in the join condition. To
perform a self join, Oracle Database combines and returns rows of the table that satisfy
the join condition.

Here joining conditional columns must by belongs to the same data type.

When ever we are working with self joins in database we should write alias

Names to tables.

In self join we must create alias name in from clause.

Syntax:-

• SELECT a.column_name, b.column_name...

FROM table1 a, table1 b


WHERE a.common_filed = b.common_field;

Examples:-

Sql> select e.empno,e.ename,m.ename from emp e,emp m

where e.mgr=m.empno;

explaination:-the above employee joins the emp table to itself. To simultate two tables in
the from clause,there two aliases ,namely e and m for the same table ,emp.in this
example ,the where clause contains the join that means “where employee (e) mgr
number matches the empno for the employee(m)

sql> select e1.ename,e2.ename from emp e1,emp e2

where e1.mgr=e2.empno

and e1.hiredate<e2.hiredate;

SQL>select e1.ename,e2.ename from emp e1,emp e2

where e1.mgr=e2.empno and e1.job='SALESMAN';

INNER JOIN:-

This will display all the records that have matched. When tables contains
common column,then only this join is used and Inner Join is also called as Simple Joins
or Equi Joins.

Syntax:-

Select * FROM table1 INNER JOIN table2 ON table1.column_name =


table2.column_name;

Sql> select empno,ename,job,dname,loc from emp e inner join dept d

on (e.deptno=d.deptno);

Explaination:-the on clause can be used as follows to join columns that have different
names.

sql>select emp.ename from emp join dept on (emp.deptno = dept.deptno) and


emp.ename like 'A%';

sql>

select emp.ename from emp join dept on (emp.ename like 'A%') and emp.deptno =
dept.deptno;

sql> select e.ename "employee",m.ename "manager" from emp e


inner join emp m on (e.mgr=m.empno);

A1 TABLE A2 TABLE

A B C A B

X Y Z X Y

SQL>select *from a1,a2

On z1.a=z2.a and z1.b=z2.b;

OUTTER JOIN: It is an extension for the Equi join. In Equi join condition we will be
getting the matching data from the tables only. So we loss un matching data from the
tables.

To overcome the above problem we use outer join which are used to getting matching
data as well as UN matching data from the tables. This outer join again classified into
three types

LEFT OUTER JOIN: It will retrieve or get matching data from both table as well as UN
matching data from left hand side table

Ex: SELECT * FROM EMP LEFT OUTER JOIN DEPT ON EMP.EID=DEPT.DNO;

RIGHT OUTER JOIN: It will retrieve or get matching data from both table as well as UN
matching data from right hand side table

Ex: SELECT * FROM EMP RIGHT OUTER JOIN DEPT ON EMP.EID=DEPT.DNO;

FULL OUTER JOIN: It will retrieve or get matching data from both table as well as UN
matching data from left hand side table plus right hand side table also.

Ex: SELECT * FROM EMP FULL OUTER JOIN DEPT ON EMP.EID=DEPT.DNO;

CROSS JOIN: Cross join is used to join more than two tables without any condition we
call as a cross join. In cross join each row of the first table join with each row of the
second table.

So, if the first table contain ‘m’ rows and second table contain ‘n’ rows then output will
be ‘m*n’ rows.

Syntax:-

Select * FROM table1 CROSS JOIN table2;

Examples:-
Sql>select empno,ename,job,dname,loc from emp cross join dept;

Sql>select empno,sal,deptno,grade ,hisal from emp,salgrade;

Sql> SELECT * FROM EMP, DEPT

Sql>SELECT * FROM EMP CROSS JOIN DEPT

NATURAL JOIN: SQL Server not supporting Natural join whereas supports in ORACLE.

Making To Joins Three Tables:

CREATE TABLE STUDENTS (SID Int, SNAME varchar (20), SMBNO char (10), CID Int)

INSERT STUDENT VALUES (1,'aa',’7894561233’, 10), (2,'bb',’9874563211’, 20),


(3,'cc',’8749653215’, 30), (4,’dd’,’7788996655’, 40)

CREATE TABLE COURSES (CID int, CNAME Varchar (20), CFEE decimal (6, 2))

INSERT COURSES VALUES (10,'c', 500), (20,'c++', 1000), (50,'sql', 1800), (60,’.net’, 3500),
(70,’sap’, 8000)

CREATE TABLE REGISTER (SNO int, REGDATE date, CID Int)

INSERT REGISTER VALUES (100,’2014/10/20’, 10), (101,’2014/10/21’, 80),


(102,’2014/10/22’, 90)

Select * from student s , course c , register r where s.cid=c.cid and c.cid=r.cid;

Select * from course c inner join student s on c.cid=s.cid inner join register r on
s.cid=r.cid;

Select * from student s left outer join course c on s.cid=c.cid left outer join register r on
c.cid=r.cid

You might also like