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

Understanding SQL Joins and Types

Uploaded by

maheshebs777
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views10 pages

Understanding SQL Joins and Types

Uploaded by

maheshebs777
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Page 1 of 10

Rollup, Cube
============

These clauses are optional clauses used along with group by clause only. These clauses are used
to calculate sub total, grand total automatically.

Rollup and cube will find sub & grand total based on single column or multiple column.

Synatx
=======
Select column1,column2,……….
From tablename
Group by rollup(column1,column2,……….);

select ename,sum(sal)
from emp
group by cube( ename)

select ename,sum(sal)
from emp
group by rollup( ename)

JOINS
=====
Join is an operation performed to combine data stored in multiple tablesles. To combine data
stored in two tables we need to join those two tables.

Types of Joins
=============
1)Equi join/inner join
2)Outer join
Page 2 of 10

3)non equi join


4)self join
5)natural join
6)cross join (or) cartesian join

1)Equi join/inner join


===================

 To perform equi join, between two there must be a common field and name of the
common field need not be same.

 Equi join is performed based on the common field with same data type.

 Equi join based on (=) operator

 This join returns only the rows with a match in both tables

Syntax
=====
[Link] = [Link]

In join queries declare table alias and prefix column names with table alias for two
reasons
1)for faster execution
2)to avoid column ambiguously defined

Display ename,sal,deptno,dname,Loc?

select ename,sal,[Link],dname,loc
from
emp e,
dept d
where
[Link]=[Link];
Page 3 of 10

ANSI(AMERICAN NATIONAL STANDARDS INSTITUTE)


======================================

USE “ON” clause or “using” clause for join conditions instead of “where” clause.

ON Clause
=========
select ename,sal,[Link],dname,loc
from emp e inner join dept d
on [Link]=[Link];

USING CLAUSE
============
Using clause can be used when name of the common field is same.

select [Link],[Link],[Link]
from emp e inner join dept d
using(deptno);

Outer join
========
Equi joins only matching records but cannot return unmatched records but to get
unmatched records also perform outer join

3 types
=========

1)Left outer join


Page 4 of 10

2)Right outer join


3)full outer join

1)Left outer join


==============

It returns all rows(matched and un matched) from left side table and matching rows
from right side table.

select ename,sal,[Link],dname,loc
from
emp e,
dept d
where
[Link]=[Link](+);

2) Right outer join


=================

It returns all rows(matched and un matched) from right side table and matching rows
from left side table.

select ename,sal,[Link],dname,loc
from
emp e,
dept d
where
[Link](+)=[Link]

full outer join


=============
Page 5 of 10

It returns all rows from both tables

select ename,sal,[Link],dname,loc
from
emp e
full outer join dept d
on
[Link]=[Link]

select ename,sal,[Link],dname,loc
from
emp e,
dept d
where
[Link]=[Link](+)
union
select ename,sal,[Link],dname,loc
from
emp e,
dept d
where
[Link](+)=[Link]

Non equi join


===============
Non equi join is performed when tables are not sharing common field.

This join is called non equi join because here join condition is not based on (=) operator

(<,<=,>,>=,between , in,………..)
Page 6 of 10
Page 7 of 10

Display ename,sal,grade ?

select ename,sal,[Link],[Link],[Link]
from emp e,
salgrade s
where [Link] between [Link] and [Link]

DISPLAY GRADE 3 EMPLOYEES LIST?


=============================

select [Link],[Link],[Link],[Link],[Link]
from emp e,
salgrade s
where [Link] between [Link] and [Link]
and [Link]=3;

self join
========
Joining a table to itself is called self join.

=> in self join a record in one table with another record of same table.

Syntax:
=======
From tablename aliasname 1,tablename aliasname2

Method 1:
Compare one value with all other column values in same column

Write a query to display the employees who are getting same salary as scott salary from
emp table using self join.
Page 8 of 10

SELECT [Link],[Link]
FROM EMP E1,
EMP E2
WHERE [Link]=[Link]
AND [Link]='SCOTT';

Compare two different column values from same table (these belongs to same datatype)

Write a query to display the employees names and their manager names from emp
table using self join.

SELECT [Link] EMPLOYEE,[Link] MANAGER,


FROM EMP E1, EMP E2
WHERE [Link]=[Link]

Write a query to display the employees who are getting more salary than their manager
salary from emp table using self join.

select [Link] employee,[Link],[Link] manager,[Link]


from emp e1,
emp e2
where [Link]=[Link]
and [Link]>[Link];

DISPLAY BLAKE’S MANAGER NAME?

select [Link],[Link],[Link],[Link] manager_name


from emp x,
emp y
where [Link]=[Link]
and [Link]='BLAKE';
Page 9 of 10

natural join
==========
This join also returns matching rows only. This join performance very high compare to
inner [Link] this we are not allowed to use joining condition.

Syntax
=======

Select * from tablename1 natural join tablename 2

select * from emp e natural join dept d;

cross join (or) cartesian join


=========================

Cross join returns cross product or cartesian product of two tables.

Ex :
====
A=1,2 (AxB)=(1,3)(1,4)(2,3)(2,4)
B=3,4

If cross join performed between two tables then each record of 1 st table joined with each and
every record of second table.

select * from emp e cross join dept d;


Page 10 of 10

You might also like